def download_subtitle(self, subtitle, rating_key, mode="m"): from interface.menu_helpers import set_refresh_menu_state item_type = subtitle.item_type part_id = subtitle.part_id metadata = get_plex_metadata(rating_key, part_id, item_type) providers = config.get_providers(media_type="series" if item_type == "episode" else "movies") scanned_parts = scan_videos([metadata], ignore_all=True, providers=providers) video, plex_part = scanned_parts.items()[0] pre_download_hook(subtitle) # downloaded_subtitles = {subliminal.Video: [subtitle, subtitle, ...]} download_subtitles([subtitle], providers=providers, provider_configs=config.provider_settings, pool_class=config.provider_pool, throttle_callback=config.provider_throttle) post_download_hook(subtitle) # may be redundant subtitle.pack_data = None download_successful = False if subtitle.content: try: save_subtitles(scanned_parts, {video: [subtitle]}, mode=mode, mods=config.default_mods) if mode == "m": Log.Debug(u"%s: Manually downloaded subtitle for: %s", self.name, rating_key) track_usage("Subtitle", "manual", "download", 1) elif mode == "b": Log.Debug(u"%s: Downloaded better subtitle for: %s", self.name, rating_key) track_usage("Subtitle", "better", "download", 1) download_successful = True refresh_item(rating_key) except: Log.Error(u"%s: Something went wrong when downloading specific subtitle: %s", self.name, traceback.format_exc()) finally: set_refresh_menu_state(None) if download_successful: # store item in history from support.history import get_history item_title = get_title_for_video_metadata(metadata, add_section_title=False) history = get_history() history.add(item_title, video.id, section_title=video.plexapi_metadata["section"], subtitle=subtitle, mode=mode) history.destroy() # clear missing subtitles menu data if not scheduler.is_task_running("MissingSubtitles"): scheduler.clear_task_data("MissingSubtitles") else: set_refresh_menu_state(u"%s: Subtitle download failed (%s)" % (self.name, rating_key)) return download_successful
def download_subtitle(self, subtitle, rating_key, mode="m"): from interface.menu_helpers import set_refresh_menu_state item_type = subtitle.item_type part_id = subtitle.part_id metadata = get_plex_metadata(rating_key, part_id, item_type) scanned_parts = scan_videos( [metadata], kind="series" if item_type == "episode" else "movie", ignore_all=True) video, plex_part = scanned_parts.items()[0] # downloaded_subtitles = {subliminal.Video: [subtitle, subtitle, ...]} download_subtitles([subtitle], providers=config.providers, provider_configs=config.provider_settings, pool_class=config.provider_pool) download_successful = False if subtitle.content: try: save_subtitles(scanned_parts, {video: [subtitle]}, mode=mode, mods=config.default_mods) Log.Debug(u"%s: Manually downloaded subtitle for: %s", self.name, rating_key) download_successful = True refresh_item(rating_key) track_usage("Subtitle", "manual", "download", 1) except: Log.Error( u"%s: Something went wrong when downloading specific subtitle: %s", self.name, traceback.format_exc()) finally: set_refresh_menu_state(None) if download_successful: # store item in history from support.history import get_history item_title = get_title_for_video_metadata( metadata, add_section_title=False) history = get_history() history.add( item_title, video.id, section_title=video.plexapi_metadata["section"], subtitle=subtitle, mode=mode) else: set_refresh_menu_state(u"%s: Subtitle download failed (%s)", self.name, rating_key) return download_successful
def manual_download_subtitle(path, language, hi, subtitle, provider, providers_auth, sceneName, title, media_type): logging.debug('BAZARR Manually downloading subtitles for this file: ' + path) subtitle = pickle.loads(codecs.decode(subtitle.encode(), "base64")) use_scenename = settings.general.getboolean('use_scenename') use_postprocessing = settings.general.getboolean('use_postprocessing') postprocessing_cmd = settings.general.postprocessing_cmd single = settings.general.getboolean('single_language') video = get_video(path, title, sceneName, use_scenename, providers={provider}, media_type=media_type) if video: min_score, max_score, scores = get_scores(video, media_type) try: if provider: download_subtitles([subtitle], providers={provider}, provider_configs=providers_auth, pool_class=provider_pool(), throttle_callback=provider_throttle) logging.debug( 'BAZARR Subtitles file downloaded for this file:' + path) else: logging.info("BAZARR All providers are throttled") return None except Exception as e: logging.exception( 'BAZARR Error downloading subtitles for this file ' + path) return None else: if not subtitle.is_valid(): notifications.write( msg='No valid subtitles file found for this file: ' + path, queue='get_subtitle') logging.exception( 'BAZARR No valid subtitles file found for this file: ' + path) return logging.debug('BAZARR Subtitles file downloaded for this file:' + path) try: score = round(subtitle.score / max_score * 100, 2) fld = get_target_folder(path) chmod = int(settings.general.chmod, 8) if not sys.platform.startswith( 'win') and settings.general.getboolean( 'chmod_enabled') else None saved_subtitles = save_subtitles( video.original_path, [subtitle], single=single, tags=None, # fixme directory=fld, chmod=chmod, # formats=("srt", "vtt") path_decoder=force_unicode) except Exception as e: logging.exception( 'BAZARR Error saving subtitles file to disk for this file:' + path) return else: if saved_subtitles: for saved_subtitle in saved_subtitles: downloaded_provider = saved_subtitle.provider_name if saved_subtitle.language == 'pt-BR': downloaded_language_code3 = 'pob' else: downloaded_language_code3 = subtitle.language.alpha3 downloaded_language = language_from_alpha3( downloaded_language_code3) downloaded_language_code2 = alpha2_from_alpha3( downloaded_language_code3) downloaded_path = saved_subtitle.storage_path logging.debug('BAZARR Subtitles file saved to disk: ' + downloaded_path) message = downloaded_language + " subtitles downloaded from " + downloaded_provider + " with a score of " + unicode( score) + "% using manual search." if use_postprocessing is True: command = pp_replace(postprocessing_cmd, path, downloaded_path, downloaded_language, downloaded_language_code2, downloaded_language_code3) try: if os.name == 'nt': codepage = subprocess.Popen( "chcp", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # wait for the process to terminate out_codepage, err_codepage = codepage.communicate( ) encoding = out_codepage.split( ':')[-1].strip() process = subprocess.Popen( command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # wait for the process to terminate out, err = process.communicate() if os.name == 'nt': out = out.decode(encoding) except: if out == "": logging.error( 'BAZARR Post-processing result for file ' + path + ' : Nothing returned from command execution' ) else: logging.error( 'BAZARR Post-processing result for file ' + path + ' : ' + out) else: if out == "": logging.info( 'BAZARR Post-processing result for file ' + path + ' : Nothing returned from command execution' ) else: logging.info( 'BAZARR Post-processing result for file ' + path + ' : ' + out) if media_type == 'series': reversed_path = path_replace_reverse(path) else: reversed_path = path_replace_reverse_movie(path) return message, reversed_path, downloaded_language_code2, downloaded_provider, subtitle.score else: logging.error( "BAZARR Tried to manually download a subtitles for file: " + path + " but we weren't able to do (probably throttled by " + str(subtitle.provider_name) + ". Please retry later or select a subtitles from another provider." ) return None logging.debug('BAZARR Ended manually downloading subtitles for file: ' + path)