Skip to content

Commit

Permalink
Merge pull request #6 from zeroquinc/dev
Browse files Browse the repository at this point in the history
v1.0.5
  • Loading branch information
zeroquinc authored Jan 24, 2024
2 parents 31e8e53 + 583543f commit 73b341c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 19 deletions.
50 changes: 47 additions & 3 deletions src/imdb.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,59 @@
import requests

from .custom_logger import logger
from .globals import port

"""
This file contains functions to get IMDb information about a media.
"""

# Get IMDb ID
def get_imdb_id(info):
imdb_id = info.get('uniqueid', {}).get('imdb')
# Function to get the media type of a media
def get_media_type(info):
media_type = info['type']
logger.debug(f"Media type: {media_type}")
# If the media type is an episode, return 'tv', this is necessary because the TMDB API uses 'tv' for TV shows and Kodi uses 'episode' for TV shows
return 'tv' if media_type == 'episode' else media_type

# Function to get the IMDb ID of a media
def get_imdb_id(info, media_type):
# If the media type is a channel, return None
if media_type == 'channel':
return None

imdb_id = None
if media_type == 'tv':
imdb_id = get_imdb_id_for_tv_show(info)
elif media_type == 'movie':
imdb_id = get_imdb_id_for_movie(info, 'movie')

logger.debug(f"IMDb ID: {imdb_id}")
return imdb_id

# Function to get the IMDb ID of a TV show
def get_imdb_id_for_tv_show(info):
tv_show_id = info['tvshowid']
# If tvshowid is not None or -1, fetch the IMDb ID from the TV show details
if tv_show_id != -1:
imdb_id = get_imdb_id_from_tv_show_details(tv_show_id)
else:
return None
return imdb_id

# Function to get the IMDb ID of a TV show via the Kodi JSONRPC API
def get_imdb_id_from_tv_show_details(tv_show_id):
tv_show_url = f"http://localhost:{port}/jsonrpc?request={{%22jsonrpc%22:%222.0%22,%22method%22:%22VideoLibrary.GetTVShowDetails%22,%22params%22:{{%22tvshowid%22:{tv_show_id},%22properties%22:[%22uniqueid%22]}},%22id%22:%22libTvShow%22}}"
tv_show_response = requests.get(tv_show_url).json()
logger.debug(f"Kodi JSONRPC search response: {tv_show_response}")
# Check if 'result' key exists in the response
if 'result' in tv_show_response and 'tvshowdetails' in tv_show_response['result'] and 'uniqueid' in tv_show_response['result']['tvshowdetails'] and 'tmdb' in tv_show_response['result']['tvshowdetails']['uniqueid']:
return tv_show_response['result']['tvshowdetails']['uniqueid']['imdb']
return None

# Function to get the TMDB ID of a TV show
def get_imdb_id_for_movie(info):
imdb_id = info['uniqueid']['imdb']
return imdb_id

# Get IMDb URL
def get_imdb_url(imdb_id):
url = f"https://www.imdb.com/title/{imdb_id}/" if imdb_id else None
Expand Down
10 changes: 7 additions & 3 deletions src/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,12 @@ def set_rp(info, length):
return
if previous_info == info and previous_speed == length['speed']: # Check if both info and speed are the same as before to prevent unnecessary updates
return
if info['type'] == 'unknown': # Check if the media type is unknown to prevent unnecessary updates
clear_rpc_if_unknown(info, length, start_time, end_time, None, None, None, None, None)
return

logger.debug(f"Retrieved info: {info}")

# Create a key for the cache
cache_key = json.dumps(info, sort_keys=True)

Expand Down Expand Up @@ -275,13 +280,12 @@ def get_urls(info, media_type):
tmdb_url = get_tmdb_url(tmdb_id, media_type)
image_url = get_image_url(tmdb_id, media_type)
if IMDB_BUTTON_ENABLED:
imdb_id = get_imdb_id(info)
imdb_id = get_imdb_id(info, media_type)
imdb_url = get_imdb_url(imdb_id)
if TMDB_BUTTON_ENABLED:
tmdb_url = get_tmdb_url(tmdb_id, media_type)
if TRAKT_BUTTON_ENABLED:
tmdb_id_trakt = get_tmdb_id_trakt(info, media_type)
trakt_url = get_trakt_url(tmdb_id_trakt, media_type)
trakt_url = get_trakt_url(tmdb_id, media_type)
if LETTERBOXD_BUTTON_ENABLED:
letterboxd_url = get_letterboxd_url(tmdb_id)

Expand Down
4 changes: 2 additions & 2 deletions src/tmdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def get_tmdb_id_for_tv_show(info):
def get_tmdb_id_from_tv_show_details(tv_show_id):
tv_show_url = f"http://localhost:{port}/jsonrpc?request={{%22jsonrpc%22:%222.0%22,%22method%22:%22VideoLibrary.GetTVShowDetails%22,%22params%22:{{%22tvshowid%22:{tv_show_id},%22properties%22:[%22uniqueid%22]}},%22id%22:%22libTvShow%22}}"
tv_show_response = requests.get(tv_show_url).json()
logger.debug(f"TMDB Show search response: {tv_show_response}")
logger.debug(f"Kodi JSONRPC search response: {tv_show_response}")
# Check if 'result' key exists in the response
if 'result' in tv_show_response and 'tvshowdetails' in tv_show_response['result'] and 'uniqueid' in tv_show_response['result']['tvshowdetails'] and 'tmdb' in tv_show_response['result']['tvshowdetails']['uniqueid']:
return tv_show_response['result']['tvshowdetails']['uniqueid']['tmdb']
Expand All @@ -56,7 +56,7 @@ def get_tmdb_id_for_media(info, media_type):
title = quote(info['showtitle'] if media_type == 'tv' else info['title'])
title_url = f"https://api.themoviedb.org/3/search/{media_type}?api_key={TMDB_API_KEY}&query={title}"
title_response = requests.get(title_url).json()
logger.debug(f"TMDB {media_type.capitalize()} search response: {title_response}")
logger.debug(f"Searching TMDB {media_type.capitalize()} with title: {title}: {title_response}")
if 'results' in title_response and len(title_response['results']) > 0:
logger.debug(f"TMDB {media_type.capitalize()} search results: {title_response['results'][0]['id']}")
return title_response['results'][0]['id']
Expand Down
15 changes: 4 additions & 11 deletions src/trakt.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def get_tmdb_id_for_media_via_api(info, media_type):
title = quote(info['showtitle'] if media_type == 'episode' else info['title'])
title_url = f"https://api.themoviedb.org/3/search/{media_type}?api_key={TMDB_API_KEY}&query={title}"
title_response = requests.get(title_url).json()
logger.debug(f"TMDB {media_type.capitalize()} search response: {title_response}")
logger.debug(f"Searching TMDB {media_type.capitalize()} with title: {title}: {title_response}")
if 'results' in title_response and len(title_response['results']) > 0:
logger.debug(f"TMDB {media_type.capitalize()} search results: {title_response['results'][0]['id']}")
return title_response['results'][0]['id']
Expand All @@ -54,7 +54,7 @@ def get_trakt_url(tmdb_id, media_type):
search_type = "movie"
elif media_type == "tv":
media_url = base_url + "shows/"
search_type = "episode"
search_type = "show"
else:
return None

Expand All @@ -70,15 +70,8 @@ def get_trakt_url(tmdb_id, media_type):
data = response.json()
logger.debug(f"Trakt search response: {data}")

# Get the Trakt slug from the first result
if media_type == "tv":
trakt_slug = data[0]["show"]["ids"]["slug"]
season = data[0]["episode"]["season"]
episode = data[0]["episode"]["number"]
trakt_url = f"{media_url}{trakt_slug}/seasons/{season}/episodes/{episode}"
else:
trakt_slug = data[0][search_type]["ids"]["slug"]
trakt_url = media_url + trakt_slug
trakt_slug = data[0][search_type]["ids"]["slug"]
trakt_url = media_url + trakt_slug

logger.debug(f"Generated Trakt URL: {trakt_url}")
return trakt_url
Expand Down

0 comments on commit 73b341c

Please sign in to comment.