Created
November 11, 2013 13:52
-
-
Save baderj/7413477 to your computer and use it in GitHub Desktop.
return IMDb movie id for search string using the undocumented IMDb API
This file contains 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
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