]>
jfr.im git - erebus.git/blob - modules/spotify.py
1 # Erebus IRC bot - Author: Conny Sjoblom
3 # This file is released into the public domain; see http://unlicense.org/
7 'author': 'Conny Sjoblom',
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
22 from BeautifulSoup
import BeautifulSoup
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):
37 if hostmask
is not None:
38 match
= hostmask_regex
.match(hostmask
)
53 @lib.hooknum("PRIVMSG")
54 def privmsg_hook(bot
, line
):
55 sender
= parser_hostmask(line
[1:line
.find(' ')])
58 linetx
= line
.split(None, 3)[3][1:]
62 if checkfor
not in line
:
63 return # doesn't concern us
65 for r
in spotify_regex
:
66 for type, track
in r
.findall(linetx
):
67 url
= '%s?uri=spotify:%s:%s' % (spotify_gateway
, type, track
)
68 xml
= urllib2
.urlopen(url
).read()
69 soup
= BeautifulSoup(xml
)
70 lookup_type
= soup
.contents
[2].name
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')
78 popularity
= float(popularity
.string
)*100
79 length
= float(soup
.find('length').string
)
80 minutes
= int(length
)/60
81 seconds
= int(length
)%60
83 bot
.msg(line
.split()[2], 'Track: %s - %s / %s %s:%.2d %2d%%' %(artist_name
, name
,
84 album_name
, minutes
, seconds
, popularity
))
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
))
93 bot
.notice(sender
['nick'], "Unsupported type.")