Last active
October 18, 2022 17:02
-
-
Save baderj/7414775 to your computer and use it in GitHub Desktop.
Example how to use the API v3 of "The Movie Database" (themoviedb.org). Saves all available posters for the movie with IMDb id tt0095016 to the current folder, using the maximum available resolution.
This file contains 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 os | |
import requests | |
CONFIG_PATTERN = 'http://api.themoviedb.org/3/configuration?api_key={key}' | |
IMG_PATTERN = 'http://api.themoviedb.org/3/movie/{imdbid}/images?api_key={key}' | |
KEY = '<your_api_key>' | |
def _get_json(url): | |
r = requests.get(url) | |
return r.json() | |
def _download_images(urls, path='.'): | |
"""download all images in list 'urls' to 'path' """ | |
for nr, url in enumerate(urls): | |
r = requests.get(url) | |
filetype = r.headers['content-type'].split('/')[-1] | |
filename = 'poster_{0}.{1}'.format(nr+1,filetype) | |
filepath = os.path.join(path, filename) | |
with open(filepath,'wb') as w: | |
w.write(r.content) | |
def get_poster_urls(imdbid): | |
""" return image urls of posters for IMDB id | |
returns all poster images from 'themoviedb.org'. Uses the | |
maximum available size. | |
Args: | |
imdbid (str): IMDB id of the movie | |
Returns: | |
list: list of urls to the images | |
""" | |
config = _get_json(CONFIG_PATTERN.format(key=KEY)) | |
base_url = config['images']['base_url'] | |
sizes = config['images']['poster_sizes'] | |
""" | |
'sizes' should be sorted in ascending order, so | |
max_size = sizes[-1] | |
should get the largest size as well. | |
""" | |
def size_str_to_int(x): | |
return float("inf") if x == 'original' else int(x[1:]) | |
max_size = max(sizes, key=size_str_to_int) | |
posters = _get_json(IMG_PATTERN.format(key=KEY,imdbid=imdbid))['posters'] | |
poster_urls = [] | |
for poster in posters: | |
rel_path = poster['file_path'] | |
url = "{0}{1}{2}".format(base_url, max_size, rel_path) | |
poster_urls.append(url) | |
return poster_urls | |
def tmdb_posters(imdbid, count=None, outpath='.'): | |
urls = get_poster_urls(imdbid) | |
if count is not None: | |
urls = urls[:count] | |
_download_images(urls, outpath) | |
if __name__=="__main__": | |
tmdb_posters('tt0095016') |
Is it possible for anyone to fork this script to just downlead posters from the upcoming TMDB section
Hers is the API call.
https://api.themoviedb.org/3/movie/upcoming?api_key='yourapikey'
Very straightforward, just look at the JSON response for the API request. Each entry has a poster_path
which works like the file_path
in the original Gist. For example:
import requests
API_KEY = "..."
""" get the config for the image base path and available sizes """
url = f"http://api.themoviedb.org/3/configuration?api_key={API_KEY}"
r = requests.get(url)
config = r.json()
base_url = config['images']['base_url']
sizes = config['images']['poster_sizes']
""" get upcoming movies """
url = f"https://api.themoviedb.org/3/movie/upcoming?api_key={API_KEY}"
r = requests.get(url)
data = r.json()
""" download the poster in the best quality (last element in sizes
for the last 5 movies """
for r in data["results"][:5]:
poster_path = r["poster_path"]
url = base_url + sizes[-1] + poster_path
r = requests.get(url)
with open(poster_path.strip("/"), "wb") as w:
w.write(r.content)
Perfect thank you.
Russ
…On Tue, Oct 18, 2022, 5:51 AM Johannes Bader ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
Is it possible for anyone to fork this script to just downlead posters
from the upcoming TMDB section
Hers is the API call.
https://api.themoviedb.org/3/movie/upcoming?api_key='yourapikey'
Very straightforward, just look at the JSON response for the API request.
Each entry has a poster_path which works like the file_path in the
original Gist. For example:
import requests
API_KEY = "..."
""" get the config for the image base path and available sizes """url = f"http://api.themoviedb.org/3/configuration?api_key={API_KEY}"r = requests.get(url)config = r.json()base_url = config['images']['base_url']sizes = config['images']['poster_sizes']
""" get upcoming movies """url = f"https://api.themoviedb.org/3/movie/upcoming?api_key={API_KEY}"r = requests.get(url)data = r.json()
""" download the poster in the best quality (last element in sizesfor the last 5 movies """for r in data["results"][:5]:
poster_path = r["poster_path"]
url = base_url + sizes[-1] + poster_path
r = requests.get(url)
with open(poster_path.strip("/"), "wb") as w:
w.write(r.content)
—
Reply to this email directly, view it on GitHub
<https://gist.github.com/7414775#gistcomment-4339658>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AK7W2Z3AEC74WTWNRYETNPTWDZ6LRANCNFSM6AAAAAARHTYSJ4>
.
You are receiving this because you commented.Message ID:
***@***.***>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is it possible for anyone to fork this script to just downlead posters from the upcoming TMDB section
Hers is the API call.
https://api.themoviedb.org/3/movie/upcoming?api_key='yourapikey'