Last active
March 14, 2021 18:48
-
-
Save cdgriffith/7c86be2a1eaa9bace2dc14e90daffbab to your computer and use it in GitHub Desktop.
Download Movie Posters from imdb
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
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
""" | |
Requires python3 and requests library (pip install requests) | |
Provide the folder containing all the other folders of movies as the only arguement. | |
python3 download_movie_posters.py "C:\My Movies" | |
""" | |
from urllib.parse import quote | |
import json | |
from pathlib import Path | |
import traceback | |
import sys | |
import requests | |
def download_poster(folder): | |
title = folder.name | |
r = requests.get(f'https://sg.media-imdb.com/suggests/{title.strip().lower()[0]}/{quote(title)}.json') | |
r.raise_for_status() | |
res = json.loads(r.text[r.text.find("({") + 1:-1]) | |
# sections in descending order or preference | |
if len(res.get("d", [])) > 0: | |
try: | |
image = requests.get(res["d"][0]["i"][0]) | |
except Exception: | |
traceback.print_exc() | |
print(f"Could not download {title} - {res}") | |
return | |
with open(folder / "cover.jpg", "wb") as f: | |
f.write(image.content) | |
print(f"Downloaded {title}") | |
else: | |
print(f"no title found for {title}") | |
def main(directory): | |
for subdir in Path(directory).iterdir(): | |
if subdir.is_dir(): | |
if not (subdir / "cover.jpg").exists() and not (subdir / "poster.jpg").exists(): | |
try: | |
download_poster(subdir) | |
except Exception: | |
traceback.print_exc() | |
print(f"Error for directory {subdir.name}") | |
continue | |
if __name__ == "__main__": | |
main(sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment