forked from phil65/script.extendedinfo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lastfm.py
30 lines (28 loc) · 1.08 KB
/
Lastfm.py
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
import xml.dom.minidom
from Utils import log, GetStringFromUrl, GetValue
lastfm_apikey = 'bb258101395ce46c63843bd6261e3fc8'
def GetSimilarById(m_id):
url = 'http://ws.audioscrobbler.com/2.0/?method=artist.getsimilar&mbid=%s&api_key=%s' % (m_id, lastfm_apikey)
ret = GetStringFromUrl(url)
curXML = xml.dom.minidom.parseString(ret)
curXMLs = curXML.getElementsByTagName('lfm')
if len(curXMLs) > 0:
curXML = curXMLs[0]
else:
log('No <lfm> found - printing retrieved xml:')
print ret
return None
curXMLs = curXML.getElementsByTagName('similarartists')
if len(curXMLs) > 0:
curXML = curXMLs[0]
else:
log('No <similarartists> found - printing retrieved xml:')
print ret
return None
artistXMLs = curXML.getElementsByTagName('artist')
similars = []
for artistXML in artistXMLs:
artist = {"name": GetValue(artistXML, 'name'), "mbid": GetValue(artistXML, 'mbid')}
similars.append(artist)
log('Found %i Similar artists in last.FM' % len(similars))
return similars