def add_new_missions(self, db_query, songs): artist_keys = self.filter_artists([song.get_str("artist") for song in songs]) album_infos = self.filter_albums([(song.get_str("artist"), song.get_str("album")) for song in songs]) if artist_keys: if is_network_connected(): self.artist_missions_threadpool.add_missions([FetchArtistCover(artist) for artist in artist_keys]) if album_infos: if is_network_connected(): self.album_missions_threadpool.add_missions([FetchAlbumCover(album_info) for album_info in album_infos])
def init_artist_missions(self): artists = self.get_infos_from_db("artist") artist_missions = [] if artists: if is_network_connected(): artist_missions = [FetchArtistCover(artist_name.replace("/", "")) for artist_name in artists] if artist_missions: if is_network_connected(): self.artist_missions_threadpool.add_missions(artist_missions)
def get_lrc(self, song, try_web=True): lrc_path = self.get_lrc_filepath(song) # lrc already exist if os.path.exists(lrc_path): if self.vaild_lrc(lrc_path): return lrc_path else: os.unlink(lrc_path) # Search in local directory of the file if song.get("uri") != None and song.get_scheme() == "file": local_lrc = os.path.join(song.get_dir(), self.get_lrc_search_str(song)) if os.path.exists(local_lrc): return local_lrc if not config.getboolean("setting", "offline") and try_web and is_network_connected(): trust_a = song.get_str("artist") trust_t = song.get_str("title") filename = song.get_filename() if "-" in filename: untrust_a = filename.split("-")[0].strip() untrust_t = filename.split("-")[1].strip() else: untrust_a = song.get_str("artist") untrust_t = song.get_filename() trust_result = self.multiple_engine(song, lrc_path, trust_a, trust_t) if trust_result: return trust_result else: return self.multiple_engine(song, lrc_path, untrust_a, untrust_t) return ""
def __open_url_addr(self, scan_keyword, index=1): # if index: keyword = urllib.quote(self.__to_code_gb2312(scan_keyword)) scan_html = SCAN_HTML_PAGE % (index, keyword) else: scan_html = MAIN_HTML + scan_keyword # print scan_html if is_network_connected(): url_open = urllib2.urlopen(scan_html) read_buffer = url_open.read() return read_buffer else: return ""
def get_cover(self, song, try_web=True): default_image_path = self.default_cover 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 default_image_path # 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: os.unlink(image_path) 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"), utils.get_uri_from_path(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 default_image_path