All my code is below. It uses an API to search for a show and then outputs the providers (like breaking bad is available on Netflix).
The main aim of this application was to practice using APIs and object oriented programming as I have been struggling and not very confident in my skill of using Object Oriented Principles. So any advice on what I did right or wrong would be helpful.
'''
An app to find the providers of a certain movie or TV show by searching its name.
'''
import requests
def get_API_key():
# Gets the API key
return "<<an API key>>"
def searchshow(key, search_term, page=1):
# Takes a user's search term and returns the appropriate page of results.
response = requests.get("https://api.themoviedb.org/3/search/tv?api_key="+key+"&query="+search_term+"&page="+str(page))
return response.json()
def searchresults(j, total_results):
# Looks at whether the results are multiple or if there's no results. Quits the program if there are no results or displays the results with a new function.
if total_results==0:
print("No search results, try again.")
quit()
elif total_results>1:
display_results(j, total_results)
def display_results(j, total_results):
# Displays all the results
for i in range(total_results):
print(str(i) + ". " + j["results"][i]["name"])
print(j["results"][i]["overview"])
print()
def choose_show(j, total_results):
# Let's the user pick a show if there are multiple choices else it chooses the only show possible if there's only one choice.
if total_results==1:
num = 0
else:
num = int(input("Please choose a show: "))
show_name = j["results"][num]["name"]
show_id = str(j["results"][num]["id"])
show_overview = j["results"][num]["overview"]
show_details = [show_name, show_id, show_overview]
print(show_name)
print(show_id)
print(show_overview)
return show_details
def get_all_providers(show_id, api_key):
response = requests.get("https://api.themoviedb.org/3/tv/"+show_id+"/watch/providers?api_key="+api_key)
return response.json()
def types_of_providers(j, country):
# Provides a list of the types of providers
providers = []
for i in j["results"][country]:
providers.append(i)
providers.pop(0)
return providers
def display_providers(j, country, type_of_provider):
# Displays the providers of each type of provider
print("For " + type_of_provider + ":")
for i in range(len(j["results"][country][type_of_provider])):
print(j["results"][country][type_of_provider][i]["provider_name"])
def main():
api_key = get_API_key()
search_term = input("Enter the TV show or movie you'd like to see the providers for: ")
j = searchshow(api_key, search_term)
total_results = j["total_results"]
searchresults(j, total_results)
show_details = choose_show(j, total_results)
j = get_all_providers(show_details[1], api_key)
country = input("Enter your country code: ")
providers = types_of_providers(j, country)
print()
print("Provided on")
for i in providers:
display_providers(j, country, i)
if __name__ == "__main__":
main()