From fba188c746e7326de9d662cfac3b548466af90b3 Mon Sep 17 00:00:00 2001 From: Yves Fischer Date: Tue, 25 Nov 2014 23:03:37 +0100 Subject: imdb data --- imdb-lookup/imdbinfo.py | 39 ++++++++++++++++++++++++++++++++++++++- imdb-lookup/index.jinja2.html | 39 ++++++++++++++++++++++++--------------- 2 files changed, 62 insertions(+), 16 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 `") @@ -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) diff --git a/imdb-lookup/index.jinja2.html b/imdb-lookup/index.jinja2.html index 6eae465..ff94348 100644 --- a/imdb-lookup/index.jinja2.html +++ b/imdb-lookup/index.jinja2.html @@ -16,14 +16,16 @@ .error { background-color: #EFAAAA; } - .td1, .td2, .td3 { + .td1, .td2, .td3, .td4 { width: 1em; font-size: 18pt; padding: 5px; + vertical-align: top; } .td1 .on { color: #ffd700; } .td2 .on { color: blue; } .td1, .td2 .off { color: gray; } + .td1 .num, .td2 .num { font-size: 12pt; color: gray; } .poster { margin: 10px; width: 185px; @@ -51,23 +53,30 @@ {% set age = gmtime.tm_year - int(info['release_date'].split('-')[0]) %} {% set centuries = int(age / 10) %} {% set posterBase64 = tmdbcache.poster_base64(info['poster_path']) %} + {% set imdb_data = tmdbcache.imdb_movie(imdb_id)['data'] %} + {% set votes = imdb_data['num_votes'] %} + {% set popularity = int( 9*(1 - (1+math.exp(-1))/(1+math.exp(votes/70000.0 -1 ))) + 1 ) %}
{% if "vote_average" in info.keys() %} - + - - {% endif %} {% endif %} - {% if 'homepage' in info %} + {% if 'homepage' in info and info['homepage'] %} @@ -104,13 +113,13 @@
+ {{ " ".join(["☆"] * (10-int(info["vote_average"]))) }} {{ " ".join(["★"] * int(info["vote_average"])) }} + {{info["vote_average"]}} + + {{ " ".join(["☆"] * (10-int(imdb_data["rating"]))) }} + {{ " ".join(["★"] * int(imdb_data["rating"])) }} + {{imdb_data["rating"]}} - {{ " ".join(["  "] * (10-centuries)) }} - {{ " ".join(["⌚"] * centuries) }} + + {{ " ".join(["  "] * (9-centuries)) }} + {{ " ".join(["⌚"] * (centuries + 1)) }} @@ -92,7 +101,7 @@ {{ info['runtime'] }}
Homepage {{ info['homepage'] }}
- → themoviedb.org + → themoviedb.org   - → imdb.com + → imdb.com   - → letterboxd.com + → letterboxd.com   - → ofdb.db + → ofdb.db
-- cgit v1.2.1