1
0
Fork 0

Add a connection with musik.p0m.fr

Fix parse error when parsing dates in 42.xml
Increase stability with try/expect
This commit is contained in:
Némunaire 2012-04-09 14:11:03 +02:00
parent ada5b0eeeb
commit 448b455e77
6 changed files with 78 additions and 6 deletions

13
atom.py
View File

@ -10,9 +10,20 @@ class AtomEntry:
def __init__ (self, node):
self.id = node.getElementsByTagName("id")[0].firstChild.nodeValue
self.title = node.getElementsByTagName("title")[0].firstChild.nodeValue
self.updated = time.strptime(node.getElementsByTagName("updated")[0].firstChild.nodeValue[:19], "%Y-%m-%dT%H:%M:%S")
try:
self.updated = time.strptime(node.getElementsByTagName("updated")[0].firstChild.nodeValue[:19], "%Y-%m-%dT%H:%M:%S")
except:
try:
self.updated = time.strptime(node.getElementsByTagName("updated")[0].firstChild.nodeValue[:10], "%Y-%m-%d")
except:
print (node.getElementsByTagName("updated")[0].firstChild.nodeValue[:10])
self.updated = time.localtime ()
self.summary = node.getElementsByTagName("summary")[0].firstChild.nodeValue
self.link = node.getElementsByTagName("link")[0].getAttribute ("href")
if len (node.getElementsByTagName("link")) > 1:
self.link2 = node.getElementsByTagName("link")[1].getAttribute ("href")
else:
self.link2 = None
class Atom:
def __init__ (self, string):

View File

@ -10,7 +10,7 @@ from datetime import datetime
from datetime import timedelta
from xml.dom.minidom import parse
imports = ["birthday", "qd", "events"]
imports = ["birthday", "qd", "events", "youtube"]
imports_launch = ["watchWebsite"]
mods = list ()
import server

4
qd.py
View File

@ -32,8 +32,8 @@ def xmlparse(node):
manche.getAttribute("winner"),
int(manche.getAttribute("winner_score")),
manche.getAttribute("who"),
datetime.now ())
# time.strptime (manche.getAttribute("date")))
# datetime.now ())
datetime.fromtimestamp (time.mktime (time.strptime (manche.getAttribute("date")[:19], "%Y-%m-%d %H:%M:%S"))))
def load_module(datas_path):
"""Load this module"""

View File

@ -46,13 +46,20 @@ class Server:
def read(self, mods):
self.readbuffer = "" #Here we store all the messages from server
while 1:
self.readbuffer = self.readbuffer + self.s.recv(1024).decode() #recieve server messages
try:
self.readbuffer = self.readbuffer + self.s.recv(1024).decode() #recieve server messages
except UnicodeDecodeError:
print ("ERREUR de décodage unicode")
continue
temp = self.readbuffer.split("\n")
self.readbuffer = temp.pop( )
for line in temp:
msg = message.Message (self, line)
msg.treat (mods)
try:
msg.treat (mods)
except:
print ("Une erreur est survenue lors du traitement du message : %s"%line)
def connect(self, mods):

View File

@ -10,6 +10,7 @@ from xml.dom.minidom import parseString
from xml.dom.minidom import getDOMImplementation
import atom
import youtube
filename = ""
SITES = []
@ -67,6 +68,9 @@ def treat_atom (lastpage, content, message):
else:
messageI = message % ("quel est ce nouveau fichier", "%s")
send_global (messageI % unquote (d.link))
elif f.id == "http://musik.p0m.fr/atom.php?nemubot":
for d in diff:
youtube.send_global (d.link2, message % (d.title, unquote (d.link)))
elif message.find ("%s") >= 0:
for d in diff:
send_global (message % unquote (d.link))

50
youtube.py Normal file
View File

@ -0,0 +1,50 @@
# coding=utf-8
import re
import http.client
URLS = dict ()
def load_module(datas_path):
"""Load this module"""
global URLS
URLS = dict ()
def save_module():
"""Save the dates"""
return
def help_tiny ():
"""Line inserted in the response to the command !help"""
return
def help_full ():
return
def parseanswer(msg):
return False
def parseask(msg):
return False
def parselisten (msg):
global URLS
matches = [".*(http://(www\.)?youtube.com/watch\?v=([a-zA-Z0-9_-]{11})).*",
".*(http://(www\.)?youtu.be/([a-zA-Z0-9_-]{11})).*"]
for m in matches:
res = re.match (m, msg.content)
if res is not None:
#print ("seen : %s"%res.group(1))
URLS[res.group(1)] = msg
conn = http.client.HTTPConnection("musik.p0m.fr")
conn.request("GET", "/?nemubot&a=add&url=%s"%(res.group (1)))
conn.getresponse()
conn.close()
return True
return False
def send_global (origin, msg):
if origin in URLS:
URLS[origin].send_chn (msg)
del URLS[origin]