]> jfr.im git - erebus.git/blame - modules/urls.py
Combined spotify/twitch/youtube to urls
[erebus.git] / modules / urls.py
CommitLineData
a83e1f9c 1# Erebus IRC bot - Author: Erebus Team
2# URL Checker
3# This file is released into the public domain; see http://unlicense.org/
4
5# module info
6modinfo = {
7 'author': 'Erebus Team',
8 'license': 'public domain',
9 'compatible': [1], # compatible module API versions
10 'depends': [], # other modules required to work properly?
11}
12
13# preamble
14import modlib
15lib = modlib.modlib(__name__)
16modstart = lib.modstart
17modstop = lib.modstop
18
19# module code
20import re, json, urllib2, urlparse, HTMLParser
21from BeautifulSoup import BeautifulSoup
22
23hostmask_regex = re.compile(r'^(.*)!(.*)@(.*)$')
24
25spotify_regex = (
26 re.compile(r'spotify:(?P<type>\w+):(?P<track_id>\w{22})'),
27 re.compile(r'http://open.spotify.com/(?P<type>\w+)/(?P<track_id>\w{22})')
28)
29youtube_regex = (
30 re.compile(r'https?://(?:www\.)?youtube\.com/watch\?[a-zA-Z0-9=&_\-]+'),
31)
32twitch_regex = (
33 re.compile('(http|ftp|https):\/\/([\w\-_]+(?:(?:\.[\w\-_]+)+))([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])?'), #TODO
34)
35
36def parser_hostmask(hostmask):
37 if isinstance(hostmask, dict):
38 return hostmask
39
40 nick = None
41 user = None
42 host = None
43
44 if hostmask is not None:
45 match = hostmask_regex.match(hostmask)
46
47 if not match:
48 nick = hostmask
49 else:
50 nick = match.group(1)
51 user = match.group(2)
52 host = match.group(3)
53
54 return {
55 'nick': nick,
56 'user': user,
57 'host': host
58 }
59
60@lib.hooknum("PRIVMSG")
61def privmsg_hook(bot, line):
62 sender = parser_hostmask(line[1:line.find(' ')])
63
64 try:
65 linetx = line.split(None, 3)[3][1:]
66 except IndexError:
67 linetx = ''
68
69 chan = line.split()[2]
70
71 if 'open.spotify.com' in line or 'spotify:' in line:
72 for r in spotify_regex:
73 for sptype, track in r.findall(linetx):
74 bot.msg(chan, gotspotify(sptype, track))
75
76 elif 'youtube.com' in line or 'youtu.be' in line:
77 print "got youtube!"
78 for r in youtube_regex:
79 for url in r.findall(linetx):
80 bot.msg(chan, gotyoutube(url))
81
82 elif 'twitch.tv' in line: pass #TODO fix twitch
83
84 else: pass #TODO generic <title> checker
85
86
87def gotspotify(type, track):
88 url = 'http://ws.spotify.com/lookup/1/?uri=spotify:%s:%s' % (type, track)
89 xml = urllib2.urlopen(url).read()
90 soup = BeautifulSoup(xml)
91 lookup_type = soup.contents[2].name
92
93 if lookup_type == 'track':
94 name = soup.find('name').string
95 album_name = soup.find('album').find('name').string
96 artist_name = soup.find('artist').find('name').string
97 popularity = soup.find('popularity')
98 if popularity:
99 popularity = float(popularity.string)*100
100 length = float(soup.find('length').string)
101 minutes = int(length)/60
102 seconds = int(length)%60
103
104 return 'Track: %s - %s / %s %s:%.2d %2d%%' % (artist_name, name, album_name, minutes, seconds, popularity)
105
106 elif lookup_type == 'album':
107 album_name = soup.find('album').find('name').string
108 artist_name = soup.find('artist').find('name').string
109 released = soup.find('released').string
110 return 'Album: %s - %s - %s' % (artist_name, album_name, released)
111
112 else:
113 return 'Unsupported type.'
114
115def gotyoutube(url):
116 url_data = urlparse.urlparse(url)
117 query = urlparse.parse_qs(url_data.query)
118 video = query["v"][0]
119 api_url = 'http://gdata.youtube.com/feeds/api/videos/%s?alt=json&v=2' % video
120 try:
121 respdata = urllib2.urlopen(api_url).read()
122 video_info = json.loads(respdata)
123
124 title = video_info['entry']['title']["$t"]
125 author = video_info['entry']['author'][0]['name']['$t']
126
127 return "Youtube: %s (%s)" % (title, author)
128 except:
129 pass
130
131
132def gottwitch(url):
133 return ""
134 #FIXME:
135 try:
136 linetx = line.split(None, 3)[3][1:]
137 except IndexError:
138 linetx = ''
139
140 if checkfor not in line:
141 return # doesn't concern us
142
143 for p, h, c in url_regex.findall(linetx):
144 if checkfor in h:
145 url = 'http://api.justin.tv/api/stream/list.json?channel=%s' % c[1:]
146 respdata = urllib2.urlopen(url).read()
147 twitch = json.loads(respdata)
148 try:
149 bot.msg(line.split()[2], 'Twitch: %s (%s playing %s)' % (twitch[0]['channel']['status'], twitch[0]['channel']['login'], twitch[0]['channel']['meta_game']))
150 except:
151 bot.msg(line.split()[2], 'Twitch: Channel offline.')