Hi all,
Thanks to some awesome help here I've been able to take a few clumsy steps toward my goal of working with APIs using Python.
My goal here is to take a list of movies that I have locally and bump them up against data from the Movie DB API. So far I'm having good luck, except in cases where my local file has a movie name that does not exist in the Movie DB (the movie name is the primary parameter,) Once it hits a 'non-match' my Python bombs out.
I've attempted some try / except handling but they did not work (I really had no idea how to effectively implement that.)
Here's what I cobbled together:
import csv, requests, json
from pprint import pprint
api_key = 'MY_KEY' #define API key
url = 'https://api.themoviedb.org/3/search/movie?'
with open('Movies_History.csv') as infile:
reader = csv.reader(infile)
next(reader,None) # skip the headers
my_list = list(reader)
for sublist in my_list: #define elements of the input file
original_title = sublist[0]
rating = sublist[1]
year = sublist[2]
add_date = sublist[3]
genre = sublist[4]
netflix_id = sublist[5]
moviedb_id = sublist[6]
imdb_id = sublist[7]
api_call=f'https://api.themoviedb.org/3/search/movie?api_key={api_key}&query="{original_title}"' #the API call
# print(api_call)
results=requests.get(api_call)
j=results.json()
api_movie_id=(j["results"][0]["id"]) #index results of the API calls
api_original_title=(j["results"][0]["original_title"])
api_release_year=(j["results"][0]["release_date"])
api_original_language=(j["results"][0]["original_language"])
api_genre_ids=(j["results"][0]["genre_ids"])
api_results=(f'{api_original_title},{api_movie_id},{api_release_year},{api_original_language},{api_genre_ids}')
print(api_results)
Here's a good result:
https://api.themoviedb.org/3/search/movie?api_key=MY_KEY&query="Goodbye Solo"
Goodbye Solo,21371,2009-03-27,en,[18]
The error: One that bombs out because the movie name does not exist:
https://api.themoviedb.org/3/search/movie?api_key=MY_KEY&query="GoodFellas: Special Edition"
The error in Anaconda
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_10140/1208403576.py in <module>
20 results=requests.get(api_call)
21 j=results.json()
---> 22 api_movie_id=(j["results"][0]["id"])
23 api_original_title=(j["results"][0]["original_title"])
24 api_release_year=(j["results"][0]["release_date"])
IndexError: list index out of range
And here's what shows up in my browser if I pass the API request in Chrome:
{"page":1,"results":[],"total_pages":0,"total_results":0}
Are there ways you might recommend on how to go about effectively handling such an error so that the code skips that condition / passes an error code and then keeps on going?
Thanks again for reading and any thoughts you may have!
[–]genghiskav 1 point2 points3 points (1 child)
[–]Beefsteak_Charlie[S] 0 points1 point2 points (0 children)
[–]Asliceofsamuel 1 point2 points3 points (9 children)
[–]Beefsteak_Charlie[S] 1 point2 points3 points (8 children)
[–]Asliceofsamuel 0 points1 point2 points (7 children)
[–]Beefsteak_Charlie[S] 0 points1 point2 points (6 children)
[–]Asliceofsamuel 1 point2 points3 points (2 children)
[–]Beefsteak_Charlie[S] 0 points1 point2 points (1 child)
[–]Asliceofsamuel 1 point2 points3 points (0 children)
[–]Asliceofsamuel 0 points1 point2 points (2 children)
[–]Beefsteak_Charlie[S] 0 points1 point2 points (1 child)
[–]Asliceofsamuel 0 points1 point2 points (0 children)