Skip to content

Instantly share code, notes, and snippets.

Created November 11, 2012 17:51
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Create a 'pretty' webpage for all of the moives in a directory by getting the movie poster from IMDB.
Create a 'pretty' webpage for all of the moives in a directory by getting the
movie poster from IMDB.
Since it depends on the OMDB API, not all posters that are returned are correct.
That being said, it's good enough for a start. =)
from contextlib import contextmanager
import glob
import json
import logging
import os
import os.path
import re
import sys
import urllib2
logging.basicConfig(level=logging.INFO, format='%(message)s')
def closing(thing):
"""Context manager that makes sure a resource is closed."""
yield thing
def download_file(url, file_path):
"""Download a file."""
with closing(urllib2.urlopen(url)) as u, open(file_path, 'w') as f:
except Exception, ex:
logging.warning("Could not download file: {}".format(ex))
def get_movies_in_dir(movie_dir):
"""Take a directory full of movies and return a list of their info."""
movies = []
for movie_file in glob.glob(os.path.join(movie_dir, '*')):
return movies
def get_movie_info_from_filepath(file_path):
"""Given the name of a movie file, get all of the relevant info.
Given a (movie) file of the form MOVIE_NAME (YEAR).avi, return a dict
containing info about each movie.
file_name = os.path.split(file_path)[1]
title = file_name.split('.')[0]
# Get the year, and then remove it from the title
year = None
matches ='\((\d{4})\)', title)
if matches:
year =
title = re.sub(r'\(\d{4}\)', '', title).strip()
return dict(title=title, year=year, path=file_path, file_name=file_name)
def create_page(movies, output_dir):
"""Create a web page for all of the movies.
Given the list of movies as generated by get_movies_in_dir(),
and create a web page containing all of the posters for those movies. The
movie and poster information is obtained by using the OMDB API.
html_file_path = os.path.join(output_dir, 'index.html')
poster_dir_name = 'posters'
poster_dir = os.path.join(output_dir, poster_dir_name)
# Create the necessary output directories
if not os.path.exists(output_dir):
if not os.path.exists(poster_dir):
with open(html_file_path, 'w') as html_file:
'<style media="screen" type="text/css">\n'\
'.movie {margin: 5px; padding: 5px; float: left; '\
'width: 200px; height: 375px}\n'\
'.movie-poster img {width: 200px;}\n'\
'.movie-title {font-weight: bold; color: #666666; '\
'font-size: large; margin-top: 5px}\n'\
'.movie-title a {color: #666666;}\n'\
# Loop over all movies
for movie in movies:
# Get the data from OMDB API
url = "{}"\
.format(movie['title'].replace(' ', '+'))
with closing(urllib2.urlopen(url)) as u:
response = json.loads(
except Exception, ex:
"Could not retrieve movie information: {}".format(ex))
movie_file_name = movie['file_name'].split('.')[0]
# Download the poster
poster_file_path = None
if 'Poster' in response and response['Poster'] != 'N/A':
poster_file_path = os.path.join(poster_dir,
"{}.jpg".format(movie_file_name))"Downloading poster {}".\
download_file(response['Poster'], poster_file_path)
poster_html = ''
if poster_file_path and os.path.exists(poster_file_path):
rel_path = os.path.join(poster_dir_name,
poster_html = '<div class="movie-poster">'\
'<img width="200px" src="{}" />'\
link_html = None
if 'imdbID' in response:
link_html = '<a href="{}/">{}</a>'\
.format(response['imdbID'], movie_file_name)
link_html = movie_file_name
# Write out the HTML for this movie
'<div class="movie">'\
'{}<div class="movie-title">{}</div>'\
'</div>\n'.format(poster_html, link_html))
if __name__ == '__main__':
usage = """
python moviepagemaker [movie_dir] [output_dir]
if not len(sys.argv) == 3:
print usage
movie_dir = sys.argv[1]
output_dir = sys.argv[2]
# Get the movies"Getting movies from {}".format(movie_dir))
movies = get_movies_in_dir(movie_dir)
if len(movies) < 1:
logging.error("There seem to be no movies in {}".format(movie_dir))
# Create the page"Creating the page for {} movies".format(str(len(movies))))
create_page(movies, output_dir)"Done!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment