I'm building a program that retrieves the poster of a movie or TV from a list of titles. For some reason, about 50% of the titles my program searches return no results when searched but when I use the exact same terms that my program is using on the actual website, it shows the exact results I'm looking for. Note that these titles are consistent and the response code to the query is always 200. Does anyone have any insight?
Some titles that return nothing: MH370: The Plane That Disappeared, Waco: American Apocalypse, I Am Georgina
Here's my code:
import requests
api_key = "key"
base_url = "https://api.themoviedb.org/3"
headers = {"Authorization": f"Bearer {api_key}"}
title = "title of show or movie"
queryDB = title
params = {
"api_key": api_key,
"query": queryDB,
"language": "en-US",
"include_adult": False
}
# Make request to search for movie
response = requests.get(f"{base_url}/search/movie", headers=headers, params=params)
response.raise_for_status() # Raise exception if request fails
# Get first result from search
results = response.json()["results"]
if not results:
# No results found for search query
poster_url = None
print("no results found")
else:
# Get poster image URL from movie details
movie_id = results[0]["id"]
response = requests.get(f"{base_url}/movie/{movie_id}", headers=headers, params=params)
response.raise_for_status() # Raise exception if request fails
poster_url = f"https://image.tmdb.org/t/p/original{response.json()['poster_path']}"
Can't find a movie or TV show? Login to create it.
Want to rate or add this item to a list?
Not a member?
Reply by superboy97
on March 28, 2023 at 3:25 AM
Did you URI encode your search string ? https://developers.themoviedb.org/3/search/search-movies
Reply by Buhlahkay
on March 28, 2023 at 7:27 PM
I didn't at the time but I rewrote my code to do just that. I'm still getting no results found. I used the "try it out" tab here https://developers.themoviedb.org/3/search/search-movies to generate an encoded URL that I could compare my encoded URL to and they match. I'd share my code but for some reason the site won't let me post it.