Skip to content

Instantly share code, notes, and snippets.

@robwiss
Created December 3, 2017 16:39
Show Gist options
  • Save robwiss/3e23450e5cce18532b368aea4fed368f to your computer and use it in GitHub Desktop.
Save robwiss/3e23450e5cce18532b368aea4fed368f to your computer and use it in GitHub Desktop.
get movie posters from themoviedb.com for mp4 files in a directory
# gets movie posters from themoviedb.com for mp4 files in a directory
# MIT License
import os
import requests
import time
import tmdbsimple as tmdb
tmdb.API_KEY = 'YOUR_API_KEY_HERE'
directory = '/movies' # expects to have movies named with format '<Name> (<Year>).mp4'
search = tmdb.Search()
for root, dirs, files in os.walk(directory):
for name in files:
if name.endswith('.mp4'):
title_year = os.path.splitext(name)[0]
temp = title_year.split()
year = temp[-1].strip('()')
title = ' '.join(temp[:-1])
else:
continue
if os.path.exists(os.path.join(root, '%s.jpg' % title_year)):
print 'Skipping %s' % title
continue # we already have the cover image
response = search.movie(query=title, year=year)
if response['total_results'] < 1:
print "Error, no results for %s" % title
continue
s = response['results'][0]
poster_url = 'http://image.tmdb.org/t/p/original/%s' % s['poster_path']
print 'Downloading poster for %s: %s...' % (title, poster_url),
response = requests.get(poster_url)
poster_path = os.path.join(root, '%s.jpg' % title_year)
with open(poster_path, 'wb') as f:
f.write(response.content)
print 'OK'
time.sleep(0.2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment