diff options
Diffstat (limited to 'imdb-lookup/imdbinfo.py')
-rwxr-xr-x | imdb-lookup/imdbinfo.py | 39 |
1 files changed, 38 insertions, 1 deletions
diff --git a/imdb-lookup/imdbinfo.py b/imdb-lookup/imdbinfo.py index 259b2c6..bb98414 100755 --- a/imdb-lookup/imdbinfo.py +++ b/imdb-lookup/imdbinfo.py @@ -11,6 +11,7 @@ import base64 import argparse import math import logging +import hashlib try: from StringIO import StringIO except: from io import StringIO @@ -21,6 +22,7 @@ try: from PIL import Image import tmdbsimple as tmdb import requests + import imdbpie except ImportError as e: print(u"Missing dependency: {0}".format(str(e))) print(u"Install using system package manager or `pip install --user <module>`") @@ -45,6 +47,7 @@ class TMDBCache(object): self.logger.info("Open db") self.db = dbm.open(self._get_db_filename("tmdbmovie.dbm"),"c") self.db_images = dbm.open(self._get_db_filename("tmdbposter.dbm"), "c") + self.imdb = imdbpie.Imdb({'cache':False}) return self def __exit__(self, type, value, traceback): @@ -86,7 +89,11 @@ class TMDBCache(object): raise Exception("Failed to query movie with id {id}: {reason}".format(id=movie_id, reason=str(e))) def prune(self, movie_id): - keys = [movie_id + "movies.info", movie_id + "movies.alt_titles"] + keys = [ + "imdb_maindetails_{}".format(movie_id), + movie_id + "movies.info", + movie_id + "movies.alt_titles", + ] for key in keys: if key in self.db: print("Remove {}".format(key)) @@ -114,6 +121,35 @@ class TMDBCache(object): filter(lambda c: c!='\n', base64.encodestring(buf.getvalue())))) return "data:{};base64,{}".format(contentType, data64) + def _imdb_request(self, path, query): + # see also https://github.com/richardasaurus/imdb-pie + # nice library but a bit strange API, so we chose to reimplement stuff here + BASE_URI = 'app.imdb.com' + API_KEY = '2wex6aeu6a8q9e49k7sfvufd6rhh0n' + SHA1_KEY = hashlib.sha1(API_KEY.encode('utf8')).hexdigest() + HEADERS = { + 'user-agent' : 'AppleWebKit/534.46 (KHTML, like Gecko) Ver sion/5.1 Mobile/9A405', + } + PARAMS = { + "api": "v1", + "appid": "iphone1_1", + "apiPolicy": "app1_1", + "apiKey": SHA1_KEY, + "locale": "en_US", + "timestamp": "{:.0f}".format(time.time()) + } + q = query.copy() + q.update(PARAMS) + return requests.get("http://{}/{}".format(BASE_URI, path), params=q, headers=HEADERS) + + def imdb_movie(self, movie_id): + def do_request(): + r = self._imdb_request("/title/maindetails", {'tconst': movie_id}) + assert r.status_code == 200, "Request must return status-code 200" + return json.loads(r.content) + key = "imdb_maindetails_{}".format(movie_id) + return self._cache(key, do_request) + def weight_rating(infos): """ add 'rating' to all infos""" @@ -204,6 +240,7 @@ def do_index(args, imdb_ids): 'urlencode' : urlencode, 'int' : int, 'listMovieFiles' : listMovieFiles, + 'math' : math, } assert not os.path.exists("index.html"), "index.html already exists" stream = template.generate(mapping) |