]> jfr.im git - erebus.git/blobdiff - modules/youtube.py
Merge branch 'master' of kronos.jfr.im:erebus
[erebus.git] / modules / youtube.py
index a0c9c9d5f52fef26be9093b6b20d5321e7553f5a..c40bf177a6ee32b8e6f6ac92db307c321b202cff 100644 (file)
@@ -1,10 +1,10 @@
-# Erebus IRC bot - Author: Conny Sjoblom
-# Spotify URL Checker
+# Erebus IRC bot - Author: Erebus Team
+# Youtube URL Checker
 # This file is released into the public domain; see http://unlicense.org/
 
 # module info
 modinfo = {
-       'author': 'Conny Sjoblom',
+       'author': 'Erebus Team',
        'license': 'public domain',
        'compatible': [1], # compatible module API versions
        'depends': [], # other modules required to work properly?
@@ -18,41 +18,18 @@ modstop = lib.modstop
 
 # module code
 import re
+import json
 import urllib2
+import urlparse
 import HTMLParser
 from BeautifulSoup import BeautifulSoup
 
 checkfor = "youtube"
-hostmask_regex = re.compile('^(.*)!(.*)@(.*)$')
 url_regex = re.compile('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+')
-def parser_hostmask(hostmask):
-       if isinstance(hostmask, dict):
-               return hostmask
-
-       nick = None
-       user = None
-       host = None
-
-       if hostmask is not None:
-               match = hostmask_regex.match(hostmask)
-
-               if not match:
-                       nick = hostmask
-               else:
-                       nick = match.group(1)
-                       user = match.group(2)
-                       host = match.group(3)
-
-       return {
-               'nick': nick,
-               'user': user,
-               'host': host
-       }
+yturl_regex = re.compile('(http|ftp|https):\/\/([\w\-_]+(?:(?:\.[\w\-_]+)+))([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?')
 
 @lib.hooknum("PRIVMSG")
 def privmsg_hook(bot, line):
-       sender = parser_hostmask(line[1:line.find(' ')])
-
        try:
                linetx = line.split(None, 3)[3][1:]
        except IndexError:
@@ -63,10 +40,17 @@ def privmsg_hook(bot, line):
 
        for url in url_regex.findall(linetx):
                if checkfor in url:
-                       html_parser = HTMLParser.HTMLParser()
-                       respdata = urllib2.urlopen(url).read()
-                       soup = BeautifulSoup(respdata)
+                       url_data = urlparse.urlparse(url)
+                       query = urlparse.parse_qs(url_data.query)
+                       video = query["v"][0]
+                       api_url = 'http://gdata.youtube.com/feeds/api/videos/%s?alt=json&v=2' % video
                        try:
-                               bot.msg(line.split()[2], BeautifulSoup(soup.title.string, convertEntities=BeautifulSoup.HTML_ENTITIES))
+                               respdata = urllib2.urlopen(api_url).read()
+                               video_info = json.loads(respdata)
+
+                               title = video_info['entry']['title']["$t"]
+                               author = video_info['entry']['author'][0]['name']['$t']
+
+                               bot.msg(line.split()[2], "Youtube: %s (%s)" % (title, author))
                        except:
                                pass