Skip to content

Commit

Permalink
fix: Ensure TMDb and IMDb IDs are attached when result is a tuple
Browse files Browse the repository at this point in the history
- Added logic to handle cases where the result is a tuple.
- fixes imdb/tmdb formatting issue by directly appending ID's to movie name

Addresses #36
  • Loading branch information
sureshfizzy committed Dec 15, 2024
1 parent 63e8412 commit 44b92ec
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
13 changes: 7 additions & 6 deletions MediaHub/api/tmdb_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from functools import lru_cache
import urllib.parse
from utils.logging_utils import log_message
from config.config import get_api_key, is_imdb_folder_id_enabled, is_tvdb_folder_id_enabled
from config.config import get_api_key, is_imdb_folder_id_enabled, is_tvdb_folder_id_enabled, is_tmdb_folder_id_enabled
from utils.file_utils import clean_query, normalize_query, standardize_title, remove_genre_names, extract_title, clean_query_movie

_api_cache = {}
Expand Down Expand Up @@ -320,17 +320,18 @@ def search_fallback(query, year=None):
release_date = chosen_movie.get('release_date')
movie_year = release_date.split('-')[0] if release_date else "Unknown Year"
tmdb_id = chosen_movie.get('id')
external_ids = get_external_ids(tmdb_id, 'movie')
imdb_id = external_ids.get('imdb_id', '')

if is_imdb_folder_id_enabled():
external_ids = get_external_ids(tmdb_id, 'movie')
imdb_id = external_ids.get('imdb_id', '')
log_message(f"Movie: {movie_name}, IMDB ID: {imdb_id}", level="INFO")
proper_name = f"{movie_name} ({movie_year}) {{imdb-{imdb_id}}}"
else:
elif is_tmdb_folder_id_enabled():
proper_name = f"{movie_name} ({movie_year}) {{tmdb-{tmdb_id}}}"
else:
proper_name = f"{movie_name} ({movie_year})"

_api_cache[cache_key] = proper_name
return proper_name
return tmdb_id, imdb_id, movie_name
else:
log_message(f"No valid selection made for query '{query}', skipping.", level="WARNING")
_api_cache[cache_key] = f"{query}"
Expand Down
27 changes: 17 additions & 10 deletions MediaHub/processors/movie_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,21 @@ def process_movie(src_file, root, file, dest_dir, actual_dir, tmdb_folder_id_ena

collection_info = None
api_key = get_api_key()

if api_key and is_movie_collection_enabled():
result = search_movie(movie_name, year, auto_select=auto_select, actual_dir=actual_dir, file=file)
if isinstance(result, tuple):
original_name, cleaned_title = result
movie_name = cleaned_title
proper_movie_name = f"{cleaned_title} ({year})"
elif isinstance(result, dict):
proper_movie_name = f"{result['title']} ({result.get('release_date', '').split('-')[0]})"
tmdb_id = result['id']
if isinstance(result, (tuple, dict)):
if isinstance(result, tuple):
tmdb_id, imdb_id, proper_name = result
elif isinstance(result, dict):
proper_name = result['title']
year = result.get('release_date', '').split('-')[0]
tmdb_id = result['id']

proper_movie_name = f"{proper_name} ({year})"
if is_tmdb_folder_id_enabled():
proper_movie_name += f" {{tmdb-{tmdb_id}}}"

tmdb_id_match = re.search(r'\{tmdb-(\d+)\}$', proper_movie_name)
if tmdb_id_match:
movie_id = tmdb_id_match.group(1)
Expand All @@ -98,9 +102,12 @@ def process_movie(src_file, root, file, dest_dir, actual_dir, tmdb_folder_id_ena
elif api_key:
result = search_movie(movie_name, year, auto_select=auto_select, file=file)
if isinstance(result, tuple):
original_name, cleaned_title = result
movie_name = cleaned_title
proper_movie_name = f"{cleaned_title} ({year})"
tmdb_id, imdb_id, proper_name = result
proper_movie_name = f"{movie_name} ({year})"
if is_tmdb_folder_id_enabled() and tmdb_id:
proper_movie_name += f" {{tmdb-{tmdb_id}}}"
if is_imdb_folder_id_enabled() and imdb_id:
proper_movie_name += f" {{imdb-{imdb_id}}}"
elif isinstance(result, dict):
proper_movie_name = f"{result['title']} ({result.get('release_date', '').split('-')[0]})"
if is_imdb_folder_id_enabled() and 'imdb_id' in result:
Expand Down

0 comments on commit 44b92ec

Please sign in to comment.