gloss-mc/modules/music_player/lastFM_interface.py

81 lines
3.0 KiB
Python
Raw Normal View History

2008-03-06 04:22:03 -08:00
import urllib
2008-03-07 05:06:44 -08:00
import xml
2008-03-06 04:22:03 -08:00
from xml.dom import minidom
import pygtk
import gtk
import gobject
from itertools import izip, repeat, chain
2008-03-06 04:22:03 -08:00
class lastFM_interface:
lastFM_base_xml_uri = "http://ws.audioscrobbler.com/1.0/"
lastFM_artist_xml_uri = lastFM_base_xml_uri + "artist/"
lastFM_album_xml_uri = lastFM_base_xml_uri + "album/"
lastFM_track_xml_uri = lastFM_base_xml_uri + "track/"
def __init__(self):
pass
#Returns a pixbuf with an image of the specified artist
#Returns None if it is unable to get an image
def get_artist_image(self, artist):
artist_clean = artist.replace(" ", "+")
artist_clean = artist_clean.replace("/", "+")
2008-03-06 04:22:03 -08:00
similar_uri = self.lastFM_artist_xml_uri + artist_clean +"/similar.xml"
filehandle = urllib.urlopen(similar_uri)
#We only need the first 2 lines of this file
2008-03-07 05:06:44 -08:00
xml_string = ""
2008-03-06 04:22:03 -08:00
for x in range(2):
2008-03-07 05:06:44 -08:00
xml_string += filehandle.readline()
2008-03-06 04:22:03 -08:00
filehandle.close()
#Check to see if the artist name was found
2008-03-07 05:06:44 -08:00
error_string = "No artist exists with this name"
2008-03-06 04:22:03 -08:00
#print "Error String: " + error_string
#print "XML: " + xml
2008-03-07 05:06:44 -08:00
if xml_string[0:len(error_string)] == error_string:
2008-03-06 04:22:03 -08:00
return None
2008-03-07 05:06:44 -08:00
#We make a little manual change to the url, so that we get the BIG pic off last.FM rather than the 160x160 one
xml_string = xml_string.replace("/160/", "/_/")
2008-03-06 04:22:03 -08:00
#Because we only read in 2 lines, we need to manually close the block
2008-03-07 05:06:44 -08:00
xml_string += "</similarartists>"
try:
dom = minidom.parseString(xml_string)
element = dom.getElementsByTagName("similarartists")[0]
pic_url = element.getAttribute("picture")
except xml.parsers.expat.ExpatError, e:
print "LastFM Error: Could not parse XML '%s'" % (xml_string)
print "LastFM Error: URI Attempted '%s'" % (similar_uri)
2008-03-07 05:06:44 -08:00
return None
2008-03-06 04:22:03 -08:00
return self.get_pixbuf_from_url(pic_url)
2008-03-06 04:22:03 -08:00
def get_pixbuf_from_url(self, pic_url):
chunk_size = 32768
2008-03-06 04:22:03 -08:00
img_handle = urllib.urlopen(pic_url)
img_size = int(img_handle.info().getheader("Content-Length"))
img_data = img_handle.read(img_size)
2008-03-07 05:06:44 -08:00
img_handle.close()
2008-03-06 04:22:03 -08:00
#print pic_url
#print "Img Size: " + str(img_size)
#print "Read size: " + str(len(img_data))
2008-03-06 04:22:03 -08:00
loader = gtk.gdk.PixbufLoader()
try:
#This is a nasty hack to get over GDK Bug: http://bugzilla.gnome.org/show_bug.cgi?id=494667
for chunk in izip(*[chain(img_data, repeat('', chunk_size-1))] * chunk_size):
loader.write(''.join(chunk))
loader.close()
except gobject.GError, e:
print "Last.FM: '%s'" % (e)
#print "Last.FM: Received invalid image file: '%s' " % (pic_url)
2008-03-06 04:22:03 -08:00
return loader.get_pixbuf()