1 #!/usr/bin/env python2.5
4 from album_art_spec import getCoverArtFileName, getCoverArtThumbFileName, get_thumb_filename_for_path
10 libxml_available = True
12 libxml_available = False
22 LASTFM_APIKEY = "1e1d53528c86406757a6887addef0ace"
23 BASE_LASTFM = "http://ws.audioscrobbler.com/2.0/?method=album.getinfo"
26 BASE_MSN = "http://www.bing.com/images/search?q="
27 MSN_MEDIUM = "+filterui:imagesize-medium"
28 MSN_SMALL = "+filterui:imagesize-medium"
29 MSN_SQUARE = "+filterui:aspect-square"
30 MSN_PHOTO = "+filterui:photo-graphics"
33 # http://www.lastfm.es/api/show?service=290
35 class MussorgskyAlbumArt:
38 bus = dbus.SessionBus ()
41 self.thumbnailer = LocalThumbnailer ()
44 self.thumbnailer = bus.get_object ('org.freedesktop.thumbnailer',
45 '/org/freedesktop/thumbnailer/Generic')
46 except dbus.exceptions.DBusException:
47 print "No thumbnailer available"
48 self.thumbnailer = None
50 def get_album_art (self, artist, album):
52 Return a tuple (album_art, thumbnail_album_art)
54 filename = getCoverArtFileName (album)
55 thumbnail = getCoverArtThumbFileName (album)
57 if (os.path.exists (filename)):
58 print "Album art already there " + filename
60 online_resource = self.__msn_images (artist, album)
62 content = self.__get_url (online_resource)
64 print "Albumart: %s " % (filename)
65 self.__save_content_into_file (content, filename)
71 if (os.path.exists (thumbnail)):
72 print "Thumbnail exists"
74 if (not self.__request_thumbnail (filename)):
75 print "Failed doing thumbnail. Probably album art is not an image!"
79 return (filename, thumbnail)
81 def __last_fm (self, artist, album):
83 if (not libxml_available):
86 if (not album or len (album) < 1):
89 URL = BASE_LASTFM + "&api_key=" + LASTFM_APIKEY
90 if (artist and len(artist) > 1):
91 URL += "&artist=" + urllib.quote(artist)
93 URL += "&album=" + urllib.quote(album)
95 print "Retrieving: %s" % (URL)
96 result = self.__get_url (URL)
99 doc = libxml2.parseDoc (result)
100 image_nodes = doc.xpathEval ("//image[@size='large']")
101 if len (image_nodes) < 1:
104 return image_nodes[0].content
106 def __msn_images (self, artist, album):
108 good_artist = self.__clean_string_for_search (artist)
109 good_album = self.__clean_string_for_search (album)
111 if (good_album and good_artist):
112 full_try = BASE_MSN + good_album + "+" + good_artist + MSN_MEDIUM + MSN_SQUARE
113 print "Retrieving (album + artist): %s" % (full_try)
114 result = self.__get_url (full_try)
116 return self.__get_first_url_from_msn_results_page (result)
119 if (album.lower ().find ("greatest hit") != -1):
120 print "Ignoring '%s': too generic" % (album)
123 album_try = BASE_MSN + good_album + MSN_MEDIUM + MSN_SQUARE
124 print "Retrieving (album): %s" % (album_try)
125 result = self.__get_url (album_try)
127 return self.__get_first_url_from_msn_results_page (result)
130 artist_try = BASE_MSN + good_artist + "+CD+music" + MSN_SMALL + MSN_SQUARE + MSN_PHOTO
131 print "Retrieving (artist CD): %s" % (artist_try)
132 result = self.__get_url (artist_try)
134 return self.__get_first_url_from_msn_results_page (result)
139 def __get_first_url_from_msn_results_page (self, page):
140 start = page.find ("furl=")
143 end = page.find ("\"", start + len ("furl="))
144 return page [start + len ("furl="): end].replace ("amp;", "")
146 def __clean_string_for_search (self, text):
147 if (not text or len (text) < 1):
150 bad_stuff = "_:?\\-~"
153 clean = clean.replace (c, " ")
155 clean.replace ("/", "%2F")
156 clean = clean.replace (" CD1", "").replace(" CD2", "")
157 return urllib.quote(clean)
159 def __save_content_into_file (self, content, filename):
160 output = open (filename, 'w')
161 output.write (content)
164 def __get_url (self, url):
165 request = urllib2.Request (url)
166 request.add_header ('User-Agent', 'Mussorgsky/0.1 Test')
167 opener = urllib2.build_opener ()
169 return opener.open (request).read ()
173 def __request_thumbnail (self, filename):
174 if (not self.thumbnailer):
175 print "No thumbnailer available"
177 uri = "file://" + filename
178 handle = time.time ()
179 return self.thumbnailer.Queue ([uri], ["image/jpeg"], dbus.UInt32 (handle))
183 class LocalThumbnailer:
185 self.THUMBNAIL_SIZE = (124,124)
187 def Queue (self, uris, mimes, handle):
188 for i in range (0, len(uris)):
190 fullCoverFileName = uri[7:]
191 if (os.path.exists (fullCoverFileName)):
192 thumbFile = get_thumb_filename_for_path (fullCoverFileName)
194 image = Image.open (fullCoverFileName)
198 image = image.resize (self.THUMBNAIL_SIZE, Image.ANTIALIAS )
199 image.save( thumbFile, "JPEG" )
200 print "Thumbnail: " + thumbFile
205 if __name__ == "__main__":
207 if ( len (sys.argv) > 2):
214 maa = MussorgskyAlbumArt ()
215 maa.get_album_art (artist, album)