Cataloguer/scripts/add_item.py

138 lines
4.6 KiB
Python
Raw Normal View History

2024-01-09 22:35:47 +00:00
# Script to add a new item to the log
from datetime import datetime
import json
import logging
import re
import requests
from urllib.request import urlopen
def import_film(imdb_id, log):
"""Import a film via the TMDB API, given an IMDB ID"""
logging.info(f"Processing {imdb_id}")
api_url = f"https://api.themoviedb.org/3/find/{imdb_id}"
# Sending API request
response = requests.get(
api_url,
params={
'external_source': 'imdb_id'
},
headers={'Authorization': 'Bearer eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOiI1NWQ2ZjY3YzJlOTQwMDI1NTFmN2VkNmEyZWVjM2E3NyIsInN1YiI6IjUxNWMyNzkxMTljMjk1MTQ0ZDAzZDM0NCIsInNjb3BlcyI6WyJhcGlfcmVhZCJdLCJ2ZXJzaW9uIjoxfQ.92eNKubJ_CORCIIlta30P9Qjg_Q9gPRFDTfG4gyz9kY'}
)
# Process the response
if (200 == response.status_code):
logging.info(response.status_code)
elif (429 == response.status_code):
time.sleep(2)
import_film(imdb_id)
else:
logging.error(response.text)
response_data = json.loads(response.text)
if 1 == len(response_data['movie_results']):
film = response_data['movie_results'][0]
elif 0 == len(response_data['movie_results']):
logging.error(f"Returned no results for {imdb_id}")
return
elif 1 < len(response_data['movie_results']):
logging.warning(f"Returned more than one film for ID {imdb_id}")
print(f"Returned more than one film for ID {imdb_id}:")
print(json.dumps(response_data['movie_results'], indent=4))
id = input("Enter the index of the result to use:")
try:
film = response_data['movie_results'][id]
except:
logging.error("Index invalid!")
print("Index invalid!")
# Modify the returned result to add additional data
film = cleanup_film(film)
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')
film['date_watched'] = date_watched
is_rewatch = ''
while is_rewatch not in ['y', 'n']:
is_rewatch = input("Is this a rewatch? [y/n]:")
if 'y' == is_rewatch: film['is_rewatch'] = True
comments = input("Enter comments (optional):")
if '' != comments: film['comments'] = comments
# Validation step
correct = ''
print("Film data to add:")
print(json.dumps(film, indent=4))
if 'y' != input("Does this look correct? [y]:"): return
# Save changes
logging.info('Adding film to log…')
with open(f"./data/films/{log}.json", "r") as films_log:
films = json.load(films_log)
films.insert(0, film)
with open(f"./data/films/{log}.json", "w") as films_log:
json.dump(films, films_log, indent=4)
logging.info(f"Added film {film['title']} ({film['release_date']}) to log {log}")
def cleanup_film(film):
"""Process a film returned by the TMDB API by removing unnecessary fields and adding others"""
del film['adult'], film['backdrop_path'], film['genre_ids'], film['popularity'], film['video'], film['vote_average'], film['vote_count']
if 'media_type' in film: del film['media_type']
if film['original_title'] == film['title'] and film['original_language'] == 'en':
del film['original_title'], film['original_language']
film['date_added'] = datetime.today().strftime('%Y-%m-%d')
return film
logging.basicConfig(filename='../logs/run.log', encoding='utf-8', level=logging.DEBUG)
media_type = ''
while media_type not in ['film', 'tv', 'book']:
media_type = input("Select media type [film|tv|book]:")
if 'film' == media_type:
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:")
import_film(imdb_id, log)
elif 'book' == media_type:
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:")
elif 'tv' == media_type:
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:")
import_tv_episode(imdb_id, log)