]> jfr.im git - erebus.git/commitdiff
Combined spotify/twitch/youtube to urls
authorzonidjan <redacted>
Thu, 6 Feb 2014 16:06:56 +0000 (10:06 -0600)
committerzonidjan <redacted>
Thu, 6 Feb 2014 16:06:56 +0000 (10:06 -0600)
Still need to re-do twitch checker.

Also want to add a generic <title> grabber.

bot.py
modules/spotify.py [deleted file]
modules/twitch.py [deleted file]
modules/urls.py [new file with mode: 0644]
modules/youtube.py [deleted file]

diff --git a/bot.py b/bot.py
index ed8112b35178521e529cb8fe52c63f7299b1dfa5..10be7f6a4908207091535cc1d05f4021c53eddd5 100644 (file)
--- a/bot.py
+++ b/bot.py
@@ -116,6 +116,8 @@ class Bot(object):
                                                self.msg(user, "Command not implemented.")
 
        def msg(self, target, msg):
+               if target is None or msg is None: return
+
                if isinstance(target, self.parent.User): self.conn.send("NOTICE %s :%s" % (target.nick, msg))
                elif isinstance(target, self.parent.Channel): self.conn.send("PRIVMSG %s :%s" % (target.name, msg))
                elif isinstance(target, basestring):
diff --git a/modules/spotify.py b/modules/spotify.py
deleted file mode 100644 (file)
index 9ee323e..0000000
+++ /dev/null
@@ -1,93 +0,0 @@
-# Erebus IRC bot - Author: Erebus Team
-# Spotify URL Checker
-# This file is released into the public domain; see http://unlicense.org/
-
-# module info
-modinfo = {
-       'author': 'Erebus Team',
-       'license': 'public domain',
-       'compatible': [1], # compatible module API versions
-       'depends': [], # other modules required to work properly?
-}
-
-# preamble
-import modlib
-lib = modlib.modlib(__name__)
-modstart = lib.modstart
-modstop = lib.modstop
-
-# module code
-import re
-import urllib2
-from BeautifulSoup import BeautifulSoup
-
-checkfor = "spotify"
-hostmask_regex = re.compile('^(.*)!(.*)@(.*)$')
-spotify_regex = ( re.compile(r'spotify:(?P<type>\w+):(?P<track_id>\w{22})'),
-                                                                       re.compile(r'http://open.spotify.com/(?P<type>\w+)/(?P<track_id>\w{22})') )
-spotify_gateway = 'http://ws.spotify.com/lookup/1/'
-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
-       }
-
-@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:
-               linetx = ''
-
-       if checkfor not in line:
-               return # doesn't concern us
-
-       for r in spotify_regex:
-               for type, track in r.findall(linetx):
-                       url = '%s?uri=spotify:%s:%s' % (spotify_gateway, type, track)
-                       xml = urllib2.urlopen(url).read()
-                       soup = BeautifulSoup(xml)
-                       lookup_type = soup.contents[2].name
-
-                       if lookup_type == 'track':
-                               name = soup.find('name').string
-                               album_name = soup.find('album').find('name').string
-                               artist_name = soup.find('artist').find('name').string
-                               popularity = soup.find('popularity')
-                               if popularity:
-                                       popularity = float(popularity.string)*100
-                               length = float(soup.find('length').string)
-                               minutes = int(length)/60
-                               seconds =  int(length)%60
-
-                               bot.msg(line.split()[2], 'Track: %s - %s / %s %s:%.2d %2d%%' %(artist_name, name,
-                                                               album_name, minutes, seconds, popularity))
-
-                       elif lookup_type == 'album':
-                               album_name = soup.find('album').find('name').string
-                               artist_name = soup.find('artist').find('name').string
-                               released = soup.find('released').string
-                               bot.msg(line.split()[2], 'Album: %s - %s - %s' %(artist_name, album_name, released))
-
-                       else:
-                               bot.notice(sender['nick'], "Unsupported type.")
diff --git a/modules/twitch.py b/modules/twitch.py
deleted file mode 100644 (file)
index ba7e81f..0000000
+++ /dev/null
@@ -1,45 +0,0 @@
-# Erebus IRC bot - Author: Erebus Team
-# Twitch URL Checker
-# This file is released into the public domain; see http://unlicense.org/
-
-# module info
-modinfo = {
-       'author': 'Erebus Team',
-       'license': 'public domain',
-       'compatible': [1], # compatible module API versions
-       'depends': [], # other modules required to work properly?
-}
-
-# preamble
-import modlib
-lib = modlib.modlib(__name__)
-modstart = lib.modstart
-modstop = lib.modstop
-
-# module code
-import re
-import urllib2
-import json
-
-checkfor = "twitch"
-url_regex = re.compile('(http|ftp|https):\/\/([\w\-_]+(?:(?:\.[\w\-_]+)+))([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])?')
-
-@lib.hooknum("PRIVMSG")
-def privmsg_hook(bot, line):
-       try:
-               linetx = line.split(None, 3)[3][1:]
-       except IndexError:
-               linetx = ''
-
-       if checkfor not in line:
-               return # doesn't concern us
-
-       for p, h, c in url_regex.findall(linetx):
-               if checkfor in h:
-                       url = 'http://api.justin.tv/api/stream/list.json?channel=%s' % c[1:]
-                       respdata = urllib2.urlopen(url).read()
-                       twitch = json.loads(respdata)
-                       try:
-                               bot.msg(line.split()[2], 'Twitch: %s (%s playing %s)' % (twitch[0]['channel']['status'], twitch[0]['channel']['login'], twitch[0]['channel']['meta_game']))
-                       except:
-                               bot.msg(line.split()[2], 'Twitch: Channel offline.')
diff --git a/modules/urls.py b/modules/urls.py
new file mode 100644 (file)
index 0000000..9e8e3e2
--- /dev/null
@@ -0,0 +1,151 @@
+# Erebus IRC bot - Author: Erebus Team
+# URL Checker
+# This file is released into the public domain; see http://unlicense.org/
+
+# module info
+modinfo = {
+       'author': 'Erebus Team',
+       'license': 'public domain',
+       'compatible': [1], # compatible module API versions
+       'depends': [], # other modules required to work properly?
+}
+
+# preamble
+import modlib
+lib = modlib.modlib(__name__)
+modstart = lib.modstart
+modstop = lib.modstop
+
+# module code
+import re, json, urllib2, urlparse, HTMLParser
+from BeautifulSoup import BeautifulSoup
+
+hostmask_regex = re.compile(r'^(.*)!(.*)@(.*)$')
+
+spotify_regex = (
+       re.compile(r'spotify:(?P<type>\w+):(?P<track_id>\w{22})'),
+       re.compile(r'http://open.spotify.com/(?P<type>\w+)/(?P<track_id>\w{22})')
+)
+youtube_regex = (
+       re.compile(r'https?://(?:www\.)?youtube\.com/watch\?[a-zA-Z0-9=&_\-]+'),
+)
+twitch_regex = (
+       re.compile('(http|ftp|https):\/\/([\w\-_]+(?:(?:\.[\w\-_]+)+))([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])?'), #TODO
+)
+
+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
+       }
+
+@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:
+               linetx = ''
+
+       chan = line.split()[2]
+
+       if 'open.spotify.com' in line or 'spotify:' in line:
+               for r in spotify_regex:
+                       for sptype, track in r.findall(linetx):
+                               bot.msg(chan, gotspotify(sptype, track))
+
+       elif 'youtube.com' in line or 'youtu.be' in line:
+               print "got youtube!"
+               for r in youtube_regex:
+                       for url in r.findall(linetx):
+                               bot.msg(chan, gotyoutube(url))
+
+       elif 'twitch.tv' in line: pass #TODO fix twitch
+
+       else: pass #TODO generic <title> checker
+
+
+def gotspotify(type, track):
+       url = 'http://ws.spotify.com/lookup/1/?uri=spotify:%s:%s' % (type, track)
+       xml = urllib2.urlopen(url).read()
+       soup = BeautifulSoup(xml)
+       lookup_type = soup.contents[2].name
+               
+       if lookup_type == 'track':
+               name = soup.find('name').string
+               album_name = soup.find('album').find('name').string
+               artist_name = soup.find('artist').find('name').string
+               popularity = soup.find('popularity')
+               if popularity:
+                       popularity = float(popularity.string)*100
+               length = float(soup.find('length').string)
+               minutes = int(length)/60
+               seconds =  int(length)%60
+       
+               return 'Track: %s - %s / %s %s:%.2d %2d%%' % (artist_name, name, album_name, minutes, seconds, popularity)
+       
+       elif lookup_type == 'album':
+               album_name = soup.find('album').find('name').string
+               artist_name = soup.find('artist').find('name').string
+               released = soup.find('released').string
+               return 'Album: %s - %s - %s' % (artist_name, album_name, released)
+       
+       else:
+               return 'Unsupported type.'
+
+def gotyoutube(url):
+       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:
+               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']
+
+               return "Youtube: %s (%s)" % (title, author)
+       except:
+               pass
+
+
+def gottwitch(url):
+       return ""
+       #FIXME:
+       try:
+               linetx = line.split(None, 3)[3][1:]
+       except IndexError:
+               linetx = ''
+
+       if checkfor not in line:
+               return # doesn't concern us
+
+       for p, h, c in url_regex.findall(linetx):
+               if checkfor in h:
+                       url = 'http://api.justin.tv/api/stream/list.json?channel=%s' % c[1:]
+                       respdata = urllib2.urlopen(url).read()
+                       twitch = json.loads(respdata)
+                       try:
+                               bot.msg(line.split()[2], 'Twitch: %s (%s playing %s)' % (twitch[0]['channel']['status'], twitch[0]['channel']['login'], twitch[0]['channel']['meta_game']))
+                       except:
+                               bot.msg(line.split()[2], 'Twitch: Channel offline.')
diff --git a/modules/youtube.py b/modules/youtube.py
deleted file mode 100644 (file)
index 538c91e..0000000
+++ /dev/null
@@ -1,54 +0,0 @@
-# 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': 'Erebus Team',
-       'license': 'public domain',
-       'compatible': [1], # compatible module API versions
-       'depends': [], # other modules required to work properly?
-}
-
-# preamble
-import modlib
-lib = modlib.modlib(__name__)
-modstart = lib.modstart
-modstop = lib.modstop
-
-# module code
-import re
-import json
-import urllib2
-import urlparse
-import HTMLParser
-from BeautifulSoup import BeautifulSoup
-
-checkfor = "youtube"
-yturl_regex = re.compile(r'https?://(?:www\.)?youtube\.com/watch\?[a-zA-Z0-9=&_\-]+')
-
-@lib.hooknum("PRIVMSG")
-def privmsg_hook(bot, line):
-       try:
-               linetx = line.split(None, 3)[3][1:]
-       except IndexError:
-               linetx = ''
-
-       if checkfor not in line:
-               return # doesn't concern us
-
-       for url in yturl_regex.findall(linetx):
-               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:
-                       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