update scripts
This commit is contained in:
parent
8281a25562
commit
d3dd9a1051
5 changed files with 270 additions and 280 deletions
3
.env.example
Normal file
3
.env.example
Normal file
|
@ -0,0 +1,3 @@
|
|||
TMDB_API_KEY=
|
||||
TVDB_API_KEY=
|
||||
OPENLIBRARY_API_KEY=
|
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -4,3 +4,4 @@ resources/
|
|||
node_modules/
|
||||
public/
|
||||
logs/
|
||||
.env
|
||||
|
|
|
@ -1,15 +1,26 @@
|
|||
# Script to add a new item to the log
|
||||
|
||||
from datetime import datetime
|
||||
from dotenv import load_dotenv
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
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}…")
|
||||
|
||||
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"""
|
||||
|
||||
api_url = f"https://api.themoviedb.org/3/find/{imdb_id}"
|
||||
|
||||
|
@ -19,7 +30,7 @@ def import_film(imdb_id, log):
|
|||
params={
|
||||
'external_source': 'imdb_id'
|
||||
},
|
||||
headers={'Authorization': 'Bearer eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOiI1NWQ2ZjY3YzJlOTQwMDI1NTFmN2VkNmEyZWVjM2E3NyIsInN1YiI6IjUxNWMyNzkxMTljMjk1MTQ0ZDAzZDM0NCIsInNjb3BlcyI6WyJhcGlfcmVhZCJdLCJ2ZXJzaW9uIjoxfQ.92eNKubJ_CORCIIlta30P9Qjg_Q9gPRFDTfG4gyz9kY'}
|
||||
headers={'Authorization': f"Bearer {TMDB_API_KEY}"}
|
||||
)
|
||||
|
||||
# Process the response
|
||||
|
@ -27,179 +38,111 @@ def import_film(imdb_id, log):
|
|||
logging.info(response.status_code)
|
||||
elif (429 == response.status_code):
|
||||
time.sleep(2)
|
||||
import_film(imdb_id)
|
||||
import_from_imdb_by_id(imdb_id, media_type)
|
||||
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']):
|
||||
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):
|
||||
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:")
|
||||
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: ")
|
||||
try:
|
||||
film = response_data['movie_results'][id]
|
||||
item = response_data[idx]
|
||||
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}")
|
||||
return cleanup_result(item)
|
||||
|
||||
|
||||
def import_tv_episode(imdb_id, log):
|
||||
"""Import a TV episode via the TMDB API, given an IMDB ID"""
|
||||
def add_item_to_log(imdb_id, media_type, log):
|
||||
"""Add a film or TV episode to a log"""
|
||||
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)
|
||||
item = import_from_imdb_by_id(imdb_id, media_type)
|
||||
|
||||
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
|
||||
item['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
|
||||
if 'y' == is_rewatch: item['is_rewatch'] = True
|
||||
item['imdb_id'] = imdb_id
|
||||
|
||||
comments = input("Enter comments (optional):")
|
||||
if '' != comments: tv_episode['comments'] = comments
|
||||
if '' != comments: item['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
|
||||
print(f"{media_type} data to add:\n")
|
||||
print(json.dumps(item, indent=4))
|
||||
if 'y' != input("\nDoes this look correct? [y]:"): return
|
||||
|
||||
# Save changes
|
||||
logging.info('Adding TV episode to log…')
|
||||
logging.info(f"Adding {media_type} to {log}…")
|
||||
|
||||
with open(f"./data/tv/{log}.json", "r") as tv_episodes_log:
|
||||
tv_episodes = json.load(tv_episodes_log)
|
||||
with open(f"./data/{media_type}/{log}.json", "r") as log_file:
|
||||
log_items = json.load(log_file)
|
||||
|
||||
tv_episodes.insert(0, tv_episode)
|
||||
log_items.insert(0, item)
|
||||
|
||||
with open(f"./data/tv/{log}.json", "w") as tv_episodes_log:
|
||||
json.dump(tv_episodes, tv_episodes_log, indent=4)
|
||||
with open(f"./data/{media_type}/{log}.json", "w") as log_file:
|
||||
json.dump(log_items, log_file, indent=4)
|
||||
|
||||
logging.info(f"Added TV episode {tv_episode['name']} ({tv_episode['air_date']}) to log {log}")
|
||||
logging.info(f"Added {media_type} {film['title']} ({film['release_date']}) to {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']
|
||||
def cleanup_result(item):
|
||||
"""Process a film or TV episode returned by the TMDB API by removing unnecessary fields and adding others"""
|
||||
|
||||
if film['original_title'] == film['title'] and film['original_language'] == 'en':
|
||||
del film['original_title'], film['original_language']
|
||||
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]
|
||||
|
||||
film['date_added'] = datetime.today().strftime('%Y-%m-%d')
|
||||
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']
|
||||
|
||||
return film
|
||||
if 'date_added' not in item: item['date_added'] = datetime.today().strftime('%Y-%m-%d')
|
||||
|
||||
|
||||
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
|
||||
return item
|
||||
|
||||
|
||||
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]:")
|
||||
load_dotenv()
|
||||
|
||||
if 'film' == media_type:
|
||||
TMDB_API_KEY = os.getenv('TMDB_API_KEY')
|
||||
TVDB_API_KEY = os.getenv('TVDB_API_KEY')
|
||||
OPENLIBRARY_API_KEY = os.getenv('OPENLIBRARY_API_KEY')
|
||||
|
||||
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")
|
||||
|
||||
media_type = ''
|
||||
while media_type not in ['films', 'tv-episodes', 'tv-series', 'books']:
|
||||
media_type = input("Select media type [films|tv-episodes|tv-series|books]:")
|
||||
|
||||
if 'films' == media_type:
|
||||
log = ''
|
||||
while log not in ['log', 'wishlist']:
|
||||
log = input ("Enter log to update [log|wishlist]:")
|
||||
|
@ -208,9 +151,9 @@ while media_type not in ['film', 'tv', 'book']:
|
|||
while re.search("tt[0-9]+", imdb_id) is None:
|
||||
imdb_id = input("Enter IMDB ID:")
|
||||
|
||||
import_film(imdb_id, log)
|
||||
add_item_to_log(imdb_id, media_type, log)
|
||||
|
||||
elif 'book' == media_type:
|
||||
elif 'books' == media_type:
|
||||
log = ''
|
||||
while log not in ['log', 'current', 'wishlist']:
|
||||
log = input ("Enter log to update [log|current|wishlist]:")
|
||||
|
@ -219,13 +162,16 @@ while media_type not in ['film', 'tv', 'book']:
|
|||
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]:")
|
||||
|
||||
elif 'tv-episodes' == media_type:
|
||||
imdb_id = ''
|
||||
while re.search("tt[0-9]+", imdb_id) is None:
|
||||
imdb_id = input("Enter IMDB ID:")
|
||||
|
||||
import_tv_episode(imdb_id, log)
|
||||
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)
|
||||
|
|
|
@ -1,143 +0,0 @@
|
|||
import json
|
||||
import logging
|
||||
import requests
|
||||
import time
|
||||
from urllib.request import urlopen
|
||||
|
||||
|
||||
def process_items(items):
|
||||
logging.info("Processing items…")
|
||||
item_values = {}
|
||||
|
||||
for i, item in enumerate(items):
|
||||
if 'id' not in item:
|
||||
if 'Date Added' in item:
|
||||
item_values['date_added'] = item['Date Added']
|
||||
del item['Date Added']
|
||||
if 'Date Watched' in item:
|
||||
item_values['date_watched'] = item['Date Watched']
|
||||
del item['Date Watched']
|
||||
if 'Rewatch' in item:
|
||||
item_values['is_rewatch'] = item['Rewatch']
|
||||
del item['Rewatch']
|
||||
if 'Comments' in item:
|
||||
item_values['comments'] = item['Comments']
|
||||
del item['Comments']
|
||||
|
||||
if 'IMDB ID' in item:
|
||||
items[i] = populate_from_id(item)
|
||||
else:
|
||||
items[i] = populate_from_details(item)
|
||||
|
||||
items[i] |= item_values
|
||||
|
||||
with open("../data/films/wishlist.json", "w") as films_log:
|
||||
json.dump(items, films_log, indent=4)
|
||||
|
||||
logging.info("Finished processing items")
|
||||
|
||||
|
||||
def populate_from_details(item):
|
||||
logging.info(f"Processing {item['Title']}…")
|
||||
|
||||
api_url = f"https://api.themoviedb.org/3/search/movie"
|
||||
|
||||
# Sending API request
|
||||
response = requests.get(
|
||||
api_url,
|
||||
params={
|
||||
'query': item['Title'],
|
||||
'include_adult': True,
|
||||
'year': item['Release Year']
|
||||
},
|
||||
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)
|
||||
populate_from_details(item)
|
||||
else:
|
||||
logging.error(response.text)
|
||||
|
||||
response_data = json.loads(response.text)
|
||||
|
||||
if 1 == len(response_data['results']):
|
||||
film = response_data['results'][0]
|
||||
return cleanup_film(film)
|
||||
elif 0 == len(response_data['results']):
|
||||
logging.warning(f"Returned no results for {item['Title']} ({item['Release Year']})")
|
||||
elif 1 < len(response_data['results']):
|
||||
response_data['results'] = [film for film in response_data['results'] if film['title'] == item['Title']]
|
||||
if 1 < len(response_data['results']):
|
||||
logging.warning(f"Returned more than one film for {item['Title']} ({item['Release Year']})")
|
||||
|
||||
item['IMDB ID'] = input(f"Enter IMDB ID for {item['Title']} ({item['Release Year']}):")
|
||||
|
||||
if item['IMDB ID'] != '':
|
||||
return populate_from_id(item)
|
||||
else:
|
||||
logging.warning(f"Skipped {item['Title']} ({item['Release Year']})")
|
||||
return item
|
||||
|
||||
|
||||
def populate_from_id(item):
|
||||
logging.info(f"Processing ID {item['IMDB ID']} ({item['Title']})…")
|
||||
|
||||
api_url = f"https://api.themoviedb.org/3/find/{item['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)
|
||||
populate_from_id(item)
|
||||
else:
|
||||
logging.error(response.text)
|
||||
|
||||
response_data = json.loads(response.text)
|
||||
if len(response_data['movie_results']) > 1:
|
||||
logging.warning(f"Returned more than one film for ID {item['IMDB ID']}")
|
||||
return item
|
||||
|
||||
if len(response_data['movie_results']) > 0:
|
||||
film = response_data['movie_results'][0]
|
||||
return cleanup_film(film)
|
||||
else:
|
||||
logging.warning(f"Returning no results for {item['Title']}")
|
||||
return item
|
||||
|
||||
return cleanup_film(film)
|
||||
|
||||
|
||||
def cleanup_film(film):
|
||||
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['poster_path'] = f"https://www.themoviedb.org/t/p/original/{film['poster_path']}"
|
||||
|
||||
return film
|
||||
|
||||
|
||||
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)
|
||||
|
||||
process_items(films)
|
||||
|
183
scripts/process_logs.py
Normal file
183
scripts/process_logs.py
Normal file
|
@ -0,0 +1,183 @@
|
|||
from dotenv import load_dotenv
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import requests
|
||||
import time
|
||||
from urllib.request import urlopen
|
||||
from scripts.add_item import cleanup_result, import_by_id
|
||||
|
||||
|
||||
def process_log(media_type, log):
|
||||
logging.info(f"Processing {media_type}/{log}…")
|
||||
|
||||
with open(f"./data/{media_type}/{log}.json", "r") as log_file:
|
||||
log_items = json.load(log_file)
|
||||
|
||||
log_item_values = {}
|
||||
|
||||
for i, item in enumerate(log_items):
|
||||
|
||||
if 'id' not in item:
|
||||
if 'films' == media_type: item_title = item['Title']
|
||||
elif 'tv-episodes' == media_type: item_title = item['Episode Title']
|
||||
elif 'tv-series' == media_type: item_title = item['Show Title']
|
||||
|
||||
logging.debug(f"Processing {item_title}…")
|
||||
|
||||
# Rename pre-existing fields
|
||||
if 'Date Added' in item:
|
||||
log_item_values['date_added'] = item['Date Added']
|
||||
del item['Date Added']
|
||||
if 'Date Watched' in item:
|
||||
log_item_values['date_watched'] = item['Date Watched']
|
||||
del item['Date Watched']
|
||||
if 'Rewatch' in item:
|
||||
log_item_values['is_rewatch'] = item['Rewatch']
|
||||
del item['Rewatch']
|
||||
if 'Comments' in item:
|
||||
log_item_values['comments'] = item['Comments']
|
||||
del item['Comments']
|
||||
if 'Series Title' in item:
|
||||
log_item_values['series_title'] = item['Series Title']
|
||||
del item['Series Title']
|
||||
if 'Episode Title' in item:
|
||||
log_item_values['name'] = item['Episode Title']
|
||||
del item['Episode Title']
|
||||
if 'Episode Number' in item:
|
||||
split_num = log_item_values['episode_number'].split("E")
|
||||
log_item_values['episode_number'] = split_num[1]
|
||||
log_item_values['season_number'] = split_num[0] or None
|
||||
del item['Episode Number']
|
||||
|
||||
if 'IMDB ID' in item:
|
||||
log_items[i] = import_by_id(item['IMDB ID'], media_type)
|
||||
else:
|
||||
log_items[i] = import_by_details(item, item_title, media_type)
|
||||
|
||||
if log_items[i] is None:
|
||||
item['imdb_id'] = input(f"Enter IMDB ID for {item_title}: ")
|
||||
|
||||
if re.search("tt[0-9]+", item['imdb_id']) is not None:
|
||||
log_items[i] = import_by_id(item['imdb_id'], media_type)
|
||||
else:
|
||||
logging.warning(f"Skipped {item_title}")
|
||||
|
||||
log_items[i] |= log_item_values
|
||||
|
||||
with open(f"./data/{media_type}/{log}.json", "w") as log_file:
|
||||
json.dump(log_items, log_file, indent=4)
|
||||
|
||||
logging.info(f"Finished processing {media_type}/{log}")
|
||||
|
||||
|
||||
def import_by_details(item, item_title, media_type):
|
||||
if media_type in ['films', 'tv-series']:
|
||||
return import_from_tmdb_by_details(item, item_title, media_type)
|
||||
elif media_type in ['tv-episodes']:
|
||||
return #import_from_tvdb_by_details(item, item_title, media_type)
|
||||
elif media_type in ['books']:
|
||||
return #import_from_openlibrary_by_details(item, item_title, media_type)
|
||||
|
||||
|
||||
def import_from_tmdb_by_details(item, item_title, media_type):
|
||||
"""Retrieve a film or TV series from TMDB using its title"""
|
||||
|
||||
logging.info(f"Processing {item['Title']}…")
|
||||
|
||||
api_url = f"https://api.themoviedb.org/3/search/{'movie' if 'films' === media_type else 'tv'}"
|
||||
|
||||
# Sending API request
|
||||
response = requests.get(
|
||||
api_url,
|
||||
params={
|
||||
'query': item_title¸
|
||||
'include_adult': True,
|
||||
'year': item['Release Year'] if 'Release Year' in item else None
|
||||
},
|
||||
headers={'Authorization': f"Bearer {TMDB_API_KEY}"}
|
||||
)
|
||||
|
||||
# Process the response
|
||||
if (200 == response.status_code):
|
||||
logging.info(response.status_code)
|
||||
elif (429 == response.status_code):
|
||||
time.sleep(2)
|
||||
import_from_tmdb_by_details(item)
|
||||
else:
|
||||
logging.error(response.text)
|
||||
|
||||
response_data = json.loads(response.text)['results']
|
||||
|
||||
if 1 == len(response_data):
|
||||
return cleanup_result(response_data[0])
|
||||
|
||||
elif 0 == len(response_data):
|
||||
logging.warning(f"Returned no {media_type} for {item['Title']} ({item['Release Year']})")
|
||||
|
||||
elif 1 < len(response_data):
|
||||
response_data = [film for film in response_data if film['title'] == item['Title']]
|
||||
|
||||
if 1 == len(response_data):
|
||||
return cleanup_result(response_data[0])
|
||||
|
||||
else:
|
||||
logging.warning(f"Returned more than one {media_type} for '{item_title}':\n")
|
||||
print(json.dumps(response_data, indent=4))
|
||||
idx = input("\nEnter the index of the result to use: ")
|
||||
|
||||
if "" != idx:
|
||||
try:
|
||||
return cleanup_result(response_data[idx])
|
||||
except:
|
||||
logging.error("Index invalid!")
|
||||
print("Index invalid!")
|
||||
|
||||
item['IMDB ID'] = input(f"Enter IMDB ID for {item['Title']} ({item['Release Year']}):")
|
||||
|
||||
if '' != item['IMDB ID']:
|
||||
return import_by_id(item['IMDB_ID'], media_type)
|
||||
else:
|
||||
logging.warning(f"Skipped {item['Title']} ({item['Release Year']})")
|
||||
return item
|
||||
|
||||
|
||||
logging.basicConfig(filename='./logs/run.log', encoding='utf-8', level=logging.DEBUG)
|
||||
|
||||
load_dotenv()
|
||||
|
||||
TMDB_API_KEY = os.getenv('TMDB_API_KEY')
|
||||
TVDB_API_KEY = os.getenv('TVDB_API_KEY')
|
||||
OPENLIBRARY_API_KEY = os.getenv('OPENLIBRARY_API_KEY')
|
||||
|
||||
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")
|
||||
|
||||
media_type = ''
|
||||
while media_type not in ['films', 'tv-episodes', 'tv-series', 'books']:
|
||||
media_type = input("Select media type [films|tv-episodes|tv-series|books]:")
|
||||
|
||||
if 'films' == media_type:
|
||||
log = ''
|
||||
while log not in ['log', 'wishlist']:
|
||||
log = input ("Enter log to process [log|wishlist]:")
|
||||
|
||||
process_log(media_type, log)
|
||||
|
||||
elif 'books' == media_type:
|
||||
log = ''
|
||||
while log not in ['log', 'current', 'wishlist']:
|
||||
log = input ("Enter log to update [log|current|wishlist]:")
|
||||
|
||||
elif 'tv-episodes' == media_type:
|
||||
process_log(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]:")
|
||||
|
||||
process_log(media_type, log)
|
||||
|
||||
|
Loading…
Reference in a new issue