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())) 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" % (unicode(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)