Commit 7dc89f1a authored by Jean-Benoist Leger's avatar Jean-Benoist Leger

maj, je ne sais plus ce qui a été fait

parent 0e1cd65d
......@@ -51,7 +51,7 @@ class MmBot(irc.IRCClient):
def __init__(self,botconf):
self.botconf = botconf
self.nickname = self.botconf['nickname']
self.codes = legifrance.codes('legifrance.conf')
self.codes = legifrance.legi('legifrance.conf')
self.view = {}
def connectionMade(self):
......@@ -79,85 +79,137 @@ class MmBot(irc.IRCClient):
"""This will get called when the bot joins the channel."""
self.logger.log("[I have joined %s]" % channel)
def c_help(self, user):
rep = u"Quand du texte est rencontré sur un channel, de la forme :\n"
rep+= u" [Name Article], [Article du Name], ou [Article de la Name]\n"
rep+= u" alors le lien vers legifrance est affiché.\n"
rep+= u" Exemple: [CP 323-1], [323-1 du CP] ou [30 de la LCEN].\n"
rep+= u"Commandes disponnibles en query:\n"
rep+= u" - help\n"
rep+= u" - list\n"
rep+= u" - add (code|texte) <name> <codeid> (e.g. \"add code CP LEGITEXT000006070719\")\n"
rep+= u" - expire <codename> (e.g. \"expire CP\")\n"
rep+= u" - reload_legifranceconf\n"
rep+= u" - reload_botconf\n"
rep+= u" - join <channel>\n"
rep+= u"Commandes disponnibles sur les chans:\n"
rep+= u" - !legifrance help\n"
rep+= u" - !legifrance list\n"
rep+= u" - !legifrance add (code|texte) <name> <codeid> (e.g. \"!legifrance add code CP LEGITEXT000006070719\")\n"
rep+= u" - !legifrance expire <codename> (e.g. \"!legifrance expire CP\")\n"
rep+= u" - !legifrance reload_legifranceconf\n"
rep+= u" - !legifrance reload_botconf\n"
rep+= u" - !legifrance join <channel>\n"
rep+= u" - !legifrance part\n"
self.msg(user, rep.encode('utf8'))
def c_list(self, user):
rep = "Codes disponnibles :\n"
for c in self.codes.conf['codeids'].keys():
rep += ' - %s : %s\n' % (c,self.codes.conf['codeids'][c])
rep+= "Textes disponnibles :\n"
for c in self.codes.conf['textids'].keys():
rep += ' - %s : %s\n' % (c,self.codes.conf['textids'][c])
self.msg(user, rep)
def c_add(self, user, what, name, nameid):
if what == 'code':
if self.codes.conf['codeids'].has_key(name):
self.msg(user, "Ce code existe.")
else:
if self.codes.conf['textids'].has_key(name):
self.msg(user, "Un texte existe sous le meme nom.")
else:
self.codes.conf['codeids'][name]=nameid
self.codes.conf.write()
self.msg(user, "done")
if what == 'texte':
if self.codes.conf['textids'].has_key(name):
self.msg(user, "Ce texte existe.")
else:
if self.codes.conf['codeids'].has_key(name):
self.msg(user, "Un code existe sous le meme nom.")
else:
self.codes.conf['textids'][name]=nameid
self.codes.conf.write()
self.msg(user, "done")
def c_expire(self, user, name):
if (not self.codes.conf['codeids'].has_key(name)) and (not self.codes.conf['texteids'].has_key(name)):
self.msg(user, "Aucun texte et code de ce nom")
if self.codes.force_ct_reload(codename):
self.msg(user, "done")
else:
self.msg(user, "nothing to do")
def c_reload_legifranceconf(self, user):
self.codes.conf.reload()
self.msg(user, "done")
def c_reload_botconf(self, user):
self.botconf.reload()
self.msg(user, "done")
def privmsg(self, user, channel, msg):
"""This will get called when the bot receives a message."""
user = user.split('!', 1)[0]
self.logger.log("%s: <%s> %s" % (channel, user, msg))
forbot=False
m=''
if channel == self.nickname:
# pv
if re.match('help',msg):
rep = u"Quand du texte est rencontré sur un channel, de la forme :\n"
rep+= u" [CodeName Article] ou [Article du CodeName]\n"
rep+= u" alors le lien vers legifrance est affiché.\n"
rep+= u" Exemple: [CP 323-1] ou [323-1 du CP].\n"
rep+= u"Commandes disponnibles en query:\n"
rep+= u" - list\n"
rep+= u" - add <codename> <codeid> (e.g. \"add CP LEGITEXT000006070719\")\n"
rep+= u" - expire <codename> (e.g. \"expire CP\")\n"
rep+= u" - reload_legifranceconf\n"
rep+= u" - reload_botconf\n"
rep+= u" - join <channel>\n"
rep+= u"Commandes disponnibles sur les chans:\n"
rep+= u" - !legifrance join <channel>\n"
rep+= u" - !legifrance part\n"
self.msg(user, rep.encode('utf8'))
if re.match('list',msg):
rep = "Codes disponnibles :\n"
for c in self.codes.conf['codeids'].keys():
rep += ' - %s : %s\n' % (c,self.codes.conf['codeids'][c])
self.msg(user, rep)
a=re.match('add ([A-Za-z0-9]+) ([A-Za-z0-9]+)', msg)
forbot=True
m=msg
a=re.match('!legifrance (.*)',msg)
if a:
forbot=True
m=a.groups()[0]
if forbot:
# pv or prefixed
if re.match('help',m):
self.c_help(user)
if re.match('list',m):
self.c_list(user)
a=re.match('add (code|texte) ([A-Za-z0-9]+) ([A-Za-z0-9]+)', m)
if a:
codename = a.groups()[0]
codeid = a.groups()[1]
if self.codes.conf['codeids'].has_key(codename):
self.msg(user, "Ce code existe.")
else:
self.codes.conf['codeids'][codename]=codeid
self.codes.conf.write()
self.msg(user, "done")
self.c_add(user, a.groups()[0], a.groups()[1], a.groups()[2])
a=re.match('expire ([A-Za-z0-9]+)', msg)
a=re.match('expire ([A-Za-z0-9]+)', m)
if a:
codename = a.groups()[0]
if not self.codes.conf['codeids'].has_key(codename):
self.msg(user, "Ce code n'existe pas.")
if self.codes.force_code_reload(codename):
self.msg(user, "done")
else:
self.msg(user, "nothing to do")
self.c_expire(user,a.groups()[0])
if re.match('reload_legifranceconf', msg):
self.codes.conf.reload()
self.msg(user, "done")
if re.match('reload_legifranceconf', m):
self.c_reload_legifranceconf(user)
if re.match('reload_botconf', msg):
self.botconf.reload()
self.msg(user, "done")
if re.match('reload_botconf', m):
self.c_reload_botconf(user)
a=re.match('join (#[A-Za-z0-9-]+)', msg)
a=re.match('join (#[A-Za-z0-9-]+)', m)
if a:
self.join(a.groups()[0])
if re.match('part',m):
if channel == self.nickname:
self.msg(user, 'Cette commande doit être lancé sur un cannal')
else:
self.part(channel)
else:
# public
a=re.match('!legifrance join (#[A-Za-z0-9-]+)', msg)
if a:
self.join(a.groups()[0])
if re.match('!legifrance part', msg):
self.part(channel)
# coeur de metier
m = msg
reg = r'(?:'
reg+= r'\[(?P<codename1>[A-Za-z0-9]+) (?P<article1>[^ \]]+)\]'
reg+= r'|'
reg+= r'\[(?P<article2>[^ \]]+) du (?P<codename2>[A-Za-z0-9]+)\]'
reg+= r'|'
reg+= r'\[(?P<article3>[^ \]]+) de la (?P<codename3>[A-Za-z0-9]+)\]'
reg+= r')'
while True:
a = re.match('.*?%s'%reg, m)
......@@ -172,6 +224,10 @@ class MmBot(irc.IRCClient):
codename = d['codename2']
article = d['article2']
form = '%s du %s' % (article, codename)
if not d['codename3'] is None:
codename = d['codename3']
article = d['article3']
form = '%s de la %s' % (article, codename)
trop_jeune = False
if self.view.has_key( (channel,codename,article) ):
......
......@@ -12,3 +12,11 @@ CMF = LEGITEXT000006072026
CPCE = LEGITEXT000006070987
CRoute = LEGITEXT000006074228
CAssur = LEGITEXT000006073984
CEnergie = LEGITEXT000023983208
CE = LEGITEXT000006070239
[textids]
LCEN = JORFTEXT000000801164
LoiInfoLib = JORFTEXT000000886460
Loi81LibPresse = LEGITEXT000006070722
Constitution = LEGITEXT000006071194
DecretEnsCachan = JORFTEXT000023375575
......@@ -33,8 +33,8 @@ import configobj
def get_articles_from_page(link):
articles={}
r = requests.get(link)
reg = '^.*?<a href="(affichCodeArticle\.do[^"]*idArticle[^"]*)" title="En savoir plus sur l\'article ([^"]+)"'
reg = '^.*?<a href="/?(affich[A-Za-z]*Article\.do[^"]*idArticle[^"]*)" title="En savoir plus sur l\'article ([^"]+)"'
c=r.content
while True:
......@@ -75,36 +75,50 @@ def get_code(codename,codeids):
articles = {}
for link in links:
articles.update(det_articles_from_page)
articles.update(get_articles_from_page(link))
return articles
class codes:
def get_text(textname, textids):
if not textids.has_key(textname):
return None
textid = textids[textname]
url = 'https://www.legifrance.gouv.fr/affichTexte.do?cidTexte=' + textid
return get_articles_from_page(url)
class legi:
def __init__(self,conffile):
self.codes = {}
self.cache = {}
self.conf = configobj.ConfigObj(conffile)
def get(self,codename,codearticle):
def get(self,name,article):
t = time.time()
if self.codes.has_key(codename):
if t - self.codes[codename]['timestamp'] > int(self.conf['expire']):
del self.codes[codename]
if self.codes.has_key(codename):
if self.cache.has_key(name):
if t - self.cache[name]['timestamp'] > int(self.conf['expire']):
del self.cache[name]
if self.cache.has_key(name):
pass
else:
articles = get_code(codename,self.conf['codeids'])
if name in self.conf['codeids'].keys():
articles = get_code(name,self.conf['codeids'])
else:
articles = get_text(name,self.conf['textids'])
if not articles is None:
self.codes[codename] = {'timestamp':t, 'articles':articles}
self.cache[name] = {'timestamp':t, 'articles':articles}
if self.codes.has_key(codename):
if self.codes[codename]['articles'].has_key(codearticle):
return self.codes[codename]['articles'][codearticle]
if self.cache.has_key(name):
if self.cache[name]['articles'].has_key(article):
return self.cache[name]['articles'][article]
return None
def force_code_reload(self,codename):
if self.codes.has_key(codename):
self.codes[codename]['timestamp']=0
def force_ct_reload(self,name):
if self.codes.has_key(name):
self.codes[name]['timestamp']=0
return True
return False
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment