def save_stored_sub(stored_subtitle, rating_key, part_id, language, item_type, plex_item=None, storage=None, stored_subs=None): """ in order for this to work, if the calling supplies stored_subs and storage, it has to trigger its saving and destruction explicitly :param stored_subtitle: :param rating_key: :param part_id: :param language: :param item_type: :param plex_item: :param storage: :param stored_subs: :return: """ from support.plex_media import get_plex_metadata from support.scanning import scan_videos from support.storage import save_subtitles, get_subtitle_storage plex_item = plex_item or get_item(rating_key) stored_subs_was_provided = True if not stored_subs or not storage: storage = get_subtitle_storage() stored_subs = storage.load(plex_item.rating_key) stored_subs_was_provided = False if not all([plex_item, stored_subs]): return try: metadata = get_plex_metadata(rating_key, part_id, item_type, plex_item=plex_item) except PartUnknownException: return scanned_parts = scan_videos([metadata], ignore_all=True, skip_hashing=True) video, plex_part = scanned_parts.items()[0] subtitle = ModifiedSubtitle(language, mods=stored_subtitle.mods) subtitle.content = stored_subtitle.content if stored_subtitle.encoding: # thanks plex setattr(subtitle, "_guessed_encoding", stored_subtitle.encoding) if stored_subtitle.encoding != "utf-8": subtitle.normalize() stored_subtitle.content = subtitle.content stored_subtitle.encoding = "utf-8" storage.save(stored_subs) subtitle.plex_media_fps = plex_part.fps subtitle.page_link = stored_subtitle.id subtitle.language = language subtitle.id = stored_subtitle.id try: save_subtitles(scanned_parts, {video: [subtitle]}, mode="m", bare_save=True) stored_subtitle.mods = subtitle.mods Log.Debug("Modified %s subtitle for: %s:%s with: %s", language.name, rating_key, part_id, ", ".join(subtitle.mods) if subtitle.mods else "none") except: Log.Error("Something went wrong when modifying subtitle: %s", traceback.format_exc()) if subtitle.storage_path: stored_subtitle.last_mod = datetime.datetime.fromtimestamp(os.path.getmtime(subtitle.storage_path)) if not stored_subs_was_provided: storage.save(stored_subs) storage.destroy()
def extract_embedded_sub(**kwargs): rating_key = kwargs["rating_key"] part_id = kwargs.pop("part_id") stream_index = kwargs.pop("stream_index") with_mods = kwargs.pop("with_mods", False) language = Language.fromietf(kwargs.pop("language")) refresh = kwargs.pop("refresh", True) set_current = kwargs.pop("set_current", True) plex_item = kwargs.pop("plex_item", get_item(rating_key)) item_type = get_item_kind_from_item(plex_item) part = kwargs.pop("part", get_part(plex_item, part_id)) scanned_videos = kwargs.pop("scanned_videos", None) any_successful = False if part: if not scanned_videos: metadata = get_plex_metadata(rating_key, part_id, item_type, plex_item=plex_item) scanned_videos = scan_videos([metadata], ignore_all=True, skip_hashing=True) for stream in part.streams: # subtitle stream if str(stream.index) == stream_index: is_forced = is_stream_forced(stream) bn = os.path.basename(part.file) set_refresh_menu_state(_(u"Extracting subtitle %(stream_index)s of %(filename)s", stream_index=stream_index, filename=bn)) Log.Info(u"Extracting stream %s (%s) of %s", stream_index, display_language(language), bn) out_codec = stream.codec if stream.codec != "mov_text" else "srt" args = [ config.plex_transcoder, "-i", part.file, "-map", "0:%s" % stream_index, "-f", out_codec, "-" ] output = None try: output = subprocess.check_output(quote_args(args), stderr=subprocess.PIPE, shell=True) except: Log.Error("Extraction failed: %s", traceback.format_exc()) if output: subtitle = ModifiedSubtitle(language, mods=config.default_mods if with_mods else None) subtitle.content = output subtitle.provider_name = "embedded" subtitle.id = "stream_%s" % stream_index subtitle.score = 0 subtitle.set_encoding("utf-8") # fixme: speedup video; only video.name is needed save_successful = save_subtitles(scanned_videos, {scanned_videos.keys()[0]: [subtitle]}, mode="m", set_current=set_current, is_forced=is_forced) set_refresh_menu_state(None) if save_successful and refresh: refresh_item(rating_key) any_successful = True return any_successful
def extract_embedded_sub(**kwargs): rating_key = kwargs["rating_key"] part_id = kwargs.pop("part_id") stream_index = kwargs.pop("stream_index") with_mods = kwargs.pop("with_mods", False) language = Language.fromietf(kwargs.pop("language")) refresh = kwargs.pop("refresh", True) set_current = kwargs.pop("set_current", True) plex_item = kwargs.pop("plex_item", get_item(rating_key)) item_type = get_item_kind_from_item(plex_item) part = kwargs.pop("part", get_part(plex_item, part_id)) scanned_videos = kwargs.pop("scanned_videos", None) extract_mode = kwargs.pop("extract_mode", "a") any_successful = False if part: if not scanned_videos: metadata = get_plex_metadata(rating_key, part_id, item_type, plex_item=plex_item) scanned_videos = scan_videos([metadata], ignore_all=True, skip_hashing=True) for stream in part.streams: # subtitle stream if str(stream.index) == stream_index: is_forced = is_stream_forced(stream) bn = os.path.basename(part.file) set_refresh_menu_state(_(u"Extracting subtitle %(stream_index)s of %(filename)s", stream_index=stream_index, filename=bn)) Log.Info(u"Extracting stream %s (%s) of %s", stream_index, str(language), bn) out_codec = stream.codec if stream.codec != "mov_text" else "srt" args = [ config.plex_transcoder, "-i", part.file, "-map", "0:%s" % stream_index, "-f", out_codec, "-" ] cmdline = quote_args(args) Log.Debug(u"Calling: %s", cmdline) if mswindows: Log.Debug("MSWindows: Fixing encoding") cmdline = cmdline.encode("mbcs") output = None try: output = subprocess.check_output(cmdline, stderr=subprocess.PIPE, shell=True) except: Log.Error("Extraction failed: %s", traceback.format_exc()) if output: subtitle = ModifiedSubtitle(language, mods=config.default_mods if with_mods else None) subtitle.content = output subtitle.provider_name = "embedded" subtitle.id = "stream_%s" % stream_index subtitle.score = 0 subtitle.set_encoding("utf-8") # fixme: speedup video; only video.name is needed video = scanned_videos.keys()[0] save_successful = save_subtitles(scanned_videos, {video: [subtitle]}, mode="m", set_current=set_current) set_refresh_menu_state(None) if save_successful and refresh: refresh_item(rating_key) # add item to history item_title = get_title_for_video_metadata(video.plexapi_metadata, add_section_title=False, add_episode_title=True) history = get_history() history.add(item_title, video.id, section_title=video.plexapi_metadata["section"], thumb=video.plexapi_metadata["super_thumb"], subtitle=subtitle, mode=extract_mode) history.destroy() any_successful = True return any_successful
def update(self, metadata, media, lang): if not config.enable_agent: Log.Debug("Skipping Sub-Zero agent(s)") return Log.Debug("Sub-Zero %s, %s update called" % (config.version, self.agent_type)) if not media: Log.Error( "Called with empty media, something is really wrong with your setup!" ) return intent = get_intent() item_ids = [] try: config.init_subliminal_patches() all_videos = media_to_videos(media, kind=self.agent_type) # media ignored? ignore_parts_cleanup = [] videos = [] for video in all_videos: if not is_wanted(video["id"], item=video["item"]): Log.Debug(u'Skipping "%s"' % video["filename"]) ignore_parts_cleanup.append(video["path"]) continue videos.append(video) # find local media update_local_media(all_videos, ignore_parts_cleanup=ignore_parts_cleanup) if not videos: Log.Debug(u"Nothing to do.") return try: use_score = int(Prefs[self.score_prefs_key].strip()) except ValueError: Log.Error( "Please only put numbers into the scores setting. Exiting") return set_refresh_menu_state(media, media_type=self.agent_type) # scanned_video_part_map = {subliminal.Video: plex_part, ...} providers = config.get_providers(media_type=self.agent_type) try: scanned_video_part_map = scan_videos(videos, providers=providers) except IOError, e: Log.Exception( "Permission error, please check your folder/file permissions. Exiting." ) if cast_bool(Prefs["check_permissions"]): config.permissions_ok = False config.missing_permissions = e.message return # auto extract embedded if config.embedded_auto_extract: if config.plex_transcoder: agent_extract_embedded(scanned_video_part_map) else: Log.Warn("Plex Transcoder not found, can't auto extract") # clear missing subtitles menu data if not scheduler.is_task_running("MissingSubtitles"): scheduler.clear_task_data("MissingSubtitles") downloaded_subtitles = None # debounce for self.debounce seconds now = datetime.datetime.now() if "last_call" in Dict: last_call = Dict["last_call"] if last_call + datetime.timedelta(seconds=self.debounce) > now: wait = self.debounce - (now - last_call).seconds if wait >= 1: Log.Debug("Waiting %s seconds until continuing", wait) Thread.Sleep(wait) # downloaded_subtitles = {subliminal.Video: [subtitle, subtitle, ...]} try: downloaded_subtitles = download_best_subtitles( scanned_video_part_map, min_score=use_score, throttle_time=self.debounce, providers=providers) except: Log.Exception( "Something went wrong when downloading subtitles") if downloaded_subtitles is not None: Dict["last_call"] = datetime.datetime.now() item_ids = get_media_item_ids(media, kind=self.agent_type) downloaded_any = False if downloaded_subtitles: downloaded_any = any(downloaded_subtitles.values()) if downloaded_any: save_successful = False try: save_successful = save_subtitles(scanned_video_part_map, downloaded_subtitles, mods=config.default_mods) except: Log.Exception("Something went wrong when saving subtitles") track_usage("Subtitle", "refreshed", "download", 1) # store SZ meta info even if download wasn't successful if not save_successful: self.store_blank_subtitle_metadata(scanned_video_part_map) else: for video, video_subtitles in downloaded_subtitles.items(): # store item(s) in history for subtitle in video_subtitles: history = get_history() item_title = get_title_for_video_metadata( video.plexapi_metadata, add_section_title=False) history.add( item_title, video.id, section_title=video. plexapi_metadata["section"], thumb=video.plexapi_metadata["super_thumb"], subtitle=subtitle) history.destroy() else: # store SZ meta info even if we've downloaded none self.store_blank_subtitle_metadata(scanned_video_part_map) update_local_media(videos)
def set_mods_for_part(rating_key, part_id, language, item_type, mods, mode="add"): from support.plex_media import get_plex_metadata from support.scanning import scan_videos from support.storage import save_subtitles plex_item = get_item(rating_key) if not plex_item: return current_sub, stored_subs, storage = get_current_sub(rating_key, part_id, language, plex_item=plex_item) if mode == "add": for mod in mods: identifier, args = SubtitleModifications.parse_identifier(mod) mod_class = SubtitleModifications.get_mod_class(identifier) if identifier not in mod_registry.mods_available: raise NotImplementedError("Mod unknown or not registered") # clean exclusive mods if mod_class.exclusive and current_sub.mods: for current_mod in current_sub.mods[:]: if current_mod.startswith(identifier): current_sub.mods.remove(current_mod) Log.Info("Removing superseded mod %s" % current_mod) current_sub.add_mod(mod) elif mode == "clear": current_sub.add_mod(None) elif mode == "remove": for mod in mods: current_sub.mods.remove(mod) elif mode == "remove_last": if current_sub.mods: current_sub.mods.pop() else: raise NotImplementedError("Wrong mode given") storage.save(stored_subs) try: metadata = get_plex_metadata(rating_key, part_id, item_type, plex_item=plex_item) except PartUnknownException: return scanned_parts = scan_videos( [metadata], kind="series" if item_type == "episode" else "movie", ignore_all=True, no_refining=True) video, plex_part = scanned_parts.items()[0] subtitle = ModifiedSubtitle(language, mods=current_sub.mods) subtitle.content = current_sub.content if current_sub.encoding: # thanks plex setattr(subtitle, "_guessed_encoding", current_sub.encoding) if current_sub.encoding != "utf-8": subtitle.normalize() current_sub.content = subtitle.content current_sub.encoding = "utf-8" storage.save(stored_subs) storage.destroy() subtitle.plex_media_fps = plex_part.fps subtitle.page_link = "modify subtitles with: %s" % (", ".join( current_sub.mods) if current_sub.mods else "none") subtitle.language = language subtitle.id = current_sub.id try: save_subtitles(scanned_parts, {video: [subtitle]}, mode="m", bare_save=True) Log.Debug("Modified %s subtitle for: %s:%s with: %s", language.name, rating_key, part_id, ", ".join(current_sub.mods) if current_sub.mods else "none") except: Log.Error("Something went wrong when modifying subtitle: %s", traceback.format_exc())
def extract_embedded_sub(**kwargs): rating_key = kwargs["rating_key"] part_id = kwargs.pop("part_id") stream_index = kwargs.pop("stream_index") with_mods = kwargs.pop("with_mods", False) language = Language.fromietf(kwargs.pop("language")) refresh = kwargs.pop("refresh", True) set_current = kwargs.pop("set_current", True) plex_item = kwargs.pop("plex_item", get_item(rating_key)) item_type = get_item_kind_from_item(plex_item) part = kwargs.pop("part", get_part(plex_item, part_id)) scanned_videos = kwargs.pop("scanned_videos", None) extract_mode = kwargs.pop("extract_mode", "a") any_successful = False if part: if not scanned_videos: metadata = get_plex_metadata(rating_key, part_id, item_type, plex_item=plex_item) scanned_videos = scan_videos([metadata], ignore_all=True, skip_hashing=True) for stream in part.streams: # subtitle stream if str(stream.index) == stream_index: is_forced = is_stream_forced(stream) bn = os.path.basename(part.file) set_refresh_menu_state( _(u"Extracting subtitle %(stream_index)s of %(filename)s", stream_index=stream_index, filename=bn)) Log.Info(u"Extracting stream %s (%s) of %s", stream_index, str(language), bn) out_codec = stream.codec if stream.codec != "mov_text" else "srt" args = [ config.plex_transcoder, "-i", part.file, "-map", "0:%s" % stream_index, "-f", out_codec, "-" ] cmdline = quote_args(args) Log.Debug(u"Calling: %s", cmdline) if mswindows: Log.Debug("MSWindows: Fixing encoding") cmdline = cmdline.encode("mbcs") output = None try: output = subprocess.check_output(cmdline, stderr=subprocess.PIPE, shell=True) except: Log.Error("Extraction failed: %s", traceback.format_exc()) if output: subtitle = ModifiedSubtitle( language, mods=config.default_mods if with_mods else None) subtitle.content = output subtitle.provider_name = "embedded" subtitle.id = "stream_%s" % stream_index subtitle.score = 0 subtitle.set_encoding("utf-8") # fixme: speedup video; only video.name is needed video = scanned_videos.keys()[0] save_successful = save_subtitles(scanned_videos, {video: [subtitle]}, mode="m", set_current=set_current) set_refresh_menu_state(None) if save_successful and refresh: refresh_item(rating_key) # add item to history item_title = get_title_for_video_metadata( video.plexapi_metadata, add_section_title=False, add_episode_title=True) history = get_history() history.add( item_title, video.id, section_title=video.plexapi_metadata["section"], thumb=video.plexapi_metadata["super_thumb"], subtitle=subtitle, mode=extract_mode) history.destroy() any_successful = True return any_successful
def update(self, metadata, media, lang): if not config.enable_agent: Log.Debug("Skipping Sub-Zero agent(s)") return Log.Debug("Sub-Zero %s, %s update called" % (config.version, self.agent_type)) intent = get_intent() if not media: Log.Error( "Called with empty media, something is really wrong with your setup!" ) return item_ids = [] try: config.init_subliminal_patches() videos = media_to_videos(media, kind=self.agent_type) # find local media update_local_media(metadata, media, media_type=self.agent_type) # media ignored? use_any_parts = False for video in videos: if is_ignored(video["id"]): Log.Debug(u"Ignoring %s" % video) continue use_any_parts = True if not use_any_parts: Log.Debug(u"Nothing to do.") return try: use_score = int(Prefs[self.score_prefs_key].strip()) except ValueError: Log.Error( "Please only put numbers into the scores setting. Exiting") return set_refresh_menu_state(media, media_type=self.agent_type) # scanned_video_part_map = {subliminal.Video: plex_part, ...} providers = config.get_providers(media_type=self.agent_type) scanned_video_part_map = scan_videos(videos, providers=providers) # auto extract embedded if config.embedded_auto_extract: agent_extract_embedded(videos) # clear missing subtitles menu data if not scheduler.is_task_running("MissingSubtitles"): scheduler.clear_task_data("MissingSubtitles") downloaded_subtitles = None # debounce for self.debounce seconds now = datetime.datetime.now() if "last_call" in Dict: last_call = Dict["last_call"] if last_call + datetime.timedelta(seconds=self.debounce) > now: wait = self.debounce - (now - last_call).seconds if wait >= 1: Log.Debug("Waiting %s seconds until continuing", wait) Thread.Sleep(wait) # downloaded_subtitles = {subliminal.Video: [subtitle, subtitle, ...]} try: downloaded_subtitles = download_best_subtitles( scanned_video_part_map, min_score=use_score, throttle_time=self.debounce, providers=providers) except: Log.Exception( "Something went wrong when downloading subtitles") if downloaded_subtitles is not None: Dict["last_call"] = datetime.datetime.now() item_ids = get_media_item_ids(media, kind=self.agent_type) downloaded_any = False if downloaded_subtitles: downloaded_any = any(downloaded_subtitles.values()) if downloaded_any: save_successful = False try: save_successful = save_subtitles(scanned_video_part_map, downloaded_subtitles, mods=config.default_mods) except: Log.Exception("Something went wrong when saving subtitles") track_usage("Subtitle", "refreshed", "download", 1) # store SZ meta info even if download wasn't successful if not save_successful: self.store_blank_subtitle_metadata(scanned_video_part_map) else: for video, video_subtitles in downloaded_subtitles.items(): # store item(s) in history for subtitle in video_subtitles: item_title = get_title_for_video_metadata( video.plexapi_metadata, add_section_title=False) history = get_history() history.add(item_title, video.id, section_title=video. plexapi_metadata["section"], subtitle=subtitle) history.destroy() else: # store SZ meta info even if we've downloaded none self.store_blank_subtitle_metadata(scanned_video_part_map) update_local_media(metadata, media, media_type=self.agent_type) finally: # update the menu state set_refresh_menu_state(None) # notify any running tasks about our finished update for item_id in item_ids: #scheduler.signal("updated_metadata", item_id) # resolve existing intent for that id intent.resolve("force", item_id) Dict.Save() # fsync cache if config.new_style_cache: config.sync_cache()
def update(self, metadata, media, lang): if not config.enable_agent: Log.Debug("Skipping Sub-Zero agent(s)") return Log.Debug("Sub-Zero %s, %s update called" % (config.version, self.agent_type)) if not media: Log.Error("Called with empty media, something is really wrong with your setup!") return intent = get_intent() item_ids = [] try: config.init_subliminal_patches() all_videos = media_to_videos(media, kind=self.agent_type) # media ignored? ignore_parts_cleanup = [] videos = [] for video in all_videos: if not is_wanted(video["id"], item=video["item"]): Log.Debug(u'Skipping "%s"' % video["filename"]) ignore_parts_cleanup.append(video["path"]) continue videos.append(video) # find local media update_local_media(all_videos, ignore_parts_cleanup=ignore_parts_cleanup) if not videos: Log.Debug(u"Nothing to do.") return try: use_score = int(Prefs[self.score_prefs_key].strip()) except ValueError: Log.Error("Please only put numbers into the scores setting. Exiting") return set_refresh_menu_state(media, media_type=self.agent_type) # scanned_video_part_map = {subliminal.Video: plex_part, ...} providers = config.get_providers(media_type=self.agent_type) try: scanned_video_part_map = scan_videos(videos, providers=providers) except IOError, e: Log.Exception("Permission error, please check your folder/file permissions. Exiting.") if cast_bool(Prefs["check_permissions"]): config.permissions_ok = False config.missing_permissions = e.message return # auto extract embedded if config.embedded_auto_extract: if config.plex_transcoder: agent_extract_embedded(scanned_video_part_map) else: Log.Warn("Plex Transcoder not found, can't auto extract") # clear missing subtitles menu data if not scheduler.is_task_running("MissingSubtitles"): scheduler.clear_task_data("MissingSubtitles") downloaded_subtitles = None # debounce for self.debounce seconds now = datetime.datetime.now() if "last_call" in Dict: last_call = Dict["last_call"] if last_call + datetime.timedelta(seconds=self.debounce) > now: wait = self.debounce - (now - last_call).seconds if wait >= 1: Log.Debug("Waiting %s seconds until continuing", wait) Thread.Sleep(wait) # downloaded_subtitles = {subliminal.Video: [subtitle, subtitle, ...]} try: downloaded_subtitles = download_best_subtitles(scanned_video_part_map, min_score=use_score, throttle_time=self.debounce, providers=providers) except: Log.Exception("Something went wrong when downloading subtitles") if downloaded_subtitles is not None: Dict["last_call"] = datetime.datetime.now() item_ids = get_media_item_ids(media, kind=self.agent_type) downloaded_any = False if downloaded_subtitles: downloaded_any = any(downloaded_subtitles.values()) if downloaded_any: save_successful = False try: save_successful = save_subtitles(scanned_video_part_map, downloaded_subtitles, mods=config.default_mods) except: Log.Exception("Something went wrong when saving subtitles") track_usage("Subtitle", "refreshed", "download", 1) # store SZ meta info even if download wasn't successful if not save_successful: self.store_blank_subtitle_metadata(scanned_video_part_map) else: for video, video_subtitles in downloaded_subtitles.items(): # store item(s) in history for subtitle in video_subtitles: history = get_history() item_title = get_title_for_video_metadata(video.plexapi_metadata, add_section_title=False) history.add(item_title, video.id, section_title=video.plexapi_metadata["section"], thumb=video.plexapi_metadata["super_thumb"], subtitle=subtitle) history.destroy() else: # store SZ meta info even if we've downloaded none self.store_blank_subtitle_metadata(scanned_video_part_map) update_local_media(videos)