From 456dc85551b367d0639f980482701bf9e4d17acf Mon Sep 17 00:00:00 2001 From: noisymime Date: Sat, 5 Apr 2008 00:40:53 +0000 Subject: [PATCH] - Fixes to last.fm interface so that it gets a good sized image, without download GIANT images - Threading fixes for image downloads --- InputQueue.py | 3 +- modules/music_player/lastFM_interface.py | 33 ++++++++++++++++++-- modules/music_player/music_objects/artist.py | 11 ++++--- 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/InputQueue.py b/InputQueue.py index bb35753..6d29bd3 100644 --- a/InputQueue.py +++ b/InputQueue.py @@ -148,7 +148,7 @@ class InputQueue(gobject.GObject): self.queue_west = 0 self.queue_north = 0 - self.emit("queue-flushed") + if not self.accelerating: self.emit("queue-flushed") def is_in_queue(self): @@ -187,5 +187,6 @@ class InputQueue(gobject.GObject): return True self.accelerating = False + self.emit("queue-flushed") #print "Deceleration finished" return False \ No newline at end of file diff --git a/modules/music_player/lastFM_interface.py b/modules/music_player/lastFM_interface.py index 49a82da..0bffdc4 100644 --- a/modules/music_player/lastFM_interface.py +++ b/modules/music_player/lastFM_interface.py @@ -12,6 +12,11 @@ class lastFM_interface: lastFM_album_xml_uri = lastFM_base_xml_uri + "album/" lastFM_track_xml_uri = lastFM_base_xml_uri + "track/" + #Maximum size of images returned + #If images are larger than this, they will be downloaded and resized + max_image_width = 800 + max_image_height = 600 + def __init__(self): pass @@ -36,8 +41,9 @@ class lastFM_interface: if xml_string[0:len(error_string)] == error_string: return None - #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/", "/_/") + #We make a little manual change to the url, so that we get a bigger pic off last.FM rather than the 160x160 one + #xml_string = xml_string.replace("/160/", "/_/") #Force use this to get BIG images + xml_string = xml_string.replace("/160/", "/500/") #Attempt to use the 500 size image #Because we only read in 2 lines, we need to manually close the block xml_string += "" @@ -51,6 +57,14 @@ class lastFM_interface: print "LastFM Error: URI Attempted '%s'" % (similar_uri) return None + #We do a check to see if a 500 size image exists + #If not, fall back onto the _ size image (ie the Biggest Last.fm has) + img_handle = urllib.urlopen(pic_url) + img_size = int(img_handle.info().getheader("Content-Length")) + if img_size == 0: + pic_url = pic_url.replace("/500/", "/_/") + img_handle.close() + return self.get_pixbuf_from_url(pic_url) @@ -77,5 +91,18 @@ class lastFM_interface: print "Last.FM: '%s'" % (e) #print "Last.FM: Received invalid image file: '%s' " % (pic_url) - return loader.get_pixbuf() + pixbuf = loader.get_pixbuf() + + #If the image is larger than our maximum size, shrink it down + #This should _generally_ not be needed, but will be used whenver last.fm returns a very large image by mistake + if pixbuf.get_width() > self.max_image_width: + yx_ratio = float(pixbuf.get_height()) / float(pixbuf.get_width()) + height = int(self.max_image_width * yx_ratio) + pixbuf = pixbuf.scale_simple(self.max_image_width, height, gtk.gdk.INTERP_HYPER) + elif pixbuf.get_height() > self.max_image_height: + xy_ratio = float(pixbuf.get_width()) / float(pixbuf.get_height()) + width = int(self.max_image_height * xy_ratio) + pixbuf = pixbuf.scale_simple(width, self.max_image_height, gtk.gdk.INTERP_HYPER) + + return pixbuf \ No newline at end of file diff --git a/modules/music_player/music_objects/artist.py b/modules/music_player/music_objects/artist.py index 0dba25b..da7aac1 100644 --- a/modules/music_player/music_objects/artist.py +++ b/modules/music_player/music_objects/artist.py @@ -57,10 +57,13 @@ class artist(MusicObject): #We send a request off to LastFM to grab an image. #This will emit the "image-found" signal when and if it was successful #gobject.idle_add(self.get_image_from_lastFM) - thread = threading.Thread(target=self.get_image_from_lastFM) - thread.start() - #thread.start_new_thread(self.get_image_from_lastFM, (None,)) - #pixbuf = self.get_image_from_lastFM() + try: + thread = threading.Thread(target=self.get_image_from_lastFM) + thread.start() + except thread.error, e: + "Music_Player: Attempted to start too many threads" + #Returning None forces the default image to be used + return None return self.PENDING_DOWNLOAD def get_image_from_lastFM(self):