1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
info = {
'author': 'Jon Bergli Heier',
'title': 'Spotify metadata',
'description': 'Fetches metadata from spotify links.',
}
import re, spotimeta
class Module:
re_spotify = re.compile(r'(?:http://open.spotify.com/|spotify:)(?:track|artist|album)[:/][\w]{22}')
metadata = spotimeta.Metadata(cache = {})
def __init__(self, bot):
self.irc = bot
if bot:
self.irc.register_callback(self)
def spotify(self, s):
try:
data = self.metadata.lookup(s)
except:
return 'Failed to fetch metadata from spotify.'
if data['type'] == 'artist':
return 'Spotify artist: %s' % data['result']['name']
elif data['type'] == 'track':
return 'Spotify track: %s - %s (%s)' % (data['result']['artist']['name'], data['result']['name'], data['result']['album']['name'])
elif data['type'] == 'album':
return u'Spotify album: %s (%d) by %s' % (data['result']['name'], data['result']['released'],
', '.join([x['name'] for x in data['result']['artists']]))
def get_spotify_data(self, msg):
titles = []
spotify_links = self.re_spotify.findall(msg)
for sp in spotify_links:
s = self.spotify(sp)
if s:
titles.append(s.encode('utf8'))
if len(titles) == 0:
return
elif len(titles) == 1:
s = titles[0]
else:
s = ''
for i in range(len(titles)):
s += '\002[%d]\002 %s ' % (i+1, titles[i])
return s
def privmsg(self, nick, channel, msg):
titles = self.get_spotify_data(msg)
if titles:
self.irc.msg(channel if not channel == self.irc.nickname else nick.split('!')[0], titles)
if __name__ == '__main__':
import sys
s = Module(None).get_spotify_data(' '.join(sys.argv[1:]))
if s:
print s
|