1 #!/usr/bin/env python2.5
5 from album_art_spec import getCoverArtFileName, getCoverArtThumbFileName, get_thumb_filename_for_path
13 except ImportException:
17 LASTFM_APIKEY = "1e1d53528c86406757a6887addef0ace"
18 BASE_LASTFM = "http://ws.audioscrobbler.com/2.0/?method=album.getinfo"
21 BASE_MSN = "http://www.bing.com/images/search?q="
22 MSN_MEDIUM = "+filterui:imagesize-medium"
23 MSN_SMALL = "+filterui:imagesize-medium"
24 MSN_SQUARE = "+filterui:aspect-square"
25 MSN_PHOTO = "+filterui:photo-graphics"
28 # http://www.lastfm.es/api/show?service=290
30 class MussorgskyAlbumArt:
33 bus = dbus.SessionBus ()
36 self.thumbnailer = LocalThumbnailer ()
39 self.thumbnailer = bus.get_object ('org.freedesktop.thumbnailer',
40 '/org/freedesktop/thumbnailer/Generic')
41 except dbus.exceptions.DBusException:
42 print "No thumbnailer available"
43 self.thumbnailer = None
45 def get_album_art (self, artist, album):
47 Return a tuple (album_art, thumbnail_album_art)
49 filename = getCoverArtFileName (album)
50 thumbnail = getCoverArtThumbFileName (album)
52 if (os.path.exists (filename)):
53 print "Album art already there " + filename
55 online_resource = self.__msn_images (artist, album)
57 content = self.__get_url (online_resource)
59 print "Saved %s -> %s " % (online_resource, filename)
60 self.__save_content_into_file (content, filename)
66 if (os.path.exists (thumbnail)):
67 print "Thumbnail exists"
69 if (not self.__request_thumbnail (filename)):
70 print "Failed doing thumbnail. Probably album art is not an image!"
74 return (filename, thumbnail)
76 def __last_fm (self, artist, album):
77 if (not album or len (album) < 1):
80 URL = BASE_LASTFM + "&api_key=" + LASTFM_APIKEY
81 if (artist and len(artist) > 1):
82 URL += "&artist=" + urllib.quote(artist)
84 URL += "&album=" + urllib.quote(album)
86 print "Retrieving: %s" % (URL)
87 result = self.__get_url (URL)
90 doc = libxml2.parseDoc (result)
91 image_nodes = doc.xpathEval ("//image[@size='large']")
92 if len (image_nodes) < 1:
95 return image_nodes[0].content
97 def __msn_images (self, artist, album):
99 good_artist = self.__clean_string_for_search (artist)
100 good_album = self.__clean_string_for_search (album)
103 if (good_album and good_artist):
104 full_try = BASE_MSN + good_album + "+" + good_artist + MSN_MEDIUM + MSN_SQUARE
105 print "Retrieving (album + artist): %s" % (full_try)
106 result = self.__get_url (full_try)
108 return self.__get_first_url_from_msn_results_page (result)
111 if (album.lower ().find ("greatest hit") != -1):
112 print "Ignoring '%s': too generic" % (album)
115 album_try = BASE_MSN + good_album + MSN_MEDIUM + MSN_SQUARE
116 print "Retrieving (album): %s" % (album_try)
117 result = self.__get_url (album_try)
119 return self.__get_first_url_from_msn_results_page (result)
122 artist_try = BASE_MSN + good_artist + "+CD+music" + MSN_SMALL + MSN_SQUARE + MSN_PHOTO
123 print "Retrieving (artist CD): %s" % (artist_try)
124 result = self.__get_url (artist_try)
126 return self.__get_first_url_from_msn_results_page (result)
131 def __get_first_url_from_msn_results_page (self, page):
132 start = page.find ("furl=")
135 end = page.find ("\"", start + len ("furl="))
136 return page [start + len ("furl="): end].replace ("amp;", "")
138 def __clean_string_for_search (self, text):
139 if (not text or len (text) < 1):
142 bad_stuff = "_:?\\-~"
145 clean = clean.replace (c, " ")
147 clean.replace ("/", "%2F")
148 clean = clean.replace (" CD1", "").replace(" CD2", "")
149 return urllib.quote(clean)
151 def __save_content_into_file (self, content, filename):
152 output = open (filename, 'w')
153 output.write (content)
156 def __get_url (self, url):
157 request = urllib2.Request (url)
158 request.add_header ('User-Agent', 'Mussorgsky/0.1 Test')
159 opener = urllib2.build_opener ()
161 return opener.open (request).read ()
165 def __request_thumbnail (self, filename):
166 if (not self.thumbnailer):
167 print "No thumbnailer available"
169 uri = "file://" + filename
170 handle = time.time ()
171 print "Call to thumbnailer"
172 return self.thumbnailer.Queue ([uri], ["image/jpeg"], dbus.UInt32 (handle))
176 class LocalThumbnailer:
178 self.THUMBNAIL_SIZE = (124,124)
180 def Queue (self, uris, mimes, handle):
181 for i in range (0, len(uris)):
183 fullCoverFileName = uri[7:]
184 print fullCoverFileName
185 if (os.path.exists (fullCoverFileName)):
186 thumbFile = get_thumb_filename_for_path (fullCoverFileName)
188 image = Image.open (fullCoverFileName)
192 image = image.resize (self.THUMBNAIL_SIZE, Image.ANTIALIAS )
193 image.save( thumbFile, "JPEG" )
194 print "Thumbnail: " + thumbFile
199 if __name__ == "__main__":
201 if ( len (sys.argv) > 2):
208 maa = MussorgskyAlbumArt ()
209 print "Artist %s - Album %s" % (artist, album)
210 print maa.get_album_art (artist, album)