Last active
February 7, 2022 13:06
-
-
Save arshamalh/1fe6adc2ae3fb16e6783a864b105b214 to your computer and use it in GitHub Desktop.
Getting TMDB trends using their API
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
""" | |
Written by Arsham Arya | |
I'm not sure it's the best way, | |
So any contribution makes everyone happy! | |
""" | |
# pip install requests | |
from requests import get | |
# Config is can is a dictionary of ENV variables including TMDB_API_KEY | |
# https://gist.github.com/arshamalh/16a42854cb43bbe48038c7431529a552 | |
from config import config | |
def get_tmdb_trend_movies( | |
media_type: str = "all", time_window: str = "day", number_of_results: int = 1000 | |
): | |
""" | |
:param number_of_results: | |
:param media_type: | |
tv => Trending TV shows in the results. | |
all => All movies, TV shows and people. | |
movie => Trending movies in the results. | |
person => Trending people in the results. | |
:param time_window: day, week. | |
:return: stream wanted number of trending movies | |
""" | |
for page_number in range(1, number_of_results // 20 + 1): | |
response = get( | |
f"https://api.themoviedb.org/3/trending/{media_type}/{time_window}?api_key={config.get('TMDB_API_KEY')}&page={page_number}" | |
) | |
movies = response.json().get("results") | |
skipped = (page_number - 1) * 20 | |
for rank, movie in enumerate(movies, start=1): | |
movie["rank"] = skipped + rank | |
yield movie |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment