From efa16f5f644458ebfd6f2b68f8e3e34d53476230 Mon Sep 17 00:00:00 2001 From: Stefan Krulj Date: Sat, 20 Oct 2012 12:08:31 +0200 Subject: [PATCH] Fixed two bugs --- modules/active/youtube.py | 49 +++++++++++++++++++++------------------ sys/persistent.py | 7 ++++-- 2 files changed, 32 insertions(+), 24 deletions(-) diff --git a/modules/active/youtube.py b/modules/active/youtube.py index 274f2e9..b743c06 100644 --- a/modules/active/youtube.py +++ b/modules/active/youtube.py @@ -18,28 +18,33 @@ def youtube_logger(mfrom,msg): if brathuhn.room_nick in unicode(mfrom): return groups=re.findall(URL,unicode(msg.getBody())) - if groups: - for g in groups: - url = g[0] - addr = g[2] - protocol = g[1] - file = g[4] - if addr in ['youtube.com','www.youtube.com','youtu.be']: - yt_service = gdata.youtube.service.YouTubeService() - id = get_id_from_url(url) - print "youtube link",id - entry = yt_service.GetYouTubeVideoEntry(video_id=id) - sec = int(entry.media.duration.seconds) - duration = "%02d:%02d:%02d" % (sec/3600, (sec%3600)/60, (sec%3600)%60) - txt = "%s posted a youtube video: %s\nTitle: %s\nCategory: %s\nTags: %s\nDuration: %s\nViews: %s\nRating: %s" % (mfrom.getResource(), - entry.media.player.url, - entry.media.title.text, - entry.media.category[0].text, - entry.media.keywords.text, - duration, - entry.statistics.view_count, - entry.rating.average) - brathuhn.sendMsg(brathuhn.ROOM,txt,'groupchat') + + tmp = {} + for g in groups: + url = g[0] + addr = g[2] + protocol = g[1] + file = g[4] + if protocol in ['http','https'] and addr in ['youtube.com','www.youtube.com','youtu.be']: + yt_service = gdata.youtube.service.YouTubeService() + id = get_id_from_url(url) + # prevent doubles + if id in tmp: + continue + tmp[id]=True + print "youtube link",id + entry = yt_service.GetYouTubeVideoEntry(video_id=id) + sec = int(entry.media.duration.seconds) + duration = "%02d:%02d:%02d" % (sec/3600, (sec%3600)/60, (sec%3600)%60) + txt = "%s posted a youtube video: %s\nTitle: %s\nCategory: %s\nTags: %s\nDuration: %s\nViews: %s\nRating: %s" % (mfrom.getResource(), + entry.media.player.url, + entry.media.title.text, + entry.media.category[0].text, + entry.media.keywords.text, + duration, + entry.statistics.view_count, + entry.rating.average) + brathuhn.sendMsg(brathuhn.ROOM,txt,'groupchat') brathuhn.addGroupListener(youtube_logger) diff --git a/sys/persistent.py b/sys/persistent.py index 139b32f..247cb5f 100644 --- a/sys/persistent.py +++ b/sys/persistent.py @@ -1,5 +1,6 @@ from ConfigParser import ConfigParser import cPickle as pickle +import base64 from threading import Lock #TODO replace ConfigParser for better performance @@ -41,7 +42,8 @@ class PersistentContainer(object): def __getitem__(self, name): if self.cp.has_option(self.namespace,name): - return pickle.loads(self.cp.get(self.namespace,name)) + b64 = self.cp.get(self.namespace,name) + return pickle.loads(base64.b64decode(b64)) return None def __setitem__(self, name,val): @@ -64,7 +66,8 @@ class PersistentContainer(object): def _write(self): for k in self.trans_cache: val = self.trans_cache[k] - self.cp.set(self.namespace,k,pickle.dumps(val)) + ser = pickle.dumps(val) + self.cp.set(self.namespace,k,base64.b64encode(ser)) while len(self.trans_del): n = self.trans_del.pop() self.cp.remove_option(self.namespace,n)