Created
February 7, 2022 09:08
-
-
Save plibither8/fa4357f2195ef750285077093535f848 to your computer and use it in GitHub Desktop.
Introduction to Programming API Examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
API_KEY = "<API-Key>" | |
BASE_URL = "http://ws.audioscrobbler.com/2.0" | |
# What was the fifth best album by Sonu Nigam? | |
def example_1(): | |
url = f"{BASE_URL}?method=artist.gettopalbums&artist=Sonu+Nigam&api_key={API_KEY}&format=json" | |
data = requests.get(url).json() | |
album = data["topalbums"]["album"][4] | |
print(album["name"]) | |
example_1() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime | |
import requests | |
API_KEY = "<API-Key>" | |
BASE_URL = "https://api.themoviedb.org/3" | |
# Who was the playback singer of the Rajkumar Hirani's 3rd film? | |
def example_1(): | |
url = f"{BASE_URL}/search/person?api_key={API_KEY}&query=Rajkumar+Hirani" | |
data = requests.get(url).json() | |
person = data["results"][0] | |
url = f"{BASE_URL}/person/{person['id']}/movie_credits?api_key={API_KEY}" | |
data = requests.get(url).json() | |
movies = sorted( | |
filter(lambda x: x["job"] == "Director" and "release_date" in x, data["crew"]), | |
key=lambda x: datetime.strptime(x["release_date"], "%Y-%m-%d"), | |
) | |
print(movies[2]["title"]) | |
url = f"{BASE_URL}/movie/{movies[2]['id']}?api_key={API_KEY}&append_to_response=credits" | |
data = requests.get(url).json() | |
singer = next(s for s in data["credits"]["crew"] if s["job"] == "Playback Singer") | |
print(singer["name"]) | |
# What was the first movie directed by the director who made the 3rd-highest grossing English comedy of 2011? | |
def example_2(): | |
url = f"{BASE_URL}/genre/movie/list?api_key={API_KEY}" | |
data = requests.get(url).json() | |
genre = next(g for g in data["genres"] if g["name"] == "Comedy") | |
url = f"{BASE_URL}/discover/movie?api_key={API_KEY}&primary_release_year=2011&sort_by=revenue.desc&with_genres={genre['id']}&with_original_language=en" | |
data = requests.get(url).json() | |
movie = data["results"][2] | |
print(movie["title"]) | |
url = f"{BASE_URL}/movie/{movie['id']}?api_key={API_KEY}&append_to_response=credits" | |
data = requests.get(url).json() | |
director = next(d for d in data["credits"]["crew"] if d["job"] == "Director") | |
print(director["name"]) | |
url = f"{BASE_URL}/person/{director['id']}/movie_credits?api_key={API_KEY}" | |
data = requests.get(url).json() | |
movies = sorted( | |
filter(lambda x: x["job"] == "Director" and "release_date" in x, data["crew"]), | |
key=lambda x: datetime.strptime(x["release_date"], "%Y-%m-%d"), | |
) | |
print(movies[0]["title"]) | |
example_1() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment