2024-01-09 22:35:47 +00:00
|
|
|
# Script to add a new item to the log
|
|
|
|
|
|
|
|
from datetime import datetime
|
2024-01-14 14:00:07 +00:00
|
|
|
from dotenv import load_dotenv
|
2024-01-09 22:35:47 +00:00
|
|
|
import json
|
|
|
|
import logging
|
2024-01-14 14:00:07 +00:00
|
|
|
import os
|
2024-01-09 22:35:47 +00:00
|
|
|
import re
|
|
|
|
import requests
|
|
|
|
from urllib.request import urlopen
|
|
|
|
|
2024-01-14 14:00:07 +00:00
|
|
|
|
|
|
|
def import_by_id(import_id, media_type):
|
|
|
|
if media_type in ['films', 'tv-series']:
|
|
|
|
return import_from_imdb_by_id(import_id, media_type)
|
|
|
|
elif media_type in ['tv-episodes']:
|
|
|
|
return #import_from_tvdb_by_id(import_id, media_type)
|
|
|
|
elif media_type in ['books']:
|
|
|
|
return #import_from_openlibrary_by_id(import_id, media_type)
|
|
|
|
|
|
|
|
|
|
|
|
def import_from_imdb_by_id(imdb_id, media_type):
|
|
|
|
"""Retrieve a film, TV show or TV episode from TMDB using an IMDB ID"""
|
2024-01-09 22:35:47 +00:00
|
|
|
|
|
|
|
api_url = f"https://api.themoviedb.org/3/find/{imdb_id}"
|
|
|
|
|
|
|
|
# Sending API request
|
|
|
|
response = requests.get(
|
|
|
|
api_url,
|
|
|
|
params={
|
|
|
|
'external_source': 'imdb_id'
|
|
|
|
},
|
2024-01-14 14:00:07 +00:00
|
|
|
headers={'Authorization': f"Bearer {TMDB_API_KEY}"}
|
2024-01-09 22:35:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Process the response
|
|
|
|
if (200 == response.status_code):
|
|
|
|
logging.info(response.status_code)
|
|
|
|
elif (429 == response.status_code):
|
|
|
|
time.sleep(2)
|
2024-01-14 14:00:07 +00:00
|
|
|
import_from_imdb_by_id(imdb_id, media_type)
|
2024-01-12 21:36:10 +00:00
|
|
|
return
|
2024-01-09 22:35:47 +00:00
|
|
|
else:
|
|
|
|
logging.error(response.text)
|
|
|
|
|
2024-01-14 14:00:07 +00:00
|
|
|
if ('films' == media_type): results_key = 'movie_results'
|
|
|
|
elif ('tv-episodes' == media_type): results_key = 'tv_episode_results'
|
|
|
|
elif ('tv-series' == media_type): results_key = 'data'
|
|
|
|
|
|
|
|
response_data = json.loads(response.text)[results_key]
|
|
|
|
|
|
|
|
if 1 == len(response_data):
|
|
|
|
item = response_data[0]
|
|
|
|
elif 0 == len(response_data):
|
2024-01-09 22:35:47 +00:00
|
|
|
logging.error(f"Returned no results for {imdb_id}")
|
|
|
|
return
|
2024-01-14 14:00:07 +00:00
|
|
|
elif 1 < len(response_data):
|
|
|
|
logging.warning(f"Returned more than one {media_type} for ID {imdb_id}")
|
|
|
|
print(f"Returned more than one {media_type} for ID {imdb_id}:\n")
|
|
|
|
print(json.dumps(response_data, indent=4))
|
|
|
|
idx = input("\nEnter the index of the result to use: ")
|
2024-01-09 22:35:47 +00:00
|
|
|
try:
|
2024-01-14 14:00:07 +00:00
|
|
|
item = response_data[idx]
|
2024-01-09 22:35:47 +00:00
|
|
|
except:
|
|
|
|
logging.error("Index invalid!")
|
|
|
|
print("Index invalid!")
|
|
|
|
|
|
|
|
# Modify the returned result to add additional data
|
2024-01-14 14:00:07 +00:00
|
|
|
return cleanup_result(item)
|
2024-01-09 22:35:47 +00:00
|
|
|
|
|
|
|
|
2024-01-14 14:00:07 +00:00
|
|
|
def add_item_to_log(imdb_id, media_type, log):
|
|
|
|
"""Add a film or TV episode to a log"""
|
2024-01-12 21:36:10 +00:00
|
|
|
logging.info(f"Processing {imdb_id}…")
|
|
|
|
|
2024-01-14 14:00:07 +00:00
|
|
|
item = import_from_imdb_by_id(imdb_id, media_type)
|
2024-01-12 21:36:10 +00:00
|
|
|
|
|
|
|
if 'log' == log:
|
|
|
|
date_watched = ''
|
|
|
|
while re.search('[0-9]{4}-[0-9]{2}-[0-9]{2}', date_watched) is None:
|
|
|
|
date_watched = input("Enter date watched [YYYY-MM-DD, t for today]:")
|
|
|
|
if 't' == date_watched: date_watched = datetime.today().strftime('%Y-%m-%d')
|
2024-01-14 14:00:07 +00:00
|
|
|
item['date_watched'] = date_watched
|
2024-01-12 21:36:10 +00:00
|
|
|
|
|
|
|
is_rewatch = ''
|
|
|
|
while is_rewatch not in ['y', 'n']:
|
|
|
|
is_rewatch = input("Is this a rewatch? [y/n]:")
|
2024-01-14 14:00:07 +00:00
|
|
|
if 'y' == is_rewatch: item['is_rewatch'] = True
|
|
|
|
item['imdb_id'] = imdb_id
|
2024-01-12 21:36:10 +00:00
|
|
|
|
|
|
|
comments = input("Enter comments (optional):")
|
2024-01-14 14:00:07 +00:00
|
|
|
if '' != comments: item['comments'] = comments
|
2024-01-12 21:36:10 +00:00
|
|
|
|
|
|
|
# Validation step
|
|
|
|
correct = ''
|
2024-01-14 14:00:07 +00:00
|
|
|
print(f"{media_type} data to add:\n")
|
|
|
|
print(json.dumps(item, indent=4))
|
|
|
|
if 'y' != input("\nDoes this look correct? [y]:"): return
|
2024-01-12 21:36:10 +00:00
|
|
|
|
|
|
|
# Save changes
|
2024-01-14 14:00:07 +00:00
|
|
|
logging.info(f"Adding {media_type} to {log}…")
|
2024-01-12 21:36:10 +00:00
|
|
|
|
2024-01-14 14:00:07 +00:00
|
|
|
with open(f"./data/{media_type}/{log}.json", "r") as log_file:
|
|
|
|
log_items = json.load(log_file)
|
2024-01-12 21:36:10 +00:00
|
|
|
|
2024-01-14 14:00:07 +00:00
|
|
|
log_items.insert(0, item)
|
2024-01-12 21:36:10 +00:00
|
|
|
|
2024-01-14 14:00:07 +00:00
|
|
|
with open(f"./data/{media_type}/{log}.json", "w") as log_file:
|
|
|
|
json.dump(log_items, log_file, indent=4)
|
2024-01-12 21:36:10 +00:00
|
|
|
|
2024-01-14 14:00:07 +00:00
|
|
|
logging.info(f"Added {media_type} {film['title']} ({film['release_date']}) to {log}")
|
2024-01-12 21:36:10 +00:00
|
|
|
|
|
|
|
|
2024-01-14 14:00:07 +00:00
|
|
|
def cleanup_result(item):
|
|
|
|
"""Process a film or TV episode returned by the TMDB API by removing unnecessary fields and adding others"""
|
2024-01-09 22:35:47 +00:00
|
|
|
|
2024-01-14 14:00:07 +00:00
|
|
|
for field_name in ['adult', 'backdrop_path', 'episode_type', 'genre_ids', 'media_type', 'popularity', 'production_code', 'runtime', 'still_path', 'video', 'vote_average', 'vote_count']:
|
|
|
|
if field_name in item: del item[field_name]
|
2024-01-09 22:35:47 +00:00
|
|
|
|
2024-01-14 14:00:07 +00:00
|
|
|
if 'original_title' in item and 'original_language' in item:
|
|
|
|
if item['original_title'] == item['title'] and item['original_language'] == 'en':
|
|
|
|
del item['original_title'], item['original_language']
|
2024-01-09 22:35:47 +00:00
|
|
|
|
2024-01-14 14:00:07 +00:00
|
|
|
if 'date_added' not in item: item['date_added'] = datetime.today().strftime('%Y-%m-%d')
|
2024-01-09 22:35:47 +00:00
|
|
|
|
2024-01-14 14:00:07 +00:00
|
|
|
return item
|
2024-01-09 22:35:47 +00:00
|
|
|
|
2024-01-12 21:36:10 +00:00
|
|
|
|
2024-01-14 14:00:07 +00:00
|
|
|
logging.basicConfig(filename='./logs/run.log', encoding='utf-8', level=logging.DEBUG)
|
2024-01-12 21:36:10 +00:00
|
|
|
|
2024-01-14 14:00:07 +00:00
|
|
|
load_dotenv()
|
2024-01-12 21:36:10 +00:00
|
|
|
|
2024-01-14 14:00:07 +00:00
|
|
|
TMDB_API_KEY = os.getenv('TMDB_API_KEY')
|
|
|
|
TVDB_API_KEY = os.getenv('TVDB_API_KEY')
|
|
|
|
OPENLIBRARY_API_KEY = os.getenv('OPENLIBRARY_API_KEY')
|
2024-01-12 21:36:10 +00:00
|
|
|
|
2024-01-14 14:00:07 +00:00
|
|
|
if "" == TMDB_API_KEY: logging.error("TMDB API key not found")
|
|
|
|
if "" == TVDB_API_KEY: logging.error("TVDB API key not found")
|
|
|
|
if "" == OPENLIBRARY_API_KEY: logging.error("OpenLibrary API key not found")
|
2024-01-09 22:35:47 +00:00
|
|
|
|
|
|
|
media_type = ''
|
2024-01-14 14:00:07 +00:00
|
|
|
while media_type not in ['films', 'tv-episodes', 'tv-series', 'books']:
|
|
|
|
media_type = input("Select media type [films|tv-episodes|tv-series|books]:")
|
2024-01-09 22:35:47 +00:00
|
|
|
|
2024-01-14 14:00:07 +00:00
|
|
|
if 'films' == media_type:
|
2024-01-09 22:35:47 +00:00
|
|
|
log = ''
|
|
|
|
while log not in ['log', 'wishlist']:
|
|
|
|
log = input ("Enter log to update [log|wishlist]:")
|
|
|
|
|
|
|
|
imdb_id = ''
|
|
|
|
while re.search("tt[0-9]+", imdb_id) is None:
|
|
|
|
imdb_id = input("Enter IMDB ID:")
|
|
|
|
|
2024-01-14 14:00:07 +00:00
|
|
|
add_item_to_log(imdb_id, media_type, log)
|
2024-01-09 22:35:47 +00:00
|
|
|
|
2024-01-14 14:00:07 +00:00
|
|
|
elif 'books' == media_type:
|
2024-01-09 22:35:47 +00:00
|
|
|
log = ''
|
|
|
|
while log not in ['log', 'current', 'wishlist']:
|
|
|
|
log = input ("Enter log to update [log|current|wishlist]:")
|
|
|
|
|
|
|
|
isbn = ''
|
|
|
|
while re.search("[0-9]+", isbn) is None:
|
|
|
|
isbn = input("Enter ISBN:")
|
|
|
|
|
2024-01-14 14:00:07 +00:00
|
|
|
elif 'tv-episodes' == media_type:
|
2024-01-09 22:35:47 +00:00
|
|
|
imdb_id = ''
|
|
|
|
while re.search("tt[0-9]+", imdb_id) is None:
|
|
|
|
imdb_id = input("Enter IMDB ID:")
|
|
|
|
|
2024-01-14 14:00:07 +00:00
|
|
|
add_item_to_log(imdb_id, media_type, 'log')
|
|
|
|
|
|
|
|
elif 'tv-series' == media_type:
|
|
|
|
log = ''
|
|
|
|
while log not in ['log', 'current', 'wishlist']:
|
|
|
|
log = input ("Enter log to update [log|current|wishlist]:")
|
|
|
|
|
|
|
|
add_item_to_log(media_type, log)
|