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 "Albumart: %s " % (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)
102 if (good_album and good_artist):
103 full_try = BASE_MSN + good_album + "+" + good_artist + MSN_MEDIUM + MSN_SQUARE
104 print "Retrieving (album + artist): %s" % (full_try)
105 result = self.__get_url (full_try)
107 return self.__get_first_url_from_msn_results_page (result)
110 if (album.lower ().find ("greatest hit") != -1):
111 print "Ignoring '%s': too generic" % (album)
114 album_try = BASE_MSN + good_album + MSN_MEDIUM + MSN_SQUARE
115 print "Retrieving (album): %s" % (album_try)
116 result = self.__get_url (album_try)
118 return self.__get_first_url_from_msn_results_page (result)
121 artist_try = BASE_MSN + good_artist + "+CD+music" + MSN_SMALL + MSN_SQUARE + MSN_PHOTO
122 print "Retrieving (artist CD): %s" % (artist_try)
123 result = self.__get_url (artist_try)
125 return self.__get_first_url_from_msn_results_page (result)
130 def __get_first_url_from_msn_results_page (self, page):
131 start = page.find ("furl=")
134 end = page.find ("\"", start + len ("furl="))
135 return page [start + len ("furl="): end].replace ("amp;", "")
137 def __clean_string_for_search (self, text):
138 if (not text or len (text) < 1):
141 bad_stuff = "_:?\\-~"
144 clean = clean.replace (c, " ")
146 clean.replace ("/", "%2F")
147 clean = clean.replace (" CD1", "").replace(" CD2", "")
148 return urllib.quote(clean)
150 def __save_content_into_file (self, content, filename):
151 output = open (filename, 'w')
152 output.write (content)
155 def __get_url (self, url):
156 request = urllib2.Request (url)
157 request.add_header ('User-Agent', 'Mussorgsky/0.1 Test')
158 opener = urllib2.build_opener ()
160 return opener.open (request).read ()
164 def __request_thumbnail (self, filename):
165 if (not self.thumbnailer):
166 print "No thumbnailer available"
168 uri = "file://" + filename
169 handle = time.time ()
170 return self.thumbnailer.Queue ([uri], ["image/jpeg"], dbus.UInt32 (handle))
174 class LocalThumbnailer:
176 self.THUMBNAIL_SIZE = (124,124)
178 def Queue (self, uris, mimes, handle):
179 for i in range (0, len(uris)):
181 fullCoverFileName = uri[7:]
182 if (os.path.exists (fullCoverFileName)):
183 thumbFile = get_thumb_filename_for_path (fullCoverFileName)
185 image = Image.open (fullCoverFileName)
189 image = image.resize (self.THUMBNAIL_SIZE, Image.ANTIALIAS )
190 image.save( thumbFile, "JPEG" )
191 print "Thumbnail: " + thumbFile
196 if __name__ == "__main__":
198 if ( len (sys.argv) > 2):
205 maa = MussorgskyAlbumArt ()
206 maa.get_album_art (artist, album)