Cataloguer/scripts/add_item.py
2024-01-12 22:36:10 +01:00

231 lines
9.1 KiB
Python

# 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)
return
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 import_tv_episode(imdb_id, log):
"""Import a TV episode 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)
return
else:
logging.error(response.text)
response_data = json.loads(response.text)
if 1 == len(response_data['tv_episode_results']):
tv_episode = response_data['tv_episode_results'][0]
elif 0 == len(response_data['tv_episode_results']):
logging.error(f"Returned no results for {imdb_id}")
return
elif 1 < len(response_data['tv_episode_results']):
logging.warning(f"Returned more than one TV episode for ID {imdb_id}")
print(f"Returned more than one TV episode for ID {imdb_id}:")
print(json.dumps(response_data['tv_episode_results'], indent=4))
id = input("Enter the index of the result to use:")
try:
tv_episode = response_data['tv_episode_results'][id]
except:
logging.error("Index invalid!")
print("Index invalid!")
# Modify the returned result to add additional data
tv_episode = cleanup_tv_episode(tv_episode)
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')
tv_episode['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: tv_episode['is_rewatch'] = True
comments = input("Enter comments (optional):")
if '' != comments: tv_episode['comments'] = comments
# Validation step
correct = ''
print("TV episode data to add:")
print(json.dumps(tv_episode, indent=4))
if 'y' != input("Does this look correct? [y]:"): return
# Save changes
logging.info('Adding TV episode to log…')
with open(f"./data/tv/{log}.json", "r") as tv_episodes_log:
tv_episodes = json.load(tv_episodes_log)
tv_episodes.insert(0, tv_episode)
with open(f"./data/tv/{log}.json", "w") as tv_episodes_log:
json.dump(tv_episodes, tv_episodes_log, indent=4)
logging.info(f"Added TV episode {tv_episode['name']} ({tv_episode['air_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
def cleanup_tv_episode(tv_episode):
"""Process a TV episode returned by the TMDB API by removing unnecessary fields and adding others"""
# eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJhZ2UiOiIiLCJhcGlrZXkiOiJlNGRiYmZhYi0wZmM3LTRkMmEtYjgyZi0wZmRmMjAwOTcwOGYiLCJjb21tdW5pdHlfc3VwcG9ydGVkIjpmYWxzZSwiZXhwIjoxNzA3NTQ1NzQ5LCJnZW5kZXIiOiIiLCJoaXRzX3Blcl9kYXkiOjEwMDAwMDAwMCwiaGl0c19wZXJfbW9udGgiOjEwMDAwMDAwMCwiaWQiOiIxNTE5NDMiLCJpc19tb2QiOmZhbHNlLCJpc19zeXN0ZW1fa2V5IjpmYWxzZSwiaXNfdHJ1c3RlZCI6ZmFsc2UsInBpbiI6bnVsbCwicm9sZXMiOltdLCJ0ZW5hbnQiOiJ0dmRiIiwidXVpZCI6IiJ9.gdBQ7q3GKhl-Sr5GjjKQ4X2lJEuS5povPkYciYTY4kr_NMy_w_qBUV1lAjR-OVOyh3EB_zjroT08JiUUOUJbRGGNBpr7ct1gJgaiqKOncwawZZHoQOZMUw-wX77rdAmW93XusX9vF3HyQGp6982E6AdUhfsdx4be8DWDtG3roKnxXiwD5dC_0_V7eB-fYdk3xWSkAjJ4u7JxZTvsKuCpKFJu5ag4HB13tEgo2wB6PR4Bea1ocv2n9BJLbJevUvz4GmS8zNMMLvOTg9kbxr_BGX77XT0UU8L3Nxr21RblHkFfiR3DrqAp-DdKBNa_r7W0-fa7LrZqHFRq8FlSfjDqp29-uS4zOPYx6DxiBOCO30h0mOEncwnjiWRKEbPHMO9i53J8rbyOykwLhx6O6q431BTNpB8RFhhk5_RxGZfYNwXNl0XgSQSxeJgM9z19G5ADOCr4fvyTAu3KvKbmMFqNRxblHWOLiqGQjMZpjwOizVLMcTxICEv4HY6Sf9hM_deETWERmagmChsj1VACLa7Yar8wABuoQDFV3dMbDijDeEZgBc7CZ9NmAlYFW2YlARlqzI3lyIAJz_WKpmxZM400gNlDICPVqhT4VNq8ZYA2_bfu8yJxbm6BLpgqw_IPP2VLzKoGN8dCmavU_QeET21GNeDXuad9XcqxmZl9K1wPJCA
del tv_episode['still_path'], tv_episode['vote_average'], tv_episode['vote_count'], tv_episode['episode_type'], tv_episode['production_code'], tv_episode['runtime']
if 'media_type' in tv_episode: del tv_episode['media_type']
tv_episode['date_added'] = datetime.today().strftime('%Y-%m-%d')
return tv_episode
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)