From 12479c2d70562c0bef22cab60f3fba7b6512c200 Mon Sep 17 00:00:00 2001 From: desimaniac <5501908+desimaniac@users.noreply.github.com> Date: Fri, 27 Sep 2019 00:29:20 -0500 Subject: [PATCH] TVDB: Check to see if series exists on TVDB before adding it. --- helpers/trakt.py | 20 +------------------- helpers/tvdb.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ traktarr.py | 5 +++++ 3 files changed, 53 insertions(+), 19 deletions(-) create mode 100644 helpers/tvdb.py diff --git a/helpers/trakt.py b/helpers/trakt.py index b8aa729..9a7048a 100644 --- a/helpers/trakt.py +++ b/helpers/trakt.py @@ -20,14 +20,10 @@ def blacklisted_show_id(show, blacklisted_ids): blacklisted = False blacklisted_ids = sorted(map(int, blacklisted_ids)) try: - if not show['show']['ids']['tvdb'] or not isinstance(show['show']['ids']['tvdb'], int): - log.debug("\'%s\' was blacklisted because it had an invalid TVDB ID", show['show']['title']) - blacklisted = True - elif show['show']['ids']['tvdb'] in blacklisted_ids: + if show['show']['ids']['tvdb'] in blacklisted_ids: log.debug("\'%s\' was blacklisted because it had a blacklisted TVDB ID of: %d", show['show']['title'], show['show']['ids']['tvdb']) blacklisted = True - except Exception: log.exception("Exception determining if show had a blacklisted TVDB ID %s: ", show) return blacklisted @@ -62,7 +58,6 @@ def blacklisted_show_network(show, networks): show['show']['network']) blacklisted = True break - except Exception: log.exception("Exception determining if show is from a blacklisted network %s: ", show) return blacklisted @@ -86,7 +81,6 @@ def blacklisted_show_country(show, allowed_countries): log.debug("\'%s\' was blacklisted because it's from the country: %s", show['show']['title'], show['show']['country'].upper()) blacklisted = True - except Exception: log.exception("Exception determining if show was from an allowed country %s: ", show) return blacklisted @@ -110,7 +104,6 @@ def blacklisted_show_language(show, allowed_languages): log.debug("\'%s\' was blacklisted because it's in the language: %s", show['show']['title'], show['show']['language'].upper()) blacklisted = True - except Exception: log.exception("Exception determining what language the show was in %s: ", show) return blacklisted @@ -136,7 +129,6 @@ def blacklisted_show_genre(show, genres): genre.title()) blacklisted = True break - except Exception: log.exception("Exception determining if show has a blacklisted genre %s: ", show) return blacklisted @@ -152,7 +144,6 @@ def blacklisted_show_runtime(show, lowest_runtime): log.debug("\'%s\' was blacklisted because it had a runtime of: %d", show['show']['title'], show['show']['runtime']) blacklisted = True - except Exception: log.exception("Exception determining if show had sufficient runtime %s: ", show) return blacklisted @@ -179,10 +170,8 @@ def is_show_blacklisted(show, blacklist_settings, ignore_blacklist, callback=Non blacklisted = True if blacklisted_show_runtime(show, blacklist_settings.blacklisted_min_runtime): blacklisted = True - if blacklisted and callback: callback('show', show) - except Exception: log.exception("Exception determining if show was blacklisted %s: ", show) return blacklisted @@ -196,7 +185,6 @@ def blacklisted_movie_id(movie, blacklisted_ids): log.debug("\'%s\' was blacklisted because it had a blacklisted TMDb ID of: %d", movie['movie']['title'], movie['movie']['ids']['tmdb']) blacklisted = True - except Exception: log.exception("Exception determining if movie had a blacklisted TMDb ID %s: ", movie) return blacklisted @@ -215,7 +203,6 @@ def blacklisted_movie_title(movie, blacklisted_keywords): keyword) blacklisted = True break - except Exception: log.exception("Exception determining if movie had a blacklisted title %s: ", movie) return blacklisted @@ -278,7 +265,6 @@ def blacklisted_movie_language(movie, allowed_languages): log.debug("\'%s\' was blacklisted because it's in the language: %s", movie['movie']['title'], movie['movie']['language'].upper()) blacklisted = True - except Exception: log.exception("Exception determining what language the movie was %s: ", movie) return blacklisted @@ -304,7 +290,6 @@ def blacklisted_movie_genre(movie, genres): genre.title()) blacklisted = True break - except Exception: log.exception("Exception determining if movie has a blacklisted genre %s: ", movie) return blacklisted @@ -320,7 +305,6 @@ def blacklisted_movie_runtime(movie, lowest_runtime): log.debug("\'%s\' was blacklisted because it had a runtime of: %d", movie['movie']['title'], movie['movie']['runtime']) blacklisted = True - except Exception: log.exception("Exception determining if movie had sufficient runtime %s: ", movie) return blacklisted @@ -347,10 +331,8 @@ def is_movie_blacklisted(movie, blacklist_settings, ignore_blacklist, callback=N blacklisted = True if blacklisted_movie_runtime(movie, blacklist_settings.blacklisted_min_runtime): blacklisted = True - if blacklisted and callback: callback('movie', movie) - except Exception: log.exception("Exception determining if movie was blacklisted %s: ", movie) return blacklisted diff --git a/helpers/tvdb.py b/helpers/tvdb.py new file mode 100644 index 0000000..54ab36f --- /dev/null +++ b/helpers/tvdb.py @@ -0,0 +1,47 @@ +from misc.log import logger +import requests + +log = logger.get_logger(__name__) + + +def validate_series_tvdb_id(series_title, series_year, series_tvdb_id): + try: + if not series_tvdb_id or not isinstance(series_tvdb_id, int): + log.debug("SKIPPING: \'%s (%s)\' blacklisted it has an invalid TVDB ID", series_title, series_year) + return False + else: + return True + except Exception: + log.exception("Exception validating TVDB ID for \'%s (%s)\'.", series_title, series_year) + return False + + +def verify_series_exists_on_tvdb(series_title, series_year, series_tvdb_id): + try: + req = requests.get('https://www.thetvdb.com/dereferrer/series/%s' % series_tvdb_id, allow_redirects=False) + if 'This record has either been deleted or has never existed.' not in req.text: + log.debug("\'%s (%s)\' [TVDB ID: %s] exists on TVDB.", series_title, series_year, series_tvdb_id) + return True + else: + log.debug("SKIPPING: \'%s (%s)\' [TVDB ID: %s] because it does not exist on TVDB.", series_title, + series_year, series_tvdb_id) + return False + except Exception: + log.exception("Exception verifying TVDB ID for \'%s (%s)\'.", series_title, series_year) + return False + + +def check_series_tvdb_id(series): + + series_title = series['show']['title'] + series_year = str(series['show']['year']) \ + if series['show']['year'] else '????' + series_tvdb_id = series['show']['ids']['tvdb'] + + try: + if validate_series_tvdb_id(series_title, series_year, series_tvdb_id) and \ + verify_series_exists_on_tvdb(series_title, series_year, series_tvdb_id): + return True + except Exception: + log.exception("Exception verifying/validating TVDB ID for \'%s (%s)\'.", series_title, series_year) + return False diff --git a/traktarr.py b/traktarr.py index 0074248..93c55c9 100755 --- a/traktarr.py +++ b/traktarr.py @@ -275,6 +275,7 @@ def shows(list_type, add_limit=0, add_delay=2.5, sort='votes', genre=None, folde from helpers import misc as misc_helper from helpers import sonarr as sonarr_helper from helpers import trakt as trakt_helper + from helpers import tvdb as tvdb_helper added_shows = 0 @@ -399,6 +400,10 @@ def shows(list_type, add_limit=0, add_delay=2.5, sort='votes', genre=None, folde series_genres = (', '.join(series['show']['genres'])).title() if series['show']['genres'] else 'N/A' try: + # check if movie has a valid TVDB ID and that it exists on TVDB + if not tvdb_helper.check_series_tvdb_id(series): + continue + # check if genre matches genre supplied via argument if genre and not misc_helper.allowed_genres(genre, 'show', series): log.debug("SKIPPING: \'%s\' because it was not from genre: %s", series['show']['title'],