add item adding script

This commit is contained in:
Ben Goldsworthy 2024-01-09 23:35:47 +01:00
parent 0a14de8aa6
commit 60e5229b47
5 changed files with 139 additions and 13730 deletions

1
.gitignore vendored
View file

@ -3,3 +3,4 @@ cgi-bin/
resources/
node_modules/
public/
logs/

137
scripts/add_item.py Normal file
View file

@ -0,0 +1,137 @@
# 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)

View file

@ -133,7 +133,7 @@ def cleanup_film(film):
return film
logging.basicConfig(filename='run.log', encoding='utf-8', level=logging.DEBUG)
logging.basicConfig(filename='../logs/run.log', encoding='utf-8', level=logging.DEBUG)
with open("../data/films/wishlist.json", "r") as films_log:
films = json.load(films_log)

View file

@ -1,147 +0,0 @@
INFO:root:Processing items…
INFO:root:Processing ID tt4053592 (How Not to Make a Movie)…
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.themoviedb.org:443
DEBUG:urllib3.connectionpool:https://api.themoviedb.org:443 "GET /3/find/tt4053592?external_source=imdb_id HTTP/1.1" 200 None
INFO:root:200
WARNING:root:Returning no results for How Not to Make a Movie
INFO:root:Processing ID (Hated: GG Allin & The Murder Junkies)…
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.themoviedb.org:443
DEBUG:urllib3.connectionpool:https://api.themoviedb.org:443 "GET /3/find/?external_source=imdb_id HTTP/1.1" 404 None
ERROR:root:{"success":false,"status_code":34,"status_message":"The resource you requested could not be found."}
INFO:root:Processing items…
INFO:root:Processing ID tt4053592 (How Not to Make a Movie)…
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.themoviedb.org:443
DEBUG:urllib3.connectionpool:https://api.themoviedb.org:443 "GET /3/find/tt4053592?external_source=imdb_id HTTP/1.1" 200 None
INFO:root:200
WARNING:root:Returning no results for How Not to Make a Movie
INFO:root:Processing ID (Hated: GG Allin & The Murder Junkies)…
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.themoviedb.org:443
DEBUG:urllib3.connectionpool:https://api.themoviedb.org:443 "GET /3/find/?external_source=imdb_id HTTP/1.1" 404 None
ERROR:root:{"success":false,"status_code":34,"status_message":"The resource you requested could not be found."}
INFO:root:Processing items…
INFO:root:Processing ID tt4053592 (How Not to Make a Movie)…
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.themoviedb.org:443
DEBUG:urllib3.connectionpool:https://api.themoviedb.org:443 "GET /3/find/tt4053592?external_source=imdb_id HTTP/1.1" 200 None
INFO:root:200
WARNING:root:Returning no results for How Not to Make a Movie
INFO:root:Processing Hated: GG Allin & The Murder Junkies…
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.themoviedb.org:443
DEBUG:urllib3.connectionpool:https://api.themoviedb.org:443 "GET /3/search/movie?query=Hated%3A+GG+Allin+%26+The+Murder+Junkies&include_adult=True&year=1994 HTTP/1.1" 200 None
INFO:root:200
WARNING:root:Returned no results for Hated: GG Allin & The Murder Junkies (1994)
INFO:root:Processing ID tt0107086 (Hated: GG Allin & The Murder Junkies)…
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.themoviedb.org:443
DEBUG:urllib3.connectionpool:https://api.themoviedb.org:443 "GET /3/find/tt0107086?external_source=imdb_id HTTP/1.1" 200 None
INFO:root:200
INFO:root:Processing ID tt0065908 (Jak rozpętałem drugą wojnę światową)…
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.themoviedb.org:443
DEBUG:urllib3.connectionpool:https://api.themoviedb.org:443 "GET /3/find/tt0065908?external_source=imdb_id HTTP/1.1" 200 None
INFO:root:200
WARNING:root:Returning no results for Jak rozpętałem drugą wojnę światową
INFO:root:Processing Martin…
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.themoviedb.org:443
DEBUG:urllib3.connectionpool:https://api.themoviedb.org:443 "GET /3/search/movie?query=Martin&include_adult=True&year=1976 HTTP/1.1" 200 None
INFO:root:200
INFO:root:Processing ID tt0077914 (Martin)…
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.themoviedb.org:443
DEBUG:urllib3.connectionpool:https://api.themoviedb.org:443 "GET /3/find/tt0077914?external_source=imdb_id HTTP/1.1" 200 None
INFO:root:200
INFO:root:Processing ID (Safe House)…
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.themoviedb.org:443
DEBUG:urllib3.connectionpool:https://api.themoviedb.org:443 "GET /3/find/?external_source=imdb_id HTTP/1.1" 404 None
ERROR:root:{"success":false,"status_code":34,"status_message":"The resource you requested could not be found."}
INFO:root:Processing items…
INFO:root:Processing ID tt4053592 (How Not to Make a Movie)…
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.themoviedb.org:443
DEBUG:urllib3.connectionpool:https://api.themoviedb.org:443 "GET /3/find/tt4053592?external_source=imdb_id HTTP/1.1" 200 None
INFO:root:200
WARNING:root:Returning no results for How Not to Make a Movie
INFO:root:Processing Hated: GG Allin & The Murder Junkies…
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.themoviedb.org:443
DEBUG:urllib3.connectionpool:https://api.themoviedb.org:443 "GET /3/search/movie?query=Hated%3A+GG+Allin+%26+The+Murder+Junkies&include_adult=True&year=1994 HTTP/1.1" 200 None
INFO:root:200
WARNING:root:Returned no results for Hated: GG Allin & The Murder Junkies (1994)
INFO:root:Processing ID tt0107086 (Hated: GG Allin & The Murder Junkies)…
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.themoviedb.org:443
DEBUG:urllib3.connectionpool:https://api.themoviedb.org:443 "GET /3/find/tt0107086?external_source=imdb_id HTTP/1.1" 200 None
INFO:root:200
INFO:root:Processing ID tt0065908 (Jak rozpętałem drugą wojnę światową)…
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.themoviedb.org:443
DEBUG:urllib3.connectionpool:https://api.themoviedb.org:443 "GET /3/find/tt0065908?external_source=imdb_id HTTP/1.1" 200 None
INFO:root:200
WARNING:root:Returning no results for Jak rozpętałem drugą wojnę światową
INFO:root:Processing Martin…
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.themoviedb.org:443
DEBUG:urllib3.connectionpool:https://api.themoviedb.org:443 "GET /3/search/movie?query=Martin&include_adult=True&year=1976 HTTP/1.1" 200 None
INFO:root:200
INFO:root:Processing ID tt0077914 (Martin)…
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.themoviedb.org:443
DEBUG:urllib3.connectionpool:https://api.themoviedb.org:443 "GET /3/find/tt0077914?external_source=imdb_id HTTP/1.1" 200 None
INFO:root:200
INFO:root:Processing Safe House…
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.themoviedb.org:443
DEBUG:urllib3.connectionpool:https://api.themoviedb.org:443 "GET /3/search/movie?query=Safe+House&include_adult=True&year=2012 HTTP/1.1" 200 None
INFO:root:200
WARNING:root:Returned more than one film for Safe House (2012)
WARNING:root:Skipped Safe House (2012)
INFO:root:Finished processing items
INFO:root:Processing items…
INFO:root:Processing ID tt4053592 (How Not to Make a Movie)…
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.themoviedb.org:443
DEBUG:urllib3.connectionpool:https://api.themoviedb.org:443 "GET /3/find/tt4053592?external_source=imdb_id HTTP/1.1" 200 None
INFO:root:200
WARNING:root:Returning no results for How Not to Make a Movie
INFO:root:Processing ID tt0065908 (Jak rozpętałem drugą wojnę światową)…
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.themoviedb.org:443
DEBUG:urllib3.connectionpool:https://api.themoviedb.org:443 "GET /3/find/tt0065908?external_source=imdb_id HTTP/1.1" 200 None
INFO:root:200
WARNING:root:Returning no results for Jak rozpętałem drugą wojnę światową
INFO:root:Processing Safe House…
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.themoviedb.org:443
DEBUG:urllib3.connectionpool:https://api.themoviedb.org:443 "GET /3/search/movie?query=Safe+House&include_adult=True&year=2012 HTTP/1.1" 200 None
INFO:root:200
WARNING:root:Returned more than one film for Safe House (2012)
WARNING:root:Skipped Safe House (2012)
INFO:root:Finished processing items
INFO:root:Processing items…
INFO:root:Processing ID tt4053592 (How Not to Make a Movie)…
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.themoviedb.org:443
DEBUG:urllib3.connectionpool:https://api.themoviedb.org:443 "GET /3/find/tt4053592?external_source=imdb_id HTTP/1.1" 200 None
INFO:root:200
WARNING:root:Returning no results for How Not to Make a Movie
INFO:root:Processing ID tt0065908 (Jak rozpętałem drugą wojnę światową)…
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.themoviedb.org:443
DEBUG:urllib3.connectionpool:https://api.themoviedb.org:443 "GET /3/find/tt0065908?external_source=imdb_id HTTP/1.1" 200 None
INFO:root:200
WARNING:root:Returning no results for Jak rozpętałem drugą wojnę światową
INFO:root:Processing Safe House…
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.themoviedb.org:443
DEBUG:urllib3.connectionpool:https://api.themoviedb.org:443 "GET /3/search/movie?query=Safe+House&include_adult=True&year=2012 HTTP/1.1" 200 None
INFO:root:200
WARNING:root:Returned more than one film for Safe House (2012)
INFO:root:Processing ID tt1599348 (Safe House)…
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.themoviedb.org:443
DEBUG:urllib3.connectionpool:https://api.themoviedb.org:443 "GET /3/find/tt1599348?external_source=imdb_id HTTP/1.1" 200 None
INFO:root:200
INFO:root:Finished processing items
INFO:root:Processing items…
INFO:root:Processing How Not to Make a Movie…
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.themoviedb.org:443
DEBUG:urllib3.connectionpool:https://api.themoviedb.org:443 "GET /3/search/movie?query=How+Not+to+Make+a+Movie&include_adult=True&year=2013 HTTP/1.1" 200 None
INFO:root:200
WARNING:root:Returned no results for How Not to Make a Movie (2013)
INFO:root:Processing ID tt4053592 (How Not to Make a Movie)…
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.themoviedb.org:443
DEBUG:urllib3.connectionpool:https://api.themoviedb.org:443 "GET /3/find/tt4053592?external_source=imdb_id HTTP/1.1" 200 None
INFO:root:200
WARNING:root:Returning no results for How Not to Make a Movie
INFO:root:Processing Jak rozpętałem drugą wojnę światową…
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.themoviedb.org:443
DEBUG:urllib3.connectionpool:https://api.themoviedb.org:443 "GET /3/search/movie?query=Jak+rozp%C4%99ta%C5%82em+drug%C4%85+wojn%C4%99+%C5%9Bwiatow%C4%85&include_adult=True&year=1970 HTTP/1.1" 200 None
INFO:root:200
INFO:root:Processing ID tt0065908 (Jak rozpętałem drugą wojnę światową)…
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.themoviedb.org:443
DEBUG:urllib3.connectionpool:https://api.themoviedb.org:443 "GET /3/find/tt0065908?external_source=imdb_id HTTP/1.1" 200 None
INFO:root:200
WARNING:root:Returning no results for Jak rozpętałem drugą wojnę światową
INFO:root:Finished processing items

File diff suppressed because it is too large Load diff