Skip to content

Commit

Permalink
helper: commands: extraids: allow to provide TV shows IMDB ID
Browse files Browse the repository at this point in the history
When resolving episodes, sometimes the first aired date returned by
TheTVDB is not matching the one of IMDB. In such situations, a TV
shows might be discarded even though it was the right one. This
thus allows to instead match using the IMDB ID even when the year
does not match.

Fixes #98

Signed-off-by: Raphaël Beamonte <raphael.beamonte@gmail.com>
  • Loading branch information
xaf committed Apr 20, 2018
1 parent 6c136e5 commit 6534119
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
41 changes: 23 additions & 18 deletions helper/commands/extraids.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

##############################################################################
# To resolve an episode ids
def resolve_episode_ids(series, season, episode, year=None):
def resolve_episode_ids(series, season, episode, year=None, imdbid=None):
# To store the IDs found
ids = {}

Expand All @@ -60,22 +60,24 @@ def resolve_episode_ids(series, season, episode, year=None):
# Try getting the series directly, but check the year if available
# as sometimes series have the same name but are from different years
tvdb_series = tvdb[series]
if year is not None and tvdb_series['firstAired'] and \
if (imdbid is None or tvdb_series['imdbId'] != imdbid) and \
year is not None and tvdb_series['firstAired'] and \
year != int(tvdb_series['firstAired'].split('-')[0]):
# It is not the expected year, we thus need to perform a search
tvdb_series = None
tvdb_search = tvdb.search(series)
for s in tvdb_search:
if not s['seriesName'].startswith(series):
LOGGER.debug('TVDB: Discarding result because of the name '
'not beginning with the expected series '
'name: {}'.format(s))
continue

if int(s['firstAired'].split('-')[0]) != year:
LOGGER.debug('TVDB: Discarding result because of the year '
'not matching: {}'.format(s))
continue
if imdbid is None or s['imdbId'] != imdbid:
if not s['seriesName'].startswith(series):
LOGGER.debug('TVDB: Discarding result because of the '
'name not beginning with the expected '
'series name: {}'.format(s))
continue

if int(s['firstAired'].split('-')[0]) != year:
LOGGER.debug('TVDB: Discarding result because of the '
'year not matching: {}'.format(s))
continue

tvdb_series = tvdb[s['seriesName']]
break
Expand Down Expand Up @@ -174,6 +176,7 @@ class Media(object):
episode = None
movie = None
year = None
imdbid = None


##############################################################################
Expand All @@ -186,12 +189,12 @@ def __init__(self, option_strings, dest, default=None,
required=required, help=help)

def __call__(self, parser, namespace, values, option_strings=None):
if len(values) < 3 or len(values) > 4:
if len(values) < 3 or len(values) > 5:
parser.error('argument {}: format is SERIES_NAME SEASON_NUMBER '
'EPISODE_NUMBER [YEAR]')
'EPISODE_NUMBER [YEAR [SERIES_IMDBID]]')
return

for i, v in enumerate(values[1:]):
for i, v in enumerate(values[1:-1]):
try:
values[i + 1] = int(v)
except ValueError:
Expand All @@ -203,8 +206,10 @@ def __call__(self, parser, namespace, values, option_strings=None):
media.series = values[0]
media.season = values[1]
media.episode = values[2]
if len(values) > 3:
if len(values) > 3 and values[3]:
media.year = values[3]
if len(values) > 4 and values[4]:
media.imdbid = values[4]

current = getattr(namespace, self.dest)
if current is None:
Expand Down Expand Up @@ -237,7 +242,7 @@ def __call__(self, parser, namespace, values, option_strings=None):

media = Media()
media.movie = values[0]
if len(values) > 1:
if len(values) > 1 and values[1]:
media.year = values[1]

current = getattr(namespace, self.dest)
Expand Down Expand Up @@ -280,7 +285,7 @@ def run(self, episodes, movies):

for e in episodes:
ep_ids = resolve_episode_ids(e.series, e.season,
e.episode, e.year)
e.episode, e.year, e.imdbid)

ids.setdefault(
'episode', {}).setdefault(
Expand Down
4 changes: 3 additions & 1 deletion helper/commands/resolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,9 @@ def insert_hash(media, ratio):
m_season = m['base']['season']
m_ep = m['base']['episode']
m_year = m['base']['parentTitle']['year']
ids = resolve_episode_ids(m_series, m_season, m_ep, m_year)
m_series_imdbid = m['base']['parentTitle']['id'][7:-1]
ids = resolve_episode_ids(m_series, m_season, m_ep, m_year,
m_series_imdbid)
else:
m_movie = m['base']['title']
m_year = m['base']['year']
Expand Down
7 changes: 6 additions & 1 deletion trakt.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2414,7 +2414,12 @@ function process_scrobble_ready()
table.insert(command, imdbinfo.parentTitle.title)
table.insert(command, tostring(imdbinfo.season))
table.insert(command, tostring(imdbinfo.episode))
table.insert(command, tostring(imdbinfo.parentTitle.year))
table.insert(
command,
tostring(imdbinfo.parentTitle.year))
table.insert(
command,
string.sub(imdbinfo.parentTitle.id, 8, -2))
else
table.insert(command, '--movie')
table.insert(command, imdbinfo.title)
Expand Down

0 comments on commit 6534119

Please sign in to comment.