improve scripts

This commit is contained in:
Ben Goldsworthy 2024-01-12 22:36:10 +01:00
parent 57a788e721
commit 3baaf67f51
2 changed files with 98 additions and 3 deletions

View file

@ -28,6 +28,7 @@ def import_film(imdb_id, log):
elif (429 == response.status_code):
time.sleep(2)
import_film(imdb_id)
return
else:
logging.error(response.text)
@ -86,6 +87,87 @@ def import_film(imdb_id, log):
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']
@ -99,7 +181,19 @@ def cleanup_film(film):
return film
logging.basicConfig(filename='../logs/run.log', encoding='utf-8', level=logging.DEBUG)
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']:

View file

@ -4,6 +4,7 @@ import requests
import time
from urllib.request import urlopen
def process_items(items):
logging.info("Processing items…")
item_values = {}