def start_mission(self): if self.artist_name: query_result = multi_query_artist_engine(self.artist_name) if query_result: if utils.download(query_result, get_tmp_save_path(self.artist_name)): cleanup_cover(get_tmp_save_path(self.artist_name), get_cover_save_path(self.artist_name))
def get_cover(self, song, try_web=True): album = self.get_cover_search_str(song) image_path = get_cache_file("cover/%s.jpg" % album) image_path_disable = get_cache_file("cover/%s.jpg.#disable#" % album) if (not song.get_str("title") and not song.get_str("album")) or \ os.path.exists(image_path_disable) or image_path in self.COVER_TO_SKIP: return None # Cover already exist. if os.path.exists(image_path): try: gtk.gdk.pixbuf_new_from_file_at_size(image_path, COVER_SIZE["x"], COVER_SIZE["y"]) except gobject.GError: try: os.unlink(image_path) except: pass else: return image_path # Retrieve cover from mp3 tag if song.get_scheme() == "file" and song.get_ext() in [".mp3", ".tta"]: found = False fp = None try: fp = file(image_path, "wb+") tag = ID3(song.get_path()) for frame in tag.getall("APIC"): found = True fp.write(frame.data) fp.flush() fp.seek(0, 0) except: if fp: fp.close() else: if fp: fp.close() if found and self.cleanup_cover(song, image_path): return image_path # Search in local directory of the file. if song.get("uri") != None and song.get_scheme() == "file": song_dir = song.get_dir() if os.path.exists(song_dir): list_file = os.listdir(song_dir) for pattern in COVER_PATTERNS: matches = fnmatch.filter(list_file, pattern) if matches: matches = sorted(matches, lambda a,b : (len(a) - len(b)) * 10 + cmp(a, b)) if self.cleanup_cover(song, song_dir + "/" + matches[0], image_path): return image_path if not config.getboolean("setting", "offline") and try_web and is_network_connected(): try: ret = False # try url cover tag if song.get("album_cover_url"): ret = utils.download(song.get("album_cover_url"), image_path) if ret and self.cleanup_cover(song, image_path): return image_path cover_img_url = multi_query_artist_engine(album) if cover_img_url: ret = utils.download(cover_img_url, image_path) if ret and self.cleanup_cover(song, image_path): return image_path except: pass # No cover found self.remove_cover(song) if try_web: self.logdebug("cover not found %s (web: %s)", image_path, try_web) return None
def get_cover(self, song, try_web=True, read_local=True): album = self.get_cover_search_str(song) image_path = get_cache_file("cover/%s.jpg" % album) image_path_disable = get_cache_file("cover/%s.jpg.#disable#" % album) if (not song.get_str("title") and not song.get_str("album")) or \ os.path.exists(image_path_disable) or image_path in self.COVER_TO_SKIP: return None # Cover already exist. if read_local: if os.path.exists(image_path): try: gtk.gdk.pixbuf_new_from_file_at_size( image_path, COVER_SIZE["x"], COVER_SIZE["y"]) except gobject.GError: try: os.unlink(image_path) except: pass else: return image_path # Retrieve cover from mp3 tag if read_local: if song.get_scheme() == "file" and song.get_ext() in [ ".mp3", ".tta" ]: found = False fp = None try: fp = file(image_path, "wb+") tag = ID3(song.get_path()) for frame in tag.getall("APIC"): found = True fp.write(frame.data) fp.flush() fp.seek(0, 0) except: if fp: fp.close() else: if fp: fp.close() if found and self.cleanup_cover(song, image_path): return image_path # Search in local directory of the file. if read_local: if song.get("uri") != None and song.get_scheme() == "file": song_dir = song.get_dir() if os.path.exists(song_dir): list_file = os.listdir(song_dir) for pattern in COVER_PATTERNS: matches = fnmatch.filter(list_file, pattern) if matches: matches = sorted( matches, lambda a, b: (len(a) - len(b)) * 10 + cmp(a, b)) if self.cleanup_cover(song, song_dir + "/" + matches[0], image_path): return image_path if not config.getboolean( "setting", "offline") and try_web and is_network_connected(): try: ret = False # try url cover tag if song.get("album_cover_url"): ret = utils.download(song.get("album_cover_url"), image_path) if ret and self.cleanup_cover(song, image_path): return image_path cover_img_url = multi_query_artist_engine(album) if cover_img_url: ret = utils.download(cover_img_url, image_path) if ret and self.cleanup_cover(song, image_path): return image_path except: pass # No cover found self.remove_cover(song) if try_web: self.logdebug("cover not found %s (web: %s)", image_path, try_web) return None