Skip to content

Instantly share code, notes, and snippets.

@baderj
Created November 11, 2013 13:52
Show Gist options
  • Save baderj/7413477 to your computer and use it in GitHub Desktop.
Save baderj/7413477 to your computer and use it in GitHub Desktop.
return IMDb movie id for search string using the undocumented IMDb API
import requests
import urllib
def imdb_id_from_title(title):
""" return IMDb movie id for search string
Args::
title (str): the movie title search string
Returns:
str. IMDB id, e.g., 'tt0095016'
None. If no match was found
"""
pattern = 'http://www.imdb.com/xml/find?json=1&nr=1&tt=on&q={movie_title}'
url = pattern.format(movie_title=urllib.quote(title))
r = requests.get(url)
res = r.json()
# sections in descending order or preference
for section in ['popular','exact','substring']:
key = 'title_' + section
if key in res:
return res[key][0]['id']
if __name__=="__main__":
title = "Die Hard"
print("{0} has id {1}".format(title, imdb_id_from_title(title)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment