diff options
author | Yves Fischer <yvesf-git@xapek.org> | 2014-11-25 23:03:37 +0100 |
---|---|---|
committer | Yves Fischer <yvesf-git@xapek.org> | 2014-11-25 23:03:37 +0100 |
commit | fba188c746e7326de9d662cfac3b548466af90b3 (patch) | |
tree | add0e323e67caa63e056deede1fd2901267015eb | |
parent | 42411217a9a12d7fae7ec2b5fe6f0f8ee43c5d42 (diff) | |
download | scripts-fba188c746e7326de9d662cfac3b548466af90b3.tar.gz scripts-fba188c746e7326de9d662cfac3b548466af90b3.zip |
imdb data
-rwxr-xr-x | imdb-lookup/imdbinfo.py | 39 | ||||
-rw-r--r-- | 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 <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) 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 ) %} <div class="box"> <table> <tr> {% if "vote_average" in info.keys() %} - <td class="td1"> + <td class="td1" title="TMDB Rating"> <span class="off">{{ " ".join(["☆"] * (10-int(info["vote_average"]))) }}</span> <span class="on">{{ " ".join(["★"] * int(info["vote_average"])) }}</span> + <span class="num">{{info["vote_average"]}}</span> + </td> + <td class="td2" title="IMDB Rating"> + <span class="off">{{ " ".join(["☆"] * (10-int(imdb_data["rating"]))) }}</span> + <span class="on">{{ " ".join(["★"] * int(imdb_data["rating"])) }}</span> + <span class="num">{{imdb_data["rating"]}}</span> </td> - <!-- - <td class="td2"> - <span class="off">{{ " ".join(["☆"] * (10-int(info["popularity"]*10))) }}</span> - <span class="on">{{ " ".join(["★"] * int(info["popularity"]*10)) }}</span> + <td class="td3" title="IMDB Popularity"> + <span class="off">{{ " ".join(["☆"] * (10-popularity)) }}</span> + <span class="on">{{ " ".join(["★"] * popularity) }}</span> </td> - --> - <td class="td3"> - <span class="off">{{ " ".join([" "] * (10-centuries)) }}</span> - <span class="on">{{ " ".join(["⌚"] * centuries) }}</span> + <td class="td4" title="Centuries since release"> + <span class="off">{{ " ".join([" "] * (9-centuries)) }}</span> + <span class="on">{{ " ".join(["⌚"] * (centuries + 1)) }}</span> </td> {% endif %} <td class="poster"> @@ -92,7 +101,7 @@ <td>{{ info['runtime'] }}</td> </tr> {% endif %} - {% if 'homepage' in info %} + {% if 'homepage' in info and info['homepage'] %} <tr> <td style="font-style: italic">Homepage</td> <td><a href="{{ info['homepage'] }}">{{ info['homepage'] }}</a></td> @@ -104,13 +113,13 @@ </tr> <tr> <td colspan="2"> - <a href="http://www.themoviedb.org/movie/{{ info.id }}" rel="noreferrer">→ themoviedb.org</a> + → <a href="http://www.themoviedb.org/movie/{{ info.id }}" rel="noreferrer">themoviedb.org</a>   - <a href="http://www.imdb.com/title/{{ info.imdb_id }}" rel="noreferrer">→ imdb.com</a> + → <a href="http://www.imdb.com/title/{{ info.imdb_id }}" rel="noreferrer">imdb.com</a>   - <a href="http://letterboxd.com/tmdb/{{ info.id }}" rel="noreferrer">→ letterboxd.com</a> + → <a href="http://letterboxd.com/tmdb/{{ info.id }}" rel="noreferrer">letterboxd.com</a>   - <a href="http://www.ofdb.de/view.php?page=suchergebnis&Kat=IMDb&SText={{ info.imdb_id }}" rel="noreferrer">→ ofdb.db</a> + → <a href="http://www.ofdb.de/view.php?page=suchergebnis&Kat=IMDb&SText={{ info.imdb_id }}" rel="noreferrer">ofdb.db</a> </td> </tr> </table> |