Commit be95acda authored by Jean-Benoist Leger's avatar Jean-Benoist Leger

initial commit

parents
#!/usr/bin/python
# -*- coding: utf-8 -*-
# twisted imports
from twisted.words.protocols import irc
from twisted.internet import reactor, protocol
from twisted.python import log
# system imports
import time, sys
import legifrance
import random
import re
class MessageLogger:
def log(self, message):
print message
class MmBot(irc.IRCClient):
def __init__(self):
self.nickname = 'legifrance'
self.codes = legifrance.codes('legifrance.conf')
self.view = {}
def connectionMade(self):
irc.IRCClient.connectionMade(self)
self.logger = MessageLogger()
self.logger.log("[connected at %s]" %
time.asctime(time.localtime(time.time())))
def connectionLost(self, reason):
irc.IRCClient.connectionLost(self, reason)
self.logger.log("[disconnected at %s]" %
time.asctime(time.localtime(time.time())))
# callbacks for events
def signedOn(self):
"""Called when bot has succesfully signed on to server."""
self.join(self.factory.channel)
def kickedFrom(self, channel, kicker, message):
self.logger.log("[kicked from %s by %s at %s]" %
( channel, kicker,
time.asctime(time.localtime(time.time())))
)
def joined(self, channel):
"""This will get called when the bot joins the channel."""
self.logger.log("[I have joined %s]" % channel)
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" % (user, msg))
# Check to see if they're sending me a private message
if channel == self.nickname:
if re.match('help',msg):
rep = "Commandes disponnibles en query:\n"
rep+= " - list\n"
rep+= " - add <codename> <codeid> (e.g. \"add CP LEGITEXT000006070719\")\n"
rep+= " - expire <codename> (e.g. \"expire CP\")\n"
rep+= " - reloadconf\n"
rep+= " - join <channel>\n"
rep+= "Commandes disponnibles sur les chans:\n"
rep+= " - !legifrance join <channel>\n"
rep+= " - !legifrance part\n"
self.msg(user, rep)
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)
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")
a=re.match('expire ([A-Za-z0-9]+)', msg)
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")
if re.match('reloadconf', msg):
self.codes.conf.reload()
self.msg(user, "done")
a=re.match('join (#[A-Za-z0-9-]+)', msg)
if a:
self.join(a.groups()[0])
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
while True:
a = re.match('.*?\[([A-Za-z0-9]+) ([^ \]]+)\]', m)
if a is None:
break
codename = a.groups()[0]
codeid = a.groups()[1]
trop_jeune = False
if self.view.has_key( (channel,codename,codeid) ):
if time.time()-self.view[(channel,codename,codeid)] < 300:
trop_jeune = True
if not trop_jeune:
link = self.codes.get(codename,codeid)
if not link is None:
self.msg(channel,'[%s %s]: %s' % (codename, codeid, link))
self.view[(channel,codename,codeid)] = time.time()
m= re.sub('\[([A-Za-z0-9]+) ([^ \]]+)\]','',m,1)
def action(self, user, channel, msg):
"""This will get called when the bot sees someone do an action."""
user = user.split('!', 1)[0]
self.logger.log("* %s %s" % (user, msg))
def irc_NICK(self, prefix, params):
"""Called when an IRC user changes their nickname."""
old_nick = prefix.split('!')[0]
new_nick = params[0]
self.logger.log("%s is now known as %s" % (old_nick, new_nick))
class MmBotFactory(protocol.ClientFactory):
def __init__(self):
self.channel = "#legitest"
def buildProtocol(self, addr):
p = MmBot()
p.factory = self
return p
def clientConnectionLost(self, connector, reason):
connector.connect()
def clientConnectionFailed(self, connector, reason):
print "connection failed:", reason
reactor.stop()
if __name__ == '__main__':
log.startLogging(sys.stdout)
f = MmBotFactory()
reactor.connectTCP("irc.crans.org", 6667, f)
reactor.run()
expire = 2592000
[codeids]
CP = LEGITEXT000006070719
CPP = LEGITEXT000006071154
CC = LEGITEXT000006070721
CPC = LEGITEXT000006070716
CR = LEGITEXT000006071367
CConso = LEGITEXT000006069565
CD = LEGITEXT000006071307
CGI = LEGITEXT000006069577
CMF = LEGITEXT000006072026
CPCE = LEGITEXT000006070987
import requests
import re
import time
import configobj
def get_code(codename,codeids):
if not codeids.has_key(codename):
return None
codeid = codeids[codename]
r=requests.get('https://www.legifrance.gouv.fr/affichCode.do?cidTexte='+codeid)
reg = '^.*?href="(affichCode\.do[^"]*idSectionTA[^"]*)"'
c=r.content
links=set()
while True:
a = re.match(reg,c,re.DOTALL)
if a is None:
break
l1 = 'https://www.legifrance.gouv.fr/'+a.groups()[0]
l1 = re.sub('&amp;','&',l1)
links.add(l1)
c = re.sub('href=','',c,1)
articles = {}
for link in links:
r = requests.get(link)
reg = '^.*?<a href="(affichCodeArticle\.do[^"]*idArticle[^"]*)" title="En savoir plus sur l\'article ([^"]+)"'
c=r.content
while True:
a = re.match(reg, c, re.DOTALL)
if a is None:
break
l1 = 'https://www.legifrance.gouv.fr/'+a.groups()[0]
l1 = re.sub('&amp;','&',l1)
l1 = re.sub(';jsessionid=[^\?]*\?','?',l1)
l1 = re.sub('&dateTexte=[^&]*','',l1)
articles[a.groups()[1]] = l1
c = re.sub('href=','',c,1)
return articles
class codes:
def __init__(self,conffile):
self.codes = {}
self.conf = configobj.ConfigObj(conffile)
def get(self,codename,codearticle):
t = time.time()
if self.codes.has_key(codename):
if t - self.codes[codename]['timestamp'] > self.conf['expire']:
del self.codes[codename]
if self.codes.has_key(codename):
pass
else:
articles = get_code(codename,self.conf['codeids'])
if not articles is None:
self.codes[codename] = {'timestamp':t, 'articles':articles}
if self.codes.has_key(codename):
if self.codes[codename]['articles'].has_key(codearticle):
return self.codes[codename]['articles'][codearticle]
return None
def force_code_reload(self,codename):
if self.codes.has_key(codename):
self.codes[codename]['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