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 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 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 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: try: os.unlink(lrc_path) except: pass # 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 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 None
def check_network_connection(self, auto=False): if is_network_connected(): switch_tab(self, self.body_paned) if not auto: self.start_fetch_channels() else: switch_tab(self, self.network_failed_box)
def fetch_comment(self): if is_network_connected(): container_remove_all(self.right_comment_box) loading_label = Label(_("Loading comments...")) loading_label_align = gtk.Alignment(0.5, 0, 0, 0) loading_label_align.add(loading_label) loading_label_align.set_padding(10, 0, 0, 0) self.right_comment_box.pack_start(loading_label_align, False, False) web_view = WebView(os.path.join(CONFIG_DIR, "cookie.txt")) web_view.connect("new-window-policy-decision-requested", self.open_url) web_view.connect('console-message', self.webview_console_message_handler) web_view_align = gtk.Alignment() web_view_align.set(0.5, 0, 0, 0) web_view_align.set_padding(33, 33, 33, 33) web_view_align.add(web_view) web_settings = web_view.get_settings() web_settings.set_property("enable-plugins", False) web_settings.set_property("enable-scripts", True) web_view.open("%s/softcenter/v1/comment?n=%s&hl=%s" % ( SERVER_ADDRESS, self.pkg_name, LANGUAGE, )) web_view.connect("load-finished", self.comment_load_finished_cb, web_view_align) create_thread(self.fetch_screenshot).start()
def get_banner(self, channel_info, try_web=True): banner_path = self.get_banner_path(channel_info.get("id", 0)) temp_path = self.get_temp_banner_path(channel_info.get("id", 0)) # Banner image already exist. if os.path.exists(banner_path): try: gtk.gdk.pixbuf_new_from_file(banner_path) except gobject.GError: try: os.unlink(banner_path) except: pass else: return banner_path # Download from remote if not config.getboolean( "setting", "offline") and try_web and is_network_connected(): banner_url = channel_info.get("banner") if banner_url: ret = utils.download(banner_url, temp_path) if ret and self.cleanup_banner(temp_path, banner_path): return banner_path return None
def check_network_connection(self): if is_network_connected(): self.network_connected_flag = True self.webview.load_uri(self.home_url) self.switch_page_view(self.webview) else: self.network_connected_flag = False self.switch_page_view(self.network_failed_box)
def check_network_connection(self): if is_network_connected(): self.network_connected_flag = True self.switch_page_view(self.loading_box) self.album_summary_view.try_fetch_data() else: self.network_connected_flag = False self.switch_page_view(self.network_failed_box)
def check_network_connection(self, auto=False): if is_network_connected(): self.network_connected_flag = True switch_tab(self, self.webview) if not auto: self.reload_browser() else: self.network_connected_flag = False switch_tab(self, self.network_failed_box)
def get_lrc(self, song, try_web=True): lrc_path = self.get_lrc_filepath(song) # user allocation lrc location_lrc = song.get("location_lrc", "") if location_lrc and os.path.exists(location_lrc): return location_lrc # lrc already exist if os.path.exists(lrc_path): if self.vaild_lrc(lrc_path): return lrc_path else: try: os.unlink(lrc_path) except: pass # search in current directory and same name file current_lrc_path = os.path.join(song.get_dir(), song.get_filename() + ".lrc") if os.path.exists(current_lrc_path) and self.vaild_lrc( current_lrc_path): return current_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 try_web and is_network_connected(): if song.get("lyric_url", None): ret = utils.download(song.get("lyric_url"), lrc_path) if ret: return lrc_path 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 None
def restore_status(self): try: target_item = self.items[self.playlist_index] except: target_item = None if target_item: self.switch_view(target_item) if is_network_connected(): self.current_item.play_song(self.last_song, play=True)
def multi_query_artist_engine(artist_name): if not is_network_connected(): return None if artist_name.find(",") != -1: artist_name = artist_name.split(",")[0] quote_artist_name = urllib.quote(artist_name) ting_result = query_artist_cover_from_ting(artist_name) if ting_result: return ting_result return query_artist_cover_from_xiami(quote_artist_name)
def get_phonetic_symbol(text): # Return nothing if text is not ONE word. if len(text.split(" ")) <= 0: return "" if not is_network_connected() or setting_config.get_trayicon_config("local_translate"): # Not found local phonetic yet. return "" else: global voice_simple return voice_simple.get_phonetic_symbol(text)
def get_voice_modules(): if not is_network_connected() or setting_config.get_trayicon_config("local_translate"): voice_engines = tts_plugin.get_voice_engines(setting_config.get_translate_config("src_lang"), False) voice_engine_names = map(lambda (name, display_name): name, voice_engines) if len(voice_engine_names) > 0: local_simple = imp.load_source("local_simple", tts_plugin.get_plugin_file(voice_engine_names[0])) return [local_simple] else: return [] else: return [voice_simple, voice_long]
def get_voice(text, voice): if is_network_connected(): return voice.get_voice(text) else: voice_engines = tts_plugin.get_voice_engines(setting_config.get_translate_config("src_lang"), False) voice_engine_names = map(lambda (name, display_name): name, voice_engines) if len(voice_engine_names) > 0: local_simple = imp.load_source("local_simple", tts_plugin.get_plugin_file(voice_engine_names[0])) return local_simple.get_voice(text) else: return []
def get_phonetic_symbol(text): # Return nothing if text is not ONE word. if len(text.split(" ")) <= 0: return "" if not is_network_connected() or setting_config.get_trayicon_config( "local_translate"): # Not found local phonetic yet. return "" else: global voice_simple return voice_simple.get_phonetic_symbol(text)
def get_lrc(self, song, try_web=True): lrc_path = self.get_lrc_filepath(song) # user allocation lrc location_lrc = song.get("location_lrc", "") if location_lrc and os.path.exists(location_lrc): return location_lrc # lrc already exist if os.path.exists(lrc_path): if self.vaild_lrc(lrc_path): return lrc_path else: try: os.unlink(lrc_path) except: pass # search in current directory and same name file current_lrc_path = os.path.join(song.get_dir(), song.get_filename() + ".lrc") if os.path.exists(current_lrc_path) and self.vaild_lrc(current_lrc_path): return current_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 try_web and is_network_connected(): if song.get("lyric_url", None): ret = utils.download(song.get("lyric_url"), lrc_path) if ret: return lrc_path 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 None
def check_network_connection(self): if is_network_connected(): if self.timeout_times < NETWORK_TRY_TIMES: self.network_connected_flag = True self.switch_page_view(self.loading_box) self.try_fetch_data() else: self.network_connected_flag = False self.switch_page_view(self.network_timeout_box) self.timeout_times = 0 else: self.network_connected_flag = False self.switch_page_view(self.network_failed_box)
def query_album_cover_from_xiami(artist_name, album_name): if not is_network_connected(): return None if not artist_name and not album_name: return False xiami_album_search_url = 'http://www.xiami.com/search/album?key=' + artist_name + '+' + album_name html = public_curl.get(xiami_album_search_url) try: search_result_object = PyQuery(html) album_info_element = search_result_object('div.albumBlock_list div.album_item100_block p.cover a.CDcover100 img') info_href_attr = album_info_element.attr('src') if not info_href_attr: return False return info_href_attr.replace("_1", "_2") except: return False
def get_voice(text, voice): if not is_network_connected() or setting_config.get_trayicon_config( "local_translate"): voice_engines = tts_plugin.get_voice_engines( setting_config.get_translate_config("src_lang"), False) voice_engine_names = map(lambda (name, display_name): name, voice_engines) if len(voice_engine_names) > 0: local_simple = imp.load_source( "local_simple", tts_plugin.get_plugin_file(voice_engine_names[0])) return local_simple.get_voice(text) else: return [] else: return voice.get_voice(text)
def get_cover(self, channel_info, try_web=True): cover_path = self.get_cover_path(channel_info.get("id", 0)) temp_path = self.get_temp_cover_path(channel_info.get("id", 0)) banner_path = self.get_banner_path(channel_info.get("id", 0)) # cover image already exist. if os.path.exists(cover_path): try: gtk.gdk.pixbuf_new_from_file(cover_path) except gobject.GError: try: os.unlink(cover_path) except: pass else: return cover_path # get cover from banner. if os.path.exists(banner_path): try: pixbuf = gtk.gdk.pixbuf_new_from_file_at_size( banner_path, DOUBAN_COVER_SIZE["x"], DOUBAN_COVER_SIZE["y"]) except: pass else: pixbuf.save(cover_path, "png") return cover_path # Download from remote if not config.getboolean( "setting", "offline") and try_web and is_network_connected(): cover_url = channel_info.get("cover") if cover_url: ret = utils.download(cover_url, temp_path) if ret and self.cleanup_cover(temp_path, cover_path): return cover_path return None
def get_cover(self, channel_info, try_web=True): cover_path = self.get_cover_path(channel_info.get("id", 0)) temp_path = self.get_temp_cover_path(channel_info.get("id", 0)) banner_path = self.get_banner_path(channel_info.get("id", 0)) # cover image already exist. if os.path.exists(cover_path): try: gtk.gdk.pixbuf_new_from_file(cover_path) except gobject.GError: try: os.unlink(cover_path) except: pass else: return cover_path # get cover from banner. if os.path.exists(banner_path): try: pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(banner_path, DOUBAN_COVER_SIZE["x"], DOUBAN_COVER_SIZE["y"]) except: pass else: pixbuf.save(cover_path, "png") return cover_path # Download from remote if not config.getboolean("setting", "offline") and try_web and is_network_connected(): cover_url = channel_info.get("cover") if cover_url: ret = utils.download(cover_url, temp_path) if ret and self.cleanup_cover(temp_path, cover_path): return cover_path return None
def get_banner(self, channel_info, try_web=True): banner_path = self.get_banner_path(channel_info.get("id", 0)) temp_path = self.get_temp_banner_path(channel_info.get("id", 0)) # Banner image already exist. if os.path.exists(banner_path): try: gtk.gdk.pixbuf_new_from_file(banner_path) except gobject.GError: try: os.unlink(banner_path) except: pass else: return banner_path # Download from remote if not config.getboolean("setting", "offline") and try_web and is_network_connected(): banner_url = channel_info.get("banner") if banner_url: ret = utils.download(banner_url, temp_path) if ret and self.cleanup_banner(temp_path, banner_path): return banner_path return None
def is_network_connected(): print "Please import deepin_utils.net.is_network_connected, this function will departed in next release version." return net.is_network_connected()
def restore_status(self): if is_network_connected(): if self.channel_info: self.play_channel(self.channel_info)
def get_translate_long(): if not is_network_connected() or setting_config.get_trayicon_config( "local_translate"): return translate_local_long else: return translate_long
def get_translate_simple(): if is_network_connected(): return translate_simple else: return translate_local_simple
def get_translate_long(): if is_network_connected(): return translate_long else: return translate_local_long
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
def check_network_connection(self, auto=False): if is_network_connected(): switch_tab(self, self.webview) else: switch_tab(self, self.network_failed_box)
def get_translate_long(): if not is_network_connected() or setting_config.get_trayicon_config("local_translate"): return translate_local_long else: return translate_long
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