Skip to content

Commit

Permalink
feature: move season and episode numbering to own RPC field
Browse files Browse the repository at this point in the history
  • Loading branch information
zeroquinc committed Sep 29, 2024
1 parent f0f129b commit ecaa495
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 12 deletions.
4 changes: 3 additions & 1 deletion src/imdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ def get_imdb_id_from_tv_show_details(tv_show_id):
return tv_show_response.get('result', {}).get('tvshowdetails', {}).get('uniqueid', {}).get('imdb', None)

def get_imdb_id_for_movie(info):
return info['uniqueid']['imdb']
if 'uniqueid' in info and 'imdb' in info['uniqueid']:
return info['uniqueid']['imdb']
return None

def get_imdb_url(imdb_id):
url = f"https://www.imdb.com/title/{imdb_id}/" if imdb_id else None
Expand Down
20 changes: 13 additions & 7 deletions src/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,20 @@ def update_rpc(info, start_time, end_time, image_url, imdb_url, tmdb_url, trakt_

buttons = create_buttons(imdb_url, letterboxd_url, tmdb_url, trakt_url)

# Define large text based on media type
if media_type == 'episode' and 'season' in info and 'episode' in info:
season_number = str(info['season']).zfill(2)
episode_number = str(info['episode']).zfill(2)
large_text = f"Season {season_number}, Episode {episode_number}"
else:
large_text = large_text_map.get(media_type.lower(), "Default Large Text")

rpc_params = {
"activity_type": ActivityType.WATCHING,
"details": str(info['label']) if media_type == 'channel' else (str(info['title']) + ' (' + str(info['year']) + ')' if media_type == 'movie' else str(info['showtitle']) if media_type == 'episode' else str(info['title'])),
"state": str(info['title']) if (media_type == 'channel' and info['title']) else ("Playing" if is_playing else "Paused"),
"large_image": image_url,
"large_text": large_text_map.get(media_type.lower(), "Default Large Text"),
"large_text": large_text,
#"small_image": 'play' if is_playing else 'pause',
#"small_text": 'Playing' if is_playing else 'Paused'
}
Expand All @@ -215,12 +223,7 @@ def update_rpc(info, start_time, end_time, image_url, imdb_url, tmdb_url, trakt_
rpc_params["end"] = end_time

if media_type == 'episode':
if 'season' in info and info['season'] and 'episode' in info and info['episode']:
season_number = str(info['season']).zfill(2)
episode_number = str(info['episode']).zfill(2)
rpc_params["state"] = f'S{season_number}E{episode_number}: {info["title"]}'
else:
rpc_params["state"] = info["title"]
rpc_params["state"] = info["title"]

if media_type == 'movie' and is_playing:
if DIRECTOR_ENABLED and 'director' in info and info['director'] is not None and GENRES_ENABLED is False:
Expand All @@ -229,8 +232,11 @@ def update_rpc(info, start_time, end_time, image_url, imdb_url, tmdb_url, trakt_
if GENRES_ENABLED and 'genre' in info and info['genre'] is not None and DIRECTOR_ENABLED is False:
genres = ', '.join(info['genre'])
rpc_params["state"] = f"{genres}"
else:
rpc_params["state"] = info["title"]

RPC.update(**rpc_params)


"""
Start and end time calculations, button creation, URL fetching
Expand Down
37 changes: 33 additions & 4 deletions src/tmdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def get_tmdb_id_for_tv_show(info):

# If tv_show_id is -1, search for the TMDb ID using show_title
if tv_show_id == -1 and show_title:
return search_tmdb_by_title(show_title)
return search_tmdb_by_showtitle(show_title)

# If tv_show_id is valid, get TMDb ID from the details
return get_tmdb_id_from_tv_show_details(tv_show_id)
Expand All @@ -52,7 +52,7 @@ def make_api_request(url):
logger.debug(f"API Response: {response}")
return response

def search_tmdb_by_title(show_title):
def search_tmdb_by_showtitle(show_title):
search_url = f"https://api.themoviedb.org/3/search/tv?api_key={TMDB_API_KEY}&query={show_title}"
response = make_api_request(search_url)

Expand All @@ -75,7 +75,36 @@ def get_tmdb_id_from_tv_show_details(tv_show_id):
return response.get('result', {}).get('tvshowdetails', {}).get('uniqueid', {}).get('tmdb')

def get_tmdb_id_for_media(info):
return info.get('uniqueid', {}).get('tmdb')
# Try to get the TMDb ID from the uniqueid field
tmdb_id = info.get('uniqueid', {}).get('tmdb')

# If TMDb ID is not found, extract the title and search TMDb
if not tmdb_id:
title = info.get('title') # Extract the title from info
if title:
logger.debug(f"TMDb ID not found for media. Searching TMDb using title: {title}")
tmdb_id = search_tmdb_by_movietitle(title)
else:
logger.debug("No title found to search TMDb.")

return tmdb_id

def search_tmdb_by_movietitle(title):
search_url = f"https://api.themoviedb.org/3/search/movie?api_key={TMDB_API_KEY}&query={title}"
response = make_api_request(search_url)

# Log the search response
logger.debug(f"Search Response for title '{title}': {response}")

if response.get('results'):
# Assuming you want the first result
show_info = response['results'][0]
tmdb_id = show_info.get('id')
logger.debug(f"Found TMDb ID for title '{title}': {tmdb_id}")
return tmdb_id

logger.debug(f"No results found for title '{title}'")
return None

def get_image_url(tmdb_id, media_type):
return get_image_url_from_tmdb(tmdb_id, media_type) if tmdb_id else DEFAULT_POSTER_URL
Expand All @@ -86,4 +115,4 @@ def get_image_url_from_tmdb(tmdb_id, media_type):
return f"https://image.tmdb.org/t/p/w185{response['poster_path']}" if 'poster_path' in response and response['poster_path'] else DEFAULT_POSTER_URL

def get_tmdb_url(tmdb_id, media_type):
return f"https://www.themoviedb.org/{media_type}/{tmdb_id}" if tmdb_id else None
return f"https://www.themoviedb.org/{media_type}/{tmdb_id}" if tmdb_id else None

0 comments on commit ecaa495

Please sign in to comment.