class GetInfo def __init__ self self artist self album def lastfm_pars

 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
60
61
62
63
64
65
66
67
68
69
class GetInfo:
def __init__(self):
self.artist = ''
self.album = ''
def lastfm_parser(self, api_key = 'b25b959554ed76058ac220b7b2e0a026', method = 'album.getinfo', format = ''):
url = 'http://ws.audioscrobbler.com/2.0/'
data = {}
if self.album:
data['album'] = self.album
if self.artist:
data['artist'] = self.artist
data['method'] = method
data['api_key'] = api_key
if format == 'json':
data['format'] = 'json'
post = urllib.urlencode(data)
parsed = urllib2.urlopen(url +'?'+ post).read()
if format == 'json':
return json.loads(parsed)
return minidom.parseString(parsed)
def get_cover_url(self):
if self.album and self.artist:
parsed = self.lastfm_parser()
covers_urls = parsed.getElementsByTagName('image')
if covers_urls:
try:
artwork = covers_urls[-1].childNodes[0].data
return artwork
except:
return ''
def get_artist_cover_url(self):
parsed = self.lastfm_parser(method = 'artist.getimages', format = 'json')
highest_raiting = 0
url = ''
if len(parsed) == 1:
for image in parsed['images']['image']:
if int(image['votes']['thumbsup']) and int(image['votes']['thumbsdown']):
raiting = float(image['votes']['thumbsup']) / float(image['votes']['thumbsdown'])
if raiting > highest_raiting:
url = image['sizes']['size'][0]['#text'], image['sizes']['size'][2]['#text']
highest_raiting = raiting
if image and not url:
url = image['sizes']['size'][0]['#text'], image['sizes']['size'][2]['#text']
if url:
return url
return ''
def get_similar_artist(self):
parsed = self.lastfm_parser(method = 'artist.getsimilar')
similar_artists = parsed.getElementsByTagName('name')
if similar_artists:
artist_list = []
for artist in similar_artists:
artist_list.append(artist.childNodes[0].data)
return artist_list[:7]
def get_tags_lastfm(self):
parsed = self.lastfm_parser()
tags_list_xml = parsed.getElementsByTagName('toptags')
if tags_list_xml:
tags_xml = tags_list_xml[0].getElementsByTagName('tag')
tags_list = []
if tags_xml:
for tag in tags_xml:
tags_list.append(tag.childNodes[1].childNodes[0].data)
return ', '.join(tags_list)