]>
Commit | Line | Data |
---|---|---|
381f9fc4 | 1 | # Erebus IRC bot - Author: Conny Sjoblom |
24883fd6 CS |
2 | # Spotify URL Checker |
3 | # This file is released into the public domain; see http://unlicense.org/ | |
4 | ||
5 | # module info | |
6 | modinfo = { | |
381f9fc4 | 7 | 'author': 'Conny Sjoblom', |
24883fd6 CS |
8 | 'license': 'public domain', |
9 | 'compatible': [1], # compatible module API versions | |
10 | 'depends': [], # other modules required to work properly? | |
11 | } | |
12 | ||
13 | # preamble | |
14 | import modlib | |
15 | lib = modlib.modlib(__name__) | |
16 | modstart = lib.modstart | |
17 | modstop = lib.modstop | |
18 | ||
19 | # module code | |
20 | import re | |
24883fd6 CS |
21 | import urllib2 |
22 | from BeautifulSoup import BeautifulSoup | |
23 | ||
c8f91e30 | 24 | checkfor = "spotify" |
24883fd6 CS |
25 | hostmask_regex = re.compile('^(.*)!(.*)@(.*)$') |
26 | spotify_regex = ( 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 | spotify_gateway = 'http://ws.spotify.com/lookup/1/' | |
29 | def parser_hostmask(hostmask): | |
30 | if isinstance(hostmask, dict): | |
31 | return hostmask | |
32 | ||
33 | nick = None | |
34 | user = None | |
35 | host = None | |
36 | ||
37 | if hostmask is not None: | |
38 | match = hostmask_regex.match(hostmask) | |
39 | ||
40 | if not match: | |
41 | nick = hostmask | |
42 | else: | |
43 | nick = match.group(1) | |
44 | user = match.group(2) | |
45 | host = match.group(3) | |
46 | ||
47 | return { | |
48 | 'nick': nick, | |
49 | 'user': user, | |
50 | 'host': host | |
51 | } | |
52 | ||
53 | @lib.hooknum("PRIVMSG") | |
54 | def privmsg_hook(bot, line): | |
55 | sender = parser_hostmask(line[1:line.find(' ')]) | |
56 | ||
57 | try: | |
c8f91e30 | 58 | linetx = line.split(None, 3)[3][1:] |
24883fd6 CS |
59 | except IndexError: |
60 | linetx = '' | |
61 | ||
c8f91e30 | 62 | if checkfor not in line: |
63 | return # doesn't concern us | |
64 | ||
24883fd6 CS |
65 | for r in spotify_regex: |
66 | for type, track in r.findall(linetx): | |
c8f91e30 | 67 | url = '%s?uri=spotify:%s:%s' % (spotify_gateway, type, track) |
24883fd6 CS |
68 | xml = urllib2.urlopen(url).read() |
69 | soup = BeautifulSoup(xml) | |
70 | lookup_type = soup.contents[2].name | |
71 | ||
72 | if lookup_type == 'track': | |
73 | name = soup.find('name').string | |
74 | album_name = soup.find('album').find('name').string | |
75 | artist_name = soup.find('artist').find('name').string | |
76 | popularity = soup.find('popularity') | |
77 | if popularity: | |
78 | popularity = float(popularity.string)*100 | |
79 | length = float(soup.find('length').string) | |
80 | minutes = int(length)/60 | |
81 | seconds = int(length)%60 | |
82 | ||
83 | bot.msg(line.split()[2], 'Track: %s - %s / %s %s:%.2d %2d%%' %(artist_name, name, | |
84 | album_name, minutes, seconds, popularity)) | |
85 | ||
86 | elif lookup_type == 'album': | |
87 | album_name = soup.find('album').find('name').string | |
88 | artist_name = soup.find('artist').find('name').string | |
89 | released = soup.find('released').string | |
90 | bot.msg(line.split()[2], 'Album: %s - %s - %s' %(artist_name, album_name, released)) | |
91 | ||
92 | else: | |
93 | bot.notice(sender['nick'], "Unsupported type.") |