Skip to content

Commit

Permalink
Refactor scene exception methods (#8753)
Browse files Browse the repository at this point in the history
* Update JS vue monorepo

* yarn dev

* Improve scene numbering

* Imp doc

* update tests

* up test

* up the test

* more opti

* flakeit

* flake

* fix stuff

* Fix mapping without scene numbers

* Reduce xem_refresh() usage and add ability to force xem refresh

* rm unused import

* leave no 0 behind

* 0 is not a valid abs number

Co-authored-by: Renovate Bot <[email protected]>
  • Loading branch information
medariox and renovate-bot authored Nov 19, 2020
1 parent 5b5a557 commit 29be151
Show file tree
Hide file tree
Showing 8 changed files with 278 additions and 224 deletions.
33 changes: 1 addition & 32 deletions medusa/helpers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

from medusa import app, db
from medusa.common import DOWNLOADED, USER_AGENT
from medusa.helper.common import (episode_num, http_code_description, media_extensions,
from medusa.helper.common import (http_code_description, media_extensions,
pretty_file_size, subtitle_extensions)
from medusa.helpers.utils import generate
from medusa.imdb import Imdb
Expand Down Expand Up @@ -741,37 +741,6 @@ def fix_set_group_id(child_path):
{'path': child_path, 'gid': parent_gid})


def get_absolute_number_from_season_and_episode(series_obj, season, episode):
"""Find the absolute number for a show episode.
:param show: Show object
:param season: Season number
:param episode: Episode number
:return: The absolute number
"""
absolute_number = None

if season and episode:
main_db_con = db.DBConnection()
sql = 'SELECT * FROM tv_episodes WHERE indexer = ? AND showid = ? AND season = ? AND episode = ?'
sql_results = main_db_con.select(sql, [series_obj.indexer, series_obj.series_id, season, episode])

if len(sql_results) == 1:
absolute_number = int(sql_results[0]['absolute_number'])
log.debug(
u'Found absolute number {absolute} for show {show} {ep}', {
'absolute': absolute_number,
'show': series_obj.name,
'ep': episode_num(season, episode),
}
)
else:
log.debug(u'No entries for absolute number for show {show} {ep}',
{'show': series_obj.name, 'ep': episode_num(season, episode)})

return absolute_number


def get_all_episodes_from_absolute_number(show, absolute_numbers, indexer_id=None, indexer=None):
episodes = []
season = None
Expand Down
102 changes: 57 additions & 45 deletions medusa/name_parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,18 +148,19 @@ def _parse_air_by_date(self, result):
season = season_number
episode = episode_number

if result.series.is_scene:
(season, episode) = scene_numbering.get_indexer_numbering(
result.series,
season_number,
episode_number,
)
log.debug(
'Scene numbering enabled series {name}, using indexer numbering: {ep}',
{'name': result.series.name, 'ep': episode_num(season, episode)}
)
new_episode_numbers.append(episode)
(idx_season, idx_episode) = scene_numbering.get_indexer_numbering(
result.series,
episode_number,
season_number
)

if idx_season is not None:
season = idx_season
if idx_episode is not None:
episode = idx_episode

new_season_numbers.append(season)
new_episode_numbers.append(episode)

return new_episode_numbers, new_season_numbers

Expand Down Expand Up @@ -191,20 +192,22 @@ def _parse_anime(result):

if result.ab_episode_numbers:
for absolute_episode in result.ab_episode_numbers:
a = absolute_episode
abs_ep = absolute_episode

# Don't assume that scene_exceptions season is the same as indexer season.
# E.g.: [HorribleSubs] Cardcaptor Sakura Clear Card - 08 [720p].mkv thetvdb s04, thexem s02
if season_exception is not None or result.series.is_scene:
# Get absolute number from custom numbering (1), XEM (2) or indexer (3)
a = scene_numbering.get_indexer_absolute_numbering(
result.series, a, fallback_to_xem=True, scene_season=season_exception
idx_abs_ep = scene_numbering.get_indexer_abs_numbering(
result.series, abs_ep, season=season_exception
)
if idx_abs_ep is not None:
abs_ep = idx_abs_ep

new_absolute_numbers.append(a)
new_absolute_numbers.append(abs_ep)

# Translate the absolute episode number, back to the indexers season and episode.
(season, episodes) = helpers.get_all_episodes_from_absolute_number(result.series, [a])
(season, episodes) = helpers.get_all_episodes_from_absolute_number(result.series, [abs_ep])
if season and episodes:

new_episode_numbers.extend(episodes)
Expand All @@ -216,18 +219,19 @@ def _parse_anime(result):
'season number in the title, '
'translating the episode #{abs} to indexer #{indexer_absolute}: {ep}',
{'series_name': result.series_name, 'scene_season': season_exception,
'abs': absolute_episode, 'indexer_absolute': a, 'ep': episode_num(season, episodes[0])}
'abs': absolute_episode, 'indexer_absolute': abs_ep,
'ep': episode_num(season, episodes[0])}
)
elif result.series.is_scene:
log.debug(
'Scene numbering enabled anime series {name} using indexer numbering #{absolute}: {ep}',
{'name': result.series.name, 'season': season, 'absolute': a,
{'name': result.series.name, 'season': season, 'absolute': abs_ep,
'ep': episode_num(season, episodes[0])}
)
else:
log.debug(
'Anime series {name} using indexer numbering #{absolute}: {ep}',
{'name': result.series.name, 'season': season, 'absolute': a,
{'name': result.series.name, 'season': season, 'absolute': abs_ep,
'ep': episode_num(season, episodes[0])}
)

Expand All @@ -238,28 +242,35 @@ def _parse_anime(result):
season = result.season_number
episode = episode_number

idx_abs_ep = scene_numbering.get_indexer_abs_numbering(result.series, episode, season=season)
if idx_abs_ep is not None:
new_absolute_numbers.append(idx_abs_ep)

(idx_season, idx_episode) = scene_numbering.get_indexer_numbering(
result.series,
episode_number,
result.season_number
)

if idx_season is not None:
season = idx_season
if idx_episode is not None:
episode = idx_episode

new_season_numbers.append(season)
new_episode_numbers.append(episode)

if result.series.is_scene:
(season, episode) = scene_numbering.get_indexer_numbering(
result.series,
result.season_number,
episode_number
)
log.debug(
'Scene numbering enabled anime {name} using indexer numbering: {ep}',
{'name': result.series.name, 'ep': episode_num(season, episode)}
{'name': result.series.name, 'absolute': idx_abs_ep, 'ep': episode_num(season, episode)}
)

a = helpers.get_absolute_number_from_season_and_episode(result.series, season, episode)
if a:
new_absolute_numbers.append(a)
else:
log.debug(
'Anime series {name} using using indexer numbering #{absolute}: {ep}',
{'name': result.series.name, 'absolute': a, 'ep': episode_num(season, episode)}
{'name': result.series.name, 'absolute': idx_abs_ep, 'ep': episode_num(season, episode)}
)

new_episode_numbers.append(episode)
new_season_numbers.append(season)

return new_episode_numbers, new_season_numbers, new_absolute_numbers

@staticmethod
Expand All @@ -268,24 +279,25 @@ def _parse_series(result):
new_season_numbers = []
new_absolute_numbers = []

season = scene_exceptions.get_season_from_name(result.series, result.series_name) or result.season_number
ex_season = scene_exceptions.get_season_from_name(result.series, result.series_name) or result.season_number

for episode_number in result.episode_numbers:
season = ex_season
episode = episode_number

if result.series.is_scene:
(season, episode) = scene_numbering.get_indexer_numbering(
result.series,
season,
episode_number
)
log.debug(
'Scene numbering enabled series {name} using indexer numbering: {ep}',
{'name': result.series.name, 'ep': episode_num(season, episode)}
)
(idx_season, idx_episode) = scene_numbering.get_indexer_numbering(
result.series,
episode_number,
ex_season
)

if idx_season is not None:
season = idx_season
if idx_episode is not None:
episode = idx_episode

new_episode_numbers.append(episode)
new_season_numbers.append(season)
new_episode_numbers.append(episode)

return new_episode_numbers, new_season_numbers, new_absolute_numbers

Expand Down
2 changes: 1 addition & 1 deletion medusa/queues/show_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ def run(self):
self.show.populate_cache()

# Load XEM data to DB for show
scene_numbering.xem_refresh(self.show)
scene_numbering.xem_refresh(self.show, force=True)
except IndexerException as error:
log.warning(
'{id}: Unable to contact {indexer}. Aborting: {error_msg}',
Expand Down
Loading

0 comments on commit 29be151

Please sign in to comment.