def set_mods_for_part(rating_key, part_id, language, item_type, mods, mode="add"): 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") save_stored_sub(current_sub, rating_key, part_id, language, item_type, plex_item=plex_item, storage=storage, stored_subs=stored_subs) storage.save(stored_subs) storage.destroy()
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())