From 38fba77bcecf69e525e972cfaaa0c200a92ad6f4 Mon Sep 17 00:00:00 2001 From: meisnate12 Date: Tue, 2 Mar 2021 16:41:34 -0500 Subject: [PATCH] moved convert_from_imdb to config --- modules/anidb.py | 42 +++++----------------------------- modules/builder.py | 4 ++-- modules/config.py | 56 +++++++++++++++++++++++++++++++++++++++++++++- modules/imdb.py | 52 +++--------------------------------------- modules/tvdb.py | 31 +++---------------------- 5 files changed, 68 insertions(+), 117 deletions(-) diff --git a/modules/anidb.py b/modules/anidb.py index 5597b0e9..7e698d1a 100644 --- a/modules/anidb.py +++ b/modules/anidb.py @@ -62,7 +62,7 @@ class AniDBAPI: return anidb_values raise Failed(f"AniDB Error: No valid AniDB IDs in {anidb_list}") - def get_items(self, method, data, language, status_message=True): + def get_items(self, config, method, data, language, status_message=True): pretty = util.pretty_names[method] if method in util.pretty_names else method if status_message: logger.debug(f"Data: {data}") @@ -80,9 +80,10 @@ class AniDBAPI: movie_ids = [] for anidb_id in anime_ids: try: - tmdb_id = self.convert_from_imdb(self.convert_anidb_to_imdb(anidb_id)) - if tmdb_id: movie_ids.append(tmdb_id) - else: raise Failed + for imdb_id in self.convert_anidb_to_imdb(anidb_id): + tmdb_id, _ = config.convert_from_imdb(imdb_id, language) + if tmdb_id: movie_ids.append(tmdb_id) + else: raise Failed except Failed: try: show_ids.append(self.convert_anidb_to_tvdb(anidb_id)) except Failed: logger.error(f"AniDB Error: No TVDb ID or IMDb ID found for AniDB ID: {anidb_id}") @@ -91,36 +92,3 @@ class AniDBAPI: logger.debug(f"TMDb IDs Found: {movie_ids}") logger.debug(f"TVDb IDs Found: {show_ids}") return movie_ids, show_ids - - def convert_from_imdb(self, imdb_id): - output_tmdb_ids = [] - if not isinstance(imdb_id, list): - imdb_id = [imdb_id] - - for imdb in imdb_id: - expired = False - if self.Cache: - tmdb_id, tvdb_id = self.Cache.get_ids_from_imdb(imdb) - if not tmdb_id: - tmdb_id, expired = self.Cache.get_tmdb_from_imdb(imdb) - if expired: - tmdb_id = None - else: - tmdb_id = None - from_cache = tmdb_id is not None - - if not tmdb_id and self.TMDb: - try: tmdb_id = self.TMDb.convert_imdb_to_tmdb(imdb) - except Failed: pass - if not tmdb_id and self.Trakt: - try: tmdb_id = self.Trakt.convert_imdb_to_tmdb(imdb) - except Failed: pass - try: - if tmdb_id and not from_cache: self.TMDb.get_movie(tmdb_id) - except Failed: tmdb_id = None - if tmdb_id: output_tmdb_ids.append(tmdb_id) - if self.Cache and tmdb_id and expired is not False: - self.Cache.update_imdb("movie", expired, imdb, tmdb_id) - if len(output_tmdb_ids) == 0: raise Failed(f"AniDB Error: No TMDb ID found for IMDb: {imdb_id}") - elif len(output_tmdb_ids) == 1: return output_tmdb_ids[0] - else: return output_tmdb_ids diff --git a/modules/builder.py b/modules/builder.py index e4082b85..76ba01b5 100644 --- a/modules/builder.py +++ b/modules/builder.py @@ -644,10 +644,10 @@ class CollectionBuilder: elif "tautulli" in method: items = self.library.Tautulli.get_items(self.library, time_range=value["list_days"], stats_count=value["list_size"], list_type=value["list_type"], stats_count_buffer=value["list_buffer"]) items_found += len(items) - elif "anidb" in method: items_found += check_map(self.config.AniDB.get_items(method, value, self.library.Plex.language)) + elif "anidb" in method: items_found += check_map(self.config.AniDB.get_items(self.config, method, value, self.library.Plex.language)) elif "mal" in method: items_found += check_map(self.config.MyAnimeList.get_items(method, value)) elif "tvdb" in method: items_found += check_map(self.config.TVDb.get_items(method, value, self.library.Plex.language)) - elif "imdb" in method: items_found += check_map(self.config.IMDb.get_items(method, value, self.library.Plex.language)) + elif "imdb" in method: items_found += check_map(self.config.IMDb.get_items(self.config, method, value, self.library.Plex.language)) elif "tmdb" in method: items_found += check_map(self.config.TMDb.get_items(method, value, self.library.is_movie)) elif "trakt" in method: items_found += check_map(self.config.Trakt.get_items(method, value, self.library.is_movie)) else: logger.error(f"Collection Error: {method} method not supported") diff --git a/modules/config.py b/modules/config.py index ccedad5d..48f7a1b5 100644 --- a/modules/config.py +++ b/modules/config.py @@ -205,7 +205,7 @@ class Config: else: logger.warning("mal attribute not found") - self.TVDb = TVDbAPI(Cache=self.Cache, TMDb=self.TMDb, Trakt=self.Trakt) + self.TVDb = TVDbAPI(self, Cache=self.Cache, TMDb=self.TMDb, Trakt=self.Trakt) self.IMDb = IMDbAPI(Cache=self.Cache, TMDb=self.TMDb, Trakt=self.Trakt, TVDb=self.TVDb) if self.TMDb or self.Trakt else None self.AniDB = AniDBAPI(Cache=self.Cache, TMDb=self.TMDb, Trakt=self.Trakt) @@ -477,6 +477,60 @@ class Config: continue builder.run_collections_again(library, collection_obj, movie_map, show_map) + def convert_from_imdb(self, imdb_id, language): + update_tmdb = False + update_tvdb = False + if self.Cache: + tmdb_id, tvdb_id = self.Cache.get_ids_from_imdb(imdb_id) + update_tmdb = False + if not tmdb_id: + tmdb_id, update_tmdb = self.Cache.get_tmdb_from_imdb(imdb_id) + if update_tmdb: + tmdb_id = None + update_tvdb = False + if not tvdb_id: + tvdb_id, update_tvdb = self.Cache.get_tvdb_from_imdb(imdb_id) + if update_tvdb: + tvdb_id = None + else: + tmdb_id = None + tvdb_id = None + from_cache = tmdb_id is not None or tvdb_id is not None + + if not tmdb_id and not tvdb_id and self.TMDb: + try: + tmdb_id = self.TMDb.convert_imdb_to_tmdb(imdb_id) + except Failed: + pass + if not tmdb_id and not tvdb_id and self.TMDb: + try: + tvdb_id = self.TMDb.convert_imdb_to_tvdb(imdb_id) + except Failed: + pass + if not tmdb_id and not tvdb_id and self.Trakt: + try: + tmdb_id = self.Trakt.convert_imdb_to_tmdb(imdb_id) + except Failed: + pass + if not tmdb_id and not tvdb_id and self.Trakt: + try: + tvdb_id = self.Trakt.convert_imdb_to_tvdb(imdb_id) + except Failed: + pass + try: + if tmdb_id and not from_cache: self.TMDb.get_movie(tmdb_id) + except Failed: tmdb_id = None + try: + if tvdb_id and not from_cache: self.TVDb.get_series(language, tvdb_id=tvdb_id) + except Failed: tvdb_id = None + if not tmdb_id and not tvdb_id: raise Failed(f"IMDb Error: No TMDb ID or TVDb ID found for IMDb: {imdb_id}") + if self.Cache: + if tmdb_id and update_tmdb is not False: + self.Cache.update_imdb("movie", update_tmdb, imdb_id, tmdb_id) + if tvdb_id and update_tvdb is not False: + self.Cache.update_imdb("show", update_tvdb, imdb_id, tvdb_id) + return tmdb_id, tvdb_id + def map_guids(self, library): movie_map = {} show_map = {} diff --git a/modules/imdb.py b/modules/imdb.py index ba17d336..402e3d4d 100644 --- a/modules/imdb.py +++ b/modules/imdb.py @@ -52,7 +52,7 @@ class IMDbAPI: def send_request(self, url, header): return html.fromstring(requests.get(url, headers=header).content) - def get_items(self, method, data, language, status_message=True): + def get_items(self, config, method, data, language, status_message=True): pretty = util.pretty_names[method] if method in util.pretty_names else method if status_message: logger.debug(f"Data: {data}") @@ -61,7 +61,7 @@ class IMDbAPI: if method == "imdb_id": if status_message: logger.info(f"Processing {pretty}: {data}") - tmdb_id, tvdb_id = self.convert_from_imdb(data, language) + tmdb_id, tvdb_id = config.convert_from_imdb(data, language) if tmdb_id: movie_ids.append(tmdb_id) if tvdb_id: show_ids.append(tvdb_id) elif method == "imdb_list": @@ -74,7 +74,7 @@ class IMDbAPI: for i, imdb_id in enumerate(imdb_ids, 1): length = util.print_return(length, f"Converting IMDb ID {i}/{total_ids}") try: - tmdb_id, tvdb_id = self.convert_from_imdb(imdb_id, language) + tmdb_id, tvdb_id = config.convert_from_imdb(imdb_id, language) if tmdb_id: movie_ids.append(tmdb_id) if tvdb_id: show_ids.append(tvdb_id) except Failed as e: logger.warning(e) @@ -85,49 +85,3 @@ class IMDbAPI: logger.debug(f"TMDb IDs Found: {movie_ids}") logger.debug(f"TVDb IDs Found: {show_ids}") return movie_ids, show_ids - - def convert_from_imdb(self, imdb_id, language): - update_tmdb = False - update_tvdb = False - if self.Cache: - tmdb_id, tvdb_id = self.Cache.get_ids_from_imdb(imdb_id) - update_tmdb = False - if not tmdb_id: - tmdb_id, update_tmdb = self.Cache.get_tmdb_from_imdb(imdb_id) - if update_tmdb: - tmdb_id = None - update_tvdb = False - if not tvdb_id: - tvdb_id, update_tvdb = self.Cache.get_tvdb_from_imdb(imdb_id) - if update_tvdb: - tvdb_id = None - else: - tmdb_id = None - tvdb_id = None - from_cache = tmdb_id is not None or tvdb_id is not None - - if not tmdb_id and not tvdb_id and self.TMDb: - try: tmdb_id = self.TMDb.convert_imdb_to_tmdb(imdb_id) - except Failed: pass - if not tmdb_id and not tvdb_id and self.TMDb: - try: tvdb_id = self.TMDb.convert_imdb_to_tvdb(imdb_id) - except Failed: pass - if not tmdb_id and not tvdb_id and self.Trakt: - try: tmdb_id = self.Trakt.convert_imdb_to_tmdb(imdb_id) - except Failed: pass - if not tmdb_id and not tvdb_id and self.Trakt: - try: tvdb_id = self.Trakt.convert_imdb_to_tvdb(imdb_id) - except Failed: pass - try: - if tmdb_id and not from_cache: self.TMDb.get_movie(tmdb_id) - except Failed: tmdb_id = None - try: - if tvdb_id and not from_cache: self.TVDb.get_series(language, tvdb_id=tvdb_id) - except Failed: tvdb_id = None - if not tmdb_id and not tvdb_id: raise Failed(f"IMDb Error: No TMDb ID or TVDb ID found for IMDb: {imdb_id}") - if self.Cache: - if tmdb_id and update_tmdb is not False: - self.Cache.update_imdb("movie", update_tmdb, imdb_id, tmdb_id) - if tvdb_id and update_tvdb is not False: - self.Cache.update_imdb("show", update_tvdb, imdb_id, tvdb_id) - return tmdb_id, tvdb_id diff --git a/modules/tvdb.py b/modules/tvdb.py index b635d859..17a64ecc 100644 --- a/modules/tvdb.py +++ b/modules/tvdb.py @@ -45,7 +45,7 @@ class TVDbObj: if not tmdb_id: results = response.xpath("//*[text()='IMDB']/@href") if len(results) > 0: - try: tmdb_id = TVDb.convert_from_imdb(util.get_id_from_imdb_url(results[0])) + try: tmdb_id, _ = TVDb.config.convert_from_imdb(util.get_id_from_imdb_url(results[0])) except Failed as e: logger.error(e) self.tmdb_id = tmdb_id self.tvdb_url = tvdb_url @@ -54,7 +54,8 @@ class TVDbObj: self.TVDb = TVDb class TVDbAPI: - def __init__(self, Cache=None, TMDb=None, Trakt=None): + def __init__(self, config, Cache=None, TMDb=None, Trakt=None): + self.config = config self.Cache = Cache self.TMDb = TMDb self.Trakt = Trakt @@ -140,29 +141,3 @@ class TVDbAPI: logger.debug(f"TMDb IDs Found: {movie_ids}") logger.debug(f"TVDb IDs Found: {show_ids}") return movie_ids, show_ids - - def convert_from_imdb(self, imdb_id): - update = False - if self.Cache: - tmdb_id, tvdb_id = self.Cache.get_ids_from_imdb(imdb_id) - if not tmdb_id: - tmdb_id, update = self.Cache.get_tmdb_from_imdb(imdb_id) - if update: - tmdb_id = None - else: - tmdb_id = None - from_cache = tmdb_id is not None - - if not tmdb_id and self.TMDb: - try: tmdb_id = self.TMDb.convert_imdb_to_tmdb(imdb_id) - except Failed: pass - if not tmdb_id and self.Trakt: - try: tmdb_id = self.Trakt.convert_imdb_to_tmdb(imdb_id) - except Failed: pass - try: - if tmdb_id and not from_cache: self.TMDb.get_movie(tmdb_id) - except Failed: tmdb_id = None - if not tmdb_id: raise Failed(f"TVDb Error: No TMDb ID found for IMDb: {imdb_id}") - if self.Cache and tmdb_id and update is not False: - self.Cache.update_imdb("movie", update, imdb_id, tmdb_id) - return tmdb_id