From b5bbe3ae089639a56dd61904eefc3fafb88b62fb Mon Sep 17 00:00:00 2001 From: Ben Goldsworthy Date: Thu, 15 Feb 2024 10:22:03 +0000 Subject: [PATCH] add TV episode importing --- scripts/add_item.py | 55 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 49 insertions(+), 6 deletions(-) diff --git a/scripts/add_item.py b/scripts/add_item.py index e86d070..8547682 100644 --- a/scripts/add_item.py +++ b/scripts/add_item.py @@ -124,7 +124,10 @@ def add_item_to_log(item_id, media_type, log) -> None: logger.info(f"Processing {item_id}…") - item, log_to_delete = check_for_existing(item_id, media_type, log) + item = None + log_to_delete = None + if "tv-episodes" != media_type: + item, log_to_delete = check_for_existing(item_id, media_type, log) if item is None: item = import_by_id(item_id, media_type) @@ -193,7 +196,7 @@ def import_by_id(import_id, media_type) -> dict: return import_from_tmdb_by_id(import_id, media_type) if media_type in ["tv-episodes"]: - return # import_from_tvdb_by_id(import_id, media_type) + return import_from_tmdb_by_imdb_id(import_id, media_type) if media_type in ["books"]: return import_from_openlibrary_by_id( @@ -201,9 +204,52 @@ def import_by_id(import_id, media_type) -> dict: ) -def import_from_tmdb_by_id(tmdb_id, media_type) -> dict: +def import_from_tmdb_by_imdb_id(imdb_id, media_type) -> dict: """Retrieve a film, TV show or TV episode from TMDB using an IMDB ID""" + api_url = f"https://api.themoviedb.org/3/find/{imdb_id}" + + # Sending API request + response = requests.get( + api_url, + headers={"Authorization": f"Bearer {TMDB_API_KEY}"}, + params={"external_source": "imdb_id"}, + timeout=15 + ) + + # Process the response + if 200 == response.status_code: + logger.debug(response.status_code) + + elif 429 == response.status_code: + time.sleep(2) + return import_from_tmdb_by_imdb_id(imdb_id, media_type) + + else: + raise Exception(f"Error {response.status_code}: {response.text}") + + key = "" + if "tv-episodes" == media_type: + key = "tv_episode_results" + elif "tv-series" == media_type: + key = "tv_results" + elif "films" == media_type: + key = "movie_results" + + response_data = json.loads(response.text)[key][0] + if response_data == None: + raise Exception(f"Nothing found for IMDB ID {imdb_id}!") + + # Modify the returned result to add additional data + return cleanup_result(response_data, media_type) + + +def import_from_tmdb_by_id(tmdb_id, media_type) -> dict: + """Retrieve a film, TV show or TV episode from TMDB using an TMDB ID""" + + if "tv-episodes" == media_type: + raise Exception("TV Episodes are TODO!") + api_path = "movie" if "films" == media_type else "tv" api_url = f"https://api.themoviedb.org/3/{api_path}/{tmdb_id}" @@ -223,9 +269,6 @@ def import_from_tmdb_by_id(tmdb_id, media_type) -> dict: else: raise Exception(f"Error {response.status_code}: {response.text}") - if "tv-episodes" == media_type: - raise Exception("TV Episodes are TODO!") - response_data = json.loads(response.text) # Modify the returned result to add additional data