Commit 4227d4f8 authored by Jean-Benoist Leger's avatar Jean-Benoist Leger

handle texts

parent 1a675ea7
......@@ -53,6 +53,7 @@ class MmBot(irc.IRCClient):
self.nickname = self.botconf['nickname']
self.codes = legifrance.legi('legifrance.conf')
self.view = {}
self.view_content = {}
def connectionMade(self):
irc.IRCClient.connectionMade(self)
......@@ -86,6 +87,10 @@ class MmBot(irc.IRCClient):
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"De même quand il est rencontré un motif de la forme {Name Article},\n"
rep+= u" {Article du Name} ou {Article de la Name}, alors le contenu\n"
rep+= u" de l'article 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"
......@@ -207,11 +212,11 @@ class MmBot(irc.IRCClient):
# coeur de metier
m = msg
reg = r'(?:'
reg+= r'\[(?P<codename1>[A-Za-z0-9]+) (?P<article1>[^ \]]+)\]'
reg+= r'(?P<mark1a>(?:\[|\{))(?P<codename1>[A-Za-z0-9]+) (?P<article1>[^ \]]+)(?P<mark1b>(?:\]|\}))'
reg+= r'|'
reg+= r'\[(?P<article2>[^ \]]+) du (?P<codename2>[A-Za-z0-9]+)\]'
reg+= r'(?P<mark2a>(?:\[|\{))(?P<article2>[^ \]]+) du (?P<codename2>[A-Za-z0-9]+)(?P<mark2b>(?:\]|\}))'
reg+= r'|'
reg+= r'\[(?P<article3>[^ \]]+) de la (?P<codename3>[A-Za-z0-9]+)\]'
reg+= r'(?P<mark3a>(?:\[|\{))(?P<article3>[^ \]]+) de la (?P<codename3>[A-Za-z0-9]+)(?P<mark3b>(?:\]|\}))'
reg+= r')'
while True:
a = re.match('.*?%s'%reg, m)
......@@ -222,25 +227,41 @@ class MmBot(irc.IRCClient):
codename = d['codename1']
article = d['article1']
form = '%s %s' % (codename, article)
marks = (d['mark1a'],d['mark1b'])
if not d['codename2'] is None:
codename = d['codename2']
article = d['article2']
form = '%s du %s' % (article, codename)
marks = (d['mark2a'],d['mark2b'])
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) ):
if time.time()-self.view[(channel,codename,article)] < int(self.botconf['remain_time']):
trop_jeune = True
if not trop_jeune:
link = self.codes.get(codename,article)
if not link is None:
self.msg(channel,'[%s]: %s' % (form, link))
self.view[(channel,codename,article)] = time.time()
marks = (d['mark3a'],d['mark3b'])
if (marks[0] == '{' and marks[1]=='}') or (marks[0]=='[' and marks[1]=']') :
trop_jeune = False
if marks[0] == '{':
used_view = self.view_content
else:
used_view = self.view
if used_view.has_key( (channel,codename,article) ):
if time.time()-used_view[(channel,codename,article)] < int(self.botconf['remain_time']):
trop_jeune = True
if not trop_jeune:
if marks[0] == '[':
link = self.codes.get(codename,article)
if not link is None:
self.msg(channel,'[%s]: %s' % (form, link))
else:
txt = self.codes.get_content(codename,article)
if not txt is None:
self.msg(channel,'{%s}:\n%s' (form, txt))
used_view[(channel,codename,article)] = time.time()
m= re.sub(reg,'',m,1)
......
......@@ -29,6 +29,7 @@ import requests
import re
import time
import configobj
import html2text
def get_articles_from_page(link):
articles={}
......@@ -50,6 +51,19 @@ def get_articles_from_page(link):
return articles
def get_article_content(link):
r = requests.get(link)
reg = '^.*<div class="corpsArt">(.*)</div>.*?Liens relatifs à cet article'
c = r.content.decode()
a = re.match(reg, c, re.DOTALL)
if a:
content = a.groups()[0]
h = html2text.HTML2Text()
h.ignore_links=True
return h.handle(content)
return None
def get_code(codename,codeids):
if not codename in codeids:
......@@ -116,6 +130,17 @@ class legi:
return self.cache[name]['articles'][article]
return None
def get_content(self,name,article,irc_format=False):
link = self.get(name,article)
if link is None:
return None
txt = get_article_content(link)
txt = re.sub('^\n+','',txt)
txt = re.sub('\n+$','',txt)
txt = re.sub('\n{3,}','\n\n',txt)
txt = re.sub('\n\n','\n \n',txt)
return txt
def force_ct_reload(self,name):
if name in self.codes:
self.codes[name]['timestamp']=0
......
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