From d0e4da7f4546e6abd26f9e0da520f9fe52049129 Mon Sep 17 00:00:00 2001 From: Stefan Krulj Date: Fri, 19 Oct 2012 22:53:35 +0200 Subject: [PATCH] Youtube video analyzer added --- modules/active/youtube.py | 47 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 modules/active/youtube.py diff --git a/modules/active/youtube.py b/modules/active/youtube.py new file mode 100644 index 0000000..274f2e9 --- /dev/null +++ b/modules/active/youtube.py @@ -0,0 +1,47 @@ +import re, brathuhn +import gdata.youtube.service + +URL = r'((\w+)://((\w+\.)+\w+)([a-zA-Z0-9\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\/\?\.\:\;\"\,]*))' +ID=r'[\w_-]'*11 +ID_URL=r'[=/]('+ID+r')[/&]|[=/]('+ID+r')$' + +def get_id_from_url(url): + groups=re.findall(ID_URL,url) + if groups: + for g in groups: + id=g[0] + if id=='': + id=g[1] + return id + +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') +brathuhn.addGroupListener(youtube_logger) + + + +