Skip to content

Instantly share code, notes, and snippets.

@prschmid
Created November 11, 2012 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prschmid/4055684 to your computer and use it in GitHub Desktop.
Save prschmid/4055684 to your computer and use it in GitHub Desktop.
Create a 'pretty' webpage for all of the moives in a directory by getting the movie poster from IMDB.
#!/usr/bin/python
"""
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')
@contextmanager
def closing(thing):
"""Context manager that makes sure a resource is closed."""
try:
yield thing
finally:
thing.close()
def download_file(url, file_path):
"""Download a file."""
try:
with closing(urllib2.urlopen(url)) as u, open(file_path, 'w') as f:
f.write(u.read())
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, '*')):
movies.append(get_movie_info_from_filepath(movie_file))
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 = re.search('\((\d{4})\)', title)
if matches:
year = matches.group(1)
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):
os.makedirs(output_dir)
if not os.path.exists(poster_dir):
os.makedirs(poster_dir)
with open(html_file_path, 'w') as html_file:
html_file.write('<html>\n<head>\n'\
'<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'\
'</style>\n</head>\n<body>\n')
# Loop over all movies
for movie in movies:
# Get the data from OMDB API
url = "http://www.omdbapi.com/?t={}"\
.format(movie['title'].replace(' ', '+'))
try:
with closing(urllib2.urlopen(url)) as u:
response = json.loads(u.read())
except Exception, ex:
logging.warning(
"Could not retrieve movie information: {}".format(ex))
continue
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))
logging.info("Downloading poster {}".\
format(poster_file_path))
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,
os.path.split(poster_file_path)[1])
poster_html = '<div class="movie-poster">'\
'<img width="200px" src="{}" />'\
'</div>'.format(rel_path)
link_html = None
if 'imdbID' in response:
link_html = '<a href="http://www.imdb.com/title/{}/">{}</a>'\
.format(response['imdbID'], movie_file_name)
else:
link_html = movie_file_name
# Write out the HTML for this movie
html_file.write(
'<div class="movie">'\
'{}<div class="movie-title">{}</div>'\
'</div>\n'.format(poster_html, link_html))
html_file.write('</body>\n</html>\n')
if __name__ == '__main__':
usage = """
Usage:
python moviepagemaker [movie_dir] [output_dir]
"""
if not len(sys.argv) == 3:
print usage
sys.exit(0)
movie_dir = sys.argv[1]
output_dir = sys.argv[2]
# Get the movies
logging.info("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))
sys.exit(0)
# Create the page
logging.info("Creating the page for {} movies".format(str(len(movies))))
create_page(movies, output_dir)
logging.info("Done!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment