218 lines
4.7 KiB
Python
218 lines
4.7 KiB
Python
#!/usr/bin/env python3
|
|
|
|
import os
|
|
import sys
|
|
import requests
|
|
|
|
|
|
TMDB_TOKEN = "eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJlOTIwMzJlMDk1NmEzMWEyZjQ4MDA4NGI0MTg4OTIyZCIsIm5iZiI6MTc5MDQzMjc5NS42NjcsInN1YiI6IjZhYjdkNjFiZDY5NmI1ZGFjNGM5NDRkMyIsInNjb3BlcyI6WyJhcGlfcmVhZCJdLCJ2ZXJzaW9uIjoxfQ.IjddKtQ5X62A63kYslH24Pxg_TFCzu-QpGak_Pemgr8"
|
|
|
|
if not TMDB_TOKEN:
|
|
print("ERROR: TMDB_API_TOKEN is not set.")
|
|
print()
|
|
print("Example:")
|
|
print(" export TMDB_API_TOKEN='your_token'")
|
|
sys.exit(1)
|
|
|
|
|
|
BASE_URL = "https://api.themoviedb.org/3"
|
|
|
|
HEADERS = {
|
|
"Authorization": f"Bearer {TMDB_TOKEN}",
|
|
"accept": "application/json",
|
|
}
|
|
|
|
|
|
def tmdb_get(endpoint, params=None):
|
|
"""Perform a GET request against TMDb."""
|
|
|
|
url = BASE_URL + endpoint
|
|
|
|
response = requests.get(
|
|
url,
|
|
headers=HEADERS,
|
|
params=params,
|
|
timeout=15,
|
|
)
|
|
|
|
response.raise_for_status()
|
|
|
|
return response.json()
|
|
|
|
|
|
def find_by_imdb(imdb_id):
|
|
"""
|
|
Find a movie/TV show/person using an IMDb ID.
|
|
|
|
Example:
|
|
tt1234567
|
|
"""
|
|
|
|
data = tmdb_get(
|
|
f"/find/{imdb_id}",
|
|
{
|
|
"external_source": "imdb_id",
|
|
},
|
|
)
|
|
|
|
return data
|
|
|
|
|
|
def find_by_tvdb(tvdb_id):
|
|
"""
|
|
Try to find a TV show using a TVDB ID.
|
|
|
|
TMDb's external ID system does not expose every external
|
|
ID through the same /find mechanism, so we first try the
|
|
external ID endpoint.
|
|
"""
|
|
|
|
data = tmdb_get(
|
|
f"/find/{tvdb_id}",
|
|
{
|
|
"external_source": "tvdb_id",
|
|
},
|
|
)
|
|
|
|
return data
|
|
|
|
|
|
def get_tv_details(tmdb_id):
|
|
"""
|
|
Retrieve TV show details in German.
|
|
"""
|
|
|
|
return tmdb_get(
|
|
f"/tv/{tmdb_id}",
|
|
{
|
|
"language": "de-DE",
|
|
},
|
|
)
|
|
|
|
|
|
def get_movie_details(tmdb_id):
|
|
"""
|
|
Retrieve movie details in German.
|
|
"""
|
|
|
|
return tmdb_get(
|
|
f"/movie/{tmdb_id}",
|
|
{
|
|
"language": "de-DE",
|
|
},
|
|
)
|
|
|
|
|
|
def print_results(data):
|
|
"""Print useful IDs returned by TMDb."""
|
|
|
|
print()
|
|
print("TMDb result")
|
|
print("=" * 60)
|
|
|
|
movies = data.get("movie_results", [])
|
|
tv = data.get("tv_results", [])
|
|
|
|
if movies:
|
|
print()
|
|
print("MOVIE")
|
|
|
|
for movie in movies:
|
|
print(f"TMDb ID: {movie.get('id')}")
|
|
print(f"Title: {movie.get('title')}")
|
|
print(f"Original: {movie.get('original_title')}")
|
|
print(f"Release date: {movie.get('release_date')}")
|
|
|
|
if tv:
|
|
print()
|
|
print("TV SHOW")
|
|
|
|
for show in tv:
|
|
print(f"TMDb ID: {show.get('id')}")
|
|
print(f"Name: {show.get('name')}")
|
|
print(f"Original: {show.get('original_name')}")
|
|
print(f"First air: {show.get('first_air_date')}")
|
|
|
|
if not movies and not tv:
|
|
print("No result found.")
|
|
|
|
|
|
def main():
|
|
|
|
if len(sys.argv) != 3:
|
|
print("Usage:")
|
|
print()
|
|
print(" python tmdb_lookup.py imdb tt1234567")
|
|
print(" python tmdb_lookup.py tvdb 417272")
|
|
sys.exit(1)
|
|
|
|
id_type = sys.argv[1].lower()
|
|
external_id = sys.argv[2]
|
|
|
|
if id_type == "imdb":
|
|
|
|
print(f"Looking up IMDb ID: {external_id}")
|
|
|
|
data = find_by_imdb(external_id)
|
|
|
|
elif id_type == "tvdb":
|
|
|
|
print(f"Looking up TVDB ID: {external_id}")
|
|
|
|
data = find_by_tvdb(external_id)
|
|
|
|
else:
|
|
print("ERROR: ID type must be:")
|
|
print(" imdb")
|
|
print(" tvdb")
|
|
sys.exit(1)
|
|
|
|
print_results(data)
|
|
|
|
# --------------------------------------------------------
|
|
# Get detailed German metadata
|
|
# --------------------------------------------------------
|
|
|
|
movies = data.get("movie_results", [])
|
|
tv = data.get("tv_results", [])
|
|
|
|
if tv:
|
|
|
|
tmdb_id = tv[0]["id"]
|
|
|
|
details = get_tv_details(tmdb_id)
|
|
|
|
print()
|
|
print("German TV metadata")
|
|
print("=" * 60)
|
|
|
|
print(f"TMDb ID: {details.get('id')}")
|
|
print(f"German name: {details.get('name')}")
|
|
print(f"Original name: {details.get('original_name')}")
|
|
print(f"First air: {details.get('first_air_date')}")
|
|
print(f"Overview: {details.get('overview')}")
|
|
|
|
elif movies:
|
|
|
|
tmdb_id = movies[0]["id"]
|
|
|
|
details = get_movie_details(tmdb_id)
|
|
|
|
print()
|
|
print("German movie metadata")
|
|
print("=" * 60)
|
|
|
|
print(f"TMDb ID: {details.get('id')}")
|
|
print(f"German title: {details.get('title')}")
|
|
print(f"Original: {details.get('original_title')}")
|
|
print(f"Release date: {details.get('release_date')}")
|
|
print(f"Overview: {details.get('overview')}")
|
|
|
|
else:
|
|
|
|
print()
|
|
print("No movie or TV result found.")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main() |