def ingest_path(self, file_path): file_name = os.path.basename(file_path) parser = self._get_parser(file_name) # match if parser.match: file_extension_match = parser.file_extension_regex.search(file_name) if file_extension_match: # skip sample files if parser.sample_file_regex.search(file_name): logger_background.warning('[NO_MATCH_SAMPLE] Not matching sample file "{}"'.format(file_path)) return False title = parser.match['title'] if not title: new_title, parser_match = self._handle_missing_title(parser, file_path) if new_title: title = new_title parser.match.update(parser_match) else: logger_background.warning('[NO_MATCH_TITLE] Could not match file without title "{}"'.format(file_path)) return False file_extension = file_extension_match.group() if file_extension in video_extensions(): if self._is_parser_exact_match(parser): if self.media_class.objects.filter(download_path=file_path).exists(): logger_background.info('[SKIP] skipping already-processed file "{}"'.format(file_path)) return False # get or set tmdb search results for this title in the cache tmdb_results = cache.get(title) if not tmdb_results: try: tmdb_results = self._get_tmdb_search_results(title) except HTTPError: logger_background.error('[ERROR_TMDB] tmdb search exception for title {} on file "{}"'.format(title, file_path)) return False cache.set(title, tmdb_results, 60 * 60) # loop over results for the exact match for tmdb_result in tmdb_results['results']: # normalize titles and see if they match if self._is_result_match_title(parser, tmdb_result, title): watch_media = self._handle_match(parser, tmdb_result, title, file_path) if watch_media: logger_background.info('[MATCH] Saved media "{}" from file "{}"'.format(watch_media, file_path)) return watch_media else: # for/else logger_background.warning('[NO_MATCH_MEDIA] No media match for title "{}" and file "{}"'.format(title, file_path)) else: logger_background.warning('[NO_MATCH_EXACT] No exact title match for title "{}" and file "{}"'.format(title, file_path)) else: logger_background.warning('[NO_MATCH_VIDEO] No valid video file extension for file "{}"'.format(file_path)) else: logger_background.warning('[NO_MATCH_EXTENSION] No file extension for file "{}"'.format(file_path)) else: logger_background.info('[NO_MATCH_UNKNOWN] Unknown match for file "{}"'.format(file_path)) return False
def _handle_match(self, parser, tmdb_result, title, file_path): poster_path = self.nefarious_settings.get_tmdb_poster_url( tmdb_result['poster_path']) if tmdb_result['poster_path'] else '' season_number = parser.match['season'][0] episode_number = parser.match['episode'][0] watch_show, _ = WatchTVShow.objects.update_or_create( tmdb_show_id=tmdb_result['id'], defaults=dict( user=self.user, name=tmdb_result['name'], poster_image_url=poster_path, ), ) episode_result = self.tmdb_client.TV_Episodes(tmdb_result['id'], season_number, episode_number) try: episode_data = episode_result.info() except HTTPError: logger_background.error( '[ERROR_TMDB] tmdb episode exception for title {} on file "{}"' .format(title, file_path)) return False watch_episode, _ = WatchTVEpisode.objects.update_or_create( watch_tv_show=watch_show, season_number=season_number, episode_number=episode_number, defaults=dict( tmdb_episode_id=episode_data['id'], user=self.user, download_path=file_path, collected=True, collected_date=timezone.utc.localize( timezone.datetime.utcnow()), release_date=parse_date(episode_data.get('air_date', '')), last_attempt_date=timezone.utc.localize( timezone.datetime.utcnow()), ), ) return watch_episode
def log_exception(**kwargs): logger_background.error('TASK EXCEPTION', exc_info=kwargs['exception'])
def wanted_tv_season_task(): nefarious_settings = NefariousSettings.get() tmdb = get_tmdb_client(nefarious_settings) # # re-check for requested tv seasons that have had new episodes released from TMDB (which was stale previously) # for tv_season_request in WatchTVSeasonRequest.objects.filter( collected=False): season_request = tmdb.TV_Seasons( tv_season_request.watch_tv_show.tmdb_show_id, tv_season_request.season_number) season = season_request.info() now = datetime.utcnow() last_air_date = parse_date(season.get('air_date') or '') # season air date # otherwise add any new episodes to our watch list for episode in season['episodes']: episode_air_date = parse_date(episode.get('air_date') or '') # if episode air date exists, use as last air date if episode_air_date: last_air_date = episode_air_date if not last_air_date or episode_air_date > last_air_date else last_air_date try: watch_tv_episode, was_created = WatchTVEpisode.objects.get_or_create( tmdb_episode_id=episode['id'], defaults=dict( watch_tv_show=tv_season_request.watch_tv_show, season_number=tv_season_request.season_number, episode_number=episode['episode_number'], user=tv_season_request.user, release_date=episode_air_date, )) except IntegrityError as e: logger_background.exception(e) logger_background.error( 'Failed creating tmdb episode {} when show {}, season #{} and episode #{} already exist' .format(episode['id'], tv_season_request.watch_tv_show.id, tv_season_request.season_number, episode['episode_number'])) continue if was_created: logger_background.info( 'adding newly found episode {} for {}'.format( episode['episode_number'], tv_season_request)) # queue task to watch episode watch_tv_episode_task.delay(watch_tv_episode.id) # assume there's no new episodes for anything that's aired this long ago days_since_aired = (now.date() - last_air_date).days if last_air_date else 0 if days_since_aired > 30: logger_background.warning( 'completing old tv season request {}'.format( tv_season_request)) tv_season_request.collected = True tv_season_request.save()