def refine_from_db(path, video): if isinstance(video, Episode): db = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30) c = db.cursor() data = c.execute( "SELECT table_shows.title, table_episodes.season, table_episodes.episode, table_episodes.title, table_shows.year, table_shows.tvdbId, table_shows.alternateTitles, table_episodes.format, table_episodes.resolution, table_episodes.video_codec, table_episodes.audio_codec FROM table_episodes INNER JOIN table_shows on table_shows.sonarrSeriesId = table_episodes.sonarrSeriesId WHERE table_episodes.path = ?", (unicode(path_replace_reverse(path)), )).fetchone() db.close() if data: video.series, year, country = series_re.match(data[0]).groups() video.season = int(data[1]) video.episode = int(data[2]) video.title = data[3] if data[4]: if int(data[4]) > 0: video.year = int(data[4]) video.series_tvdb_id = int(data[5]) video.alternative_series = ast.literal_eval(data[6]) if not video.format: video.format = str(data[7]) if not video.resolution: video.resolution = str(data[8]) if not video.video_codec: if data[9]: video.video_codec = data[9] if not video.audio_codec: if data[10]: video.audio_codec = data[10] elif isinstance(video, Movie): db = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30) c = db.cursor() data = c.execute( "SELECT title, year, alternativeTitles, format, resolution, video_codec, audio_codec, imdbId FROM table_movies WHERE path = ?", (unicode(path_replace_reverse_movie(path)), )).fetchone() db.close() if data: video.title = re.sub(r'(\(\d\d\d\d\))', '', data[0]) if data[1]: if int(data[1]) > 0: video.year = int(data[1]) if data[7]: video.imdb_id = data[7] video.alternative_titles = ast.literal_eval(data[2]) if not video.format: if data[3]: video.format = data[3] if not video.resolution: if data[4]: video.resolution = data[4] if not video.video_codec: if data[5]: video.video_codec = data[5] if not video.audio_codec: if data[6]: video.audio_codec = data[6] return video
def refine(video, **kwargs): """Refine a video by searching `TheTVDB <http://thetvdb.com/>`_. .. note:: This refiner only work for instances of :class:`~subliminal.video.Episode`. Several attributes can be found: * :attr:`~subliminal.video.Episode.series` * :attr:`~subliminal.video.Episode.year` * :attr:`~subliminal.video.Episode.series_imdb_id` * :attr:`~subliminal.video.Episode.series_tvdb_id` * :attr:`~subliminal.video.Episode.title` * :attr:`~subliminal.video.Video.imdb_id` * :attr:`~subliminal.video.Episode.tvdb_id` """ # only deal with Episode videos if not isinstance(video, Episode): logger.error('Can only refine episodes') return # exit if the information is complete if video.series_tvdb_id and video.tvdb_id: logger.debug('No need to search') return # search the series logger.info('Searching series %r', video.series) results = search_series(video.series.lower()) if not results: logger.warning('No results for series') return logger.debug('Found %d results', len(results)) # search for exact matches matching_results = [] for result in results: matching_result = {} # use seriesName and aliases series_names = [result['seriesName']] series_names.extend(result['aliases']) # parse the original series as series + year or country original_match = series_re.match(result['seriesName']).groupdict() # parse series year series_year = None if result['firstAired']: series_year = datetime.datetime.strptime(result['firstAired'], '%Y-%m-%d').year # discard mismatches on year if video.year and series_year and video.year != series_year: logger.debug('Discarding series %r mismatch on year %d', result['seriesName'], series_year) continue # iterate over series names for series_name in series_names: # parse as series and year series, year, country = series_re.match(series_name).groups() if year: year = int(year) # discard mismatches on year if year and (video.original_series or video.year != year): logger.debug('Discarding series name %r mismatch on year %d', series, year) continue # match on sanitized series name if sanitize(series) == sanitize(video.series): logger.debug('Found exact match on series %r', series_name) matching_result['match'] = { 'series': original_match['series'], 'year': series_year, 'original_series': original_match['year'] is None } break # add the result on match if matching_result: matching_result['data'] = result matching_results.append(matching_result) # exit if we don't have exactly 1 matching result if not matching_results: logger.warning('No matching series found') return if len(matching_results) > 1: logger.error('Multiple matches found') return # get the series matching_result = matching_results[0] series = get_series(matching_result['data']['id']) # add series information logger.debug('Found series %r', series) video.series = matching_result['match']['series'] video.alternative_series.extend(series['aliases']) video.year = matching_result['match']['year'] video.original_series = matching_result['match']['original_series'] video.series_tvdb_id = series['id'] video.series_imdb_id = series['imdbId'] or None # get the episode logger.info('Getting series episode %dx%d', video.season, video.episode) episode = get_series_episode(video.series_tvdb_id, video.season, video.episode) if not episode: logger.warning('No results for episode') return # add episode information logger.debug('Found episode %r', episode) video.tvdb_id = episode['id'] video.title = episode['episodeName'] or None video.imdb_id = episode['imdbId'] or None # get season fully aired information season_fully_aired = is_season_fully_aired(video.series_tvdb_id, video.season) if season_fully_aired is not None: video.season_fully_aired = season_fully_aired
def test_series_re_country(): groups = series_re.match("Series Name (UK)").groupdict() assert groups["series"] == "Series Name" assert groups["year"] is None assert groups["country"] == "UK"
def test_series_re_year_parenthesis(): groups = series_re.match("Series Name (2013)").groupdict() assert groups["series"] == "Series Name" assert groups["year"] == "2013" assert groups["country"] is None
def test_series_re_text_unclosed_parenthesis(): groups = series_re.match("Series Name (2013").groupdict() assert groups["series"] == "Series Name (2013" assert groups["year"] is None assert groups["country"] is None
def test_series_re_no_year(): groups = series_re.match("Series Name").groupdict() assert groups["series"] == "Series Name" assert groups["year"] is None
def test_series_re_country(): groups = series_re.match('Series Name (UK)').groupdict() assert groups['series'] == 'Series Name' assert groups['year'] is None assert groups['country'] == 'UK'
def test_series_re_text_unclosed_parenthesis(): groups = series_re.match('Series Name (2013').groupdict() assert groups['series'] == 'Series Name (2013' assert groups['year'] is None assert groups['country'] is None
def test_series_re_year_parenthesis(): groups = series_re.match('Series Name (2013)').groupdict() assert groups['series'] == 'Series Name' assert groups['year'] == '2013' assert groups['country'] is None
def test_series_re_no_year(): groups = series_re.match('Series Name').groupdict() assert groups['series'] == 'Series Name' assert groups['year'] is None