]>
jfr.im git - erebus.git/blob - modules/spotify.py
1 # Erebus IRC bot - Author: John Runyon
3 # This file is released into the public domain; see http://unlicense.org/
7 'author': 'Conny Sjoblom (BiohZn)',
8 'license': 'public domain',
9 'compatible': [1], # compatible module API versions
10 'depends': [], # other modules required to work properly?
15 lib
= modlib
.modlib(__name__
)
16 modstart
= lib
.modstart
23 from BeautifulSoup
import BeautifulSoup
26 hostmask_regex
= re
.compile('^(.*)!(.*)@(.*)$')
27 spotify_regex
= ( re
.compile(r
'spotify:(?P<type>\w+):(?P<track_id>\w{22})'),
28 re
.compile(r
'http://open.spotify.com/(?P<type>\w+)/(?P<track_id>\w{22})') )
29 spotify_gateway
= 'http://ws.spotify.com/lookup/1/'
30 def parser_hostmask(hostmask
):
31 if isinstance(hostmask
, dict):
38 if hostmask
is not None:
39 match
= hostmask_regex
.match(hostmask
)
54 @lib.hooknum("PRIVMSG")
55 def privmsg_hook(bot
, line
):
56 sender
= parser_hostmask(line
[1:line
.find(' ')])
59 linetx
= line
.split(None, 3)[3][1:]
63 if checkfor
not in line
:
64 return # doesn't concern us
66 for r
in spotify_regex
:
67 for type, track
in r
.findall(linetx
):
68 url
= '%s?uri=spotify:%s:%s' % (spotify_gateway
, type, track
)
69 xml
= urllib2
.urlopen(url
).read()
70 soup
= BeautifulSoup(xml
)
71 lookup_type
= soup
.contents
[2].name
73 if lookup_type
== 'track':
74 name
= soup
.find('name').string
75 album_name
= soup
.find('album').find('name').string
76 artist_name
= soup
.find('artist').find('name').string
77 popularity
= soup
.find('popularity')
79 popularity
= float(popularity
.string
)*100
80 length
= float(soup
.find('length').string
)
81 minutes
= int(length
)/60
82 seconds
= int(length
)%60
84 bot
.msg(line
.split()[2], 'Track: %s - %s / %s %s:%.2d %2d%%' %(artist_name
, name
,
85 album_name
, minutes
, seconds
, popularity
))
87 elif lookup_type
== 'album':
88 album_name
= soup
.find('album').find('name').string
89 artist_name
= soup
.find('artist').find('name').string
90 released
= soup
.find('released').string
91 bot
.msg(line
.split()[2], 'Album: %s - %s - %s' %(artist_name
, album_name
, released
))
94 bot
.notice(sender
['nick'], "Unsupported type.")