def prepare(self): self.items_done = [] recent_items = get_recent_items() missing = items_get_all_missing_subs(recent_items) ids = set([id for added_at, id, title, item in missing if not is_ignored(id, item=item)]) self.items_searching = missing self.items_searching_ids = ids self.items_failed = [] self.percentage = 0 self.time_start = datetime.datetime.now() self.ready_for_display = True
def prepare(self, *args, **kwargs): self.items_done = [] recent_items = get_recent_items() missing = items_get_all_missing_subs(recent_items) ids = set([ id for added_at, id, title, item, missing_languages in missing if not is_ignored(id, item=item) ]) self.items_searching = missing self.items_searching_ids = ids self.items_failed = [] self.percentage = 0 self.ready_for_display = True
def update(self, metadata, media, lang): 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, ...} scanned_video_part_map = scan_videos(videos, kind=self.agent_type) downloaded_subtitles = None if not config.enable_agent: Log.Debug("Skipping Sub-Zero agent(s)") else: # downloaded_subtitles = {subliminal.Video: [subtitle, subtitle, ...]} downloaded_subtitles = download_best_subtitles( scanned_video_part_map, min_score=use_score) 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_subtitles(scanned_video_part_map, downloaded_subtitles, mods=config.default_mods) track_usage("Subtitle", "refreshed", "download", 1) 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) else: # store subtitle info even if we've downloaded none store_subtitle_info( scanned_video_part_map, dict((k, []) for k in scanned_video_part_map.keys()), None, mode="a") 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()
def run(self): super(SearchAllRecentlyAddedMissing, self).run() self.running = True self.prepare() from support.history import get_history history = get_history() now = datetime.datetime.now() min_score_series = int( Prefs["subtitles.search.minimumTVScore2"].strip()) min_score_movies = int( Prefs["subtitles.search.minimumMovieScore2"].strip()) is_recent_str = Prefs["scheduler.item_is_recent_age"] num, ident = is_recent_str.split() max_search_days = 0 if ident == "days": max_search_days = int(num) elif ident == "weeks": max_search_days = int(num) * 7 subtitle_storage = get_subtitle_storage() recent_sub_fns = subtitle_storage.get_recent_files( age_days=max_search_days) viable_items = {} # determine viable items for fn in recent_sub_fns: # added_date <= max_search_days? stored_subs = subtitle_storage.load(filename=fn) if not stored_subs: continue if stored_subs.added_at + datetime.timedelta( days=max_search_days) <= now: continue viable_items[fn] = stored_subs subtitle_storage.destroy() self.items_searching = len(viable_items) download_count = 0 videos_with_downloads = 0 config.init_subliminal_patches() Log.Info(u"%s: Searching for subtitles for %s items", self.name, self.items_searching) # search for subtitles in viable items for fn, stored_subs in viable_items.iteritems(): video_id = stored_subs.video_id if stored_subs.item_type == "episode": min_score = min_score_series else: min_score = min_score_movies parts = [] plex_item = get_item(video_id) if not plex_item: Log.Info(u"%s: Item %s unknown, skipping", self.name, video_id) continue if is_ignored(video_id, item=plex_item): continue for media in plex_item.media: parts += media.parts downloads_per_video = 0 hit_providers = False for part in parts: part_id = part.id try: metadata = get_plex_metadata(video_id, part_id, stored_subs.item_type) except PartUnknownException: Log.Info(u"%s: Part %s:%s unknown, skipping", self.name, video_id, part_id) continue if not metadata: Log.Info(u"%s: Part %s:%s unknown, skipping", self.name, video_id, part_id) continue Log.Debug(u"%s: Looking for missing subtitles: %s:%s", self.name, video_id, part_id) scanned_parts = scan_videos( [metadata], kind="series" if stored_subs.item_type == "episode" else "movie") downloaded_subtitles = download_best_subtitles( scanned_parts, min_score=min_score) hit_providers = downloaded_subtitles is not None download_successful = False if downloaded_subtitles: downloaded_any = any(downloaded_subtitles.values()) if not downloaded_any: continue try: save_subtitles(scanned_parts, downloaded_subtitles, mode="a", mods=config.default_mods) Log.Debug( u"%s: Downloaded subtitle for item with missing subs: %s", self.name, video_id) download_successful = True refresh_item(video_id) 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: item_title = get_title_for_video_metadata( metadata, add_section_title=False) if download_successful: # store item in history for video, video_subtitles in downloaded_subtitles.items( ): if not video_subtitles: continue for subtitle in video_subtitles: downloads_per_video += 1 history.add( item_title, video.id, section_title=metadata["section"], subtitle=subtitle, mode="a") Log.Debug(u"%s: Waiting %s seconds before continuing", self.name, PROVIDER_SLACK) time.sleep(PROVIDER_SLACK) download_count += downloads_per_video if downloads_per_video: videos_with_downloads += 1 self.items_done = self.items_done + 1 self.percentage = int(self.items_done * 100 / self.items_searching) if downloads_per_video: Log.Debug( u"%s: Subtitles have been downloaded, " u"waiting %s seconds before continuing", self.name, DL_PROVIDER_SLACK) time.sleep(DL_PROVIDER_SLACK) else: if hit_providers: Log.Debug(u"%s: Waiting %s seconds before continuing", self.name, PROVIDER_SLACK) time.sleep(PROVIDER_SLACK) if download_count: Log.Debug( u"%s: done. Missing subtitles found for %s/%s items (%s subs downloaded)", self.name, videos_with_downloads, self.items_searching, download_count) else: Log.Debug(u"%s: done. No subtitles found for %s items", self.name, self.items_searching)
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) 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.Warning( "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: 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)