summaryrefslogtreecommitdiff
path: root/imdb-lookup/imdbinfo.py
diff options
context:
space:
mode:
Diffstat (limited to 'imdb-lookup/imdbinfo.py')
-rwxr-xr-ximdb-lookup/imdbinfo.py91
1 files changed, 74 insertions, 17 deletions
diff --git a/imdb-lookup/imdbinfo.py b/imdb-lookup/imdbinfo.py
index 970b284..9c60965 100755
--- a/imdb-lookup/imdbinfo.py
+++ b/imdb-lookup/imdbinfo.py
@@ -8,13 +8,12 @@ import re
import dbm
import json
import time
-import base64
import argparse
import math
+import shutil
import logging
import hashlib
from io import BytesIO
-from urllib.parse import quote as urlencode
try:
from PIL import Image, ImageFilter
@@ -109,7 +108,7 @@ class TMDBCache(object):
else:
self.logger.debug("Key not in db {}".format(key))
- def poster(self, poster_path, f="w185"):
+ def poster(self, poster_path, f="w154"):
self.logger.debug("poster %s", poster_path)
key = "poster_{}_{}".format(f, hashlib.md5(poster_path.encode('utf-8')).hexdigest()[0:10])
keyContentType = "{}_ct".format(key)
@@ -120,7 +119,7 @@ class TMDBCache(object):
self.db_images[keyContentType] = r.headers['content-type']
return (self.db_images[keyContentType], self.db_images[key])
- def poster_base64(self, poster_path, format="w185"):
+ def poster_low(self, poster_path, format="w154"):
p = self.poster(poster_path, format)
if not p:
return None
@@ -128,9 +127,8 @@ class TMDBCache(object):
image = Image.open(BytesIO(data))
image = image.filter(ImageFilter.GaussianBlur(radius=1))
buf = BytesIO()
- image.save(buf, "JPEG", quality=15, optimize=True)
- data64 = base64.encodestring(buf.getvalue()).translate(None, b'\n')
- return "data:{};base64,{}".format("image/jpeg", data64.decode())
+ image.save(buf, "JPEG", quality=18, optimize=True)
+ return ("image/jpeg", buf.getvalue())
def _imdb_request(self, path, query):
# see also https://github.com/richardasaurus/imdb-pie
@@ -250,7 +248,8 @@ def do_data(args, imdb_ids):
with TMDBCache() as tmdbcache:
for (filename, imdb_id) in imdb_ids:
print_data({"TMDB": tmdbcache.infos(imdb_id),
- "IMDB": tmdbcache.imdb_movie(imdb_id)}, sys.stdout)
+ "IMDB": tmdbcache.imdb_movie(imdb_id),
+ "OMDB": tmdbcache.omdb_movie(imdb_id)}, sys.stdout)
def do_year(args, imdb_ids):
@@ -286,6 +285,69 @@ def do_index(args, imdb_ids):
if sum(map(curfile.endswith, valid_extensions)):
yield os.path.join(root, curfile)
+ def install(filename):
+ outpath = os.path.join(".index.html", os.path.dirname(filename))
+ if not os.path.exists(outpath):
+ os.makedirs(outpath)
+ src = os.path.join(os.path.dirname(__file__),filename)
+ out = os.path.join(outpath, os.path.basename(filename))
+ shutil.copy(src, out)
+ return out
+
+ def data(callbackName):
+ db = Protector(tmdbcache)
+ def poster(imdb_id, data):
+ if data:
+ if not os.path.exists(".index.html/poster"):
+ os.makedirs(".index.html/poster")
+
+ out = os.path.join(".index.html/poster",imdb_id+".jpg")
+ open(out, "wb").write(data[1])
+ return out
+ else:
+ return None
+
+ def getInfo(a):
+ path, imdb_id = a
+ imdb = db.imdb_movie(imdb_id)['data']
+ omdb = db.omdb_movie(imdb_id)
+ tmdb = db.infos(imdb_id)
+ if not imdb or not omdb or not tmdb:
+ print("Error in {} {}".format(path, imdb_id))
+ return None
+ else:
+ return {
+ 'id': imdb_id,
+ 'title': tmdb['title'],
+ 'path': path,
+ 'poster': poster(imdb_id, db.poster_low(tmdb['poster_path'])),
+ 'tagline': 'tagline' in imdb and imdb['tagline'] or None,
+ 'plot': 'plot' in imdb and imdb['plot']['outline'] or None,
+ 'website': 'homepage' in imdb and imdb['homepage'] or omdb['Website'] != 'N/A' and omdb['Website'] or None,
+ 'movieFiles': list(listMovieFiles(path)),
+ 'imdbRating': imdb['rating'],
+ 'imdbVotes': imdb['num_votes'],
+ 'omdbTomatoConsensus': (omdb['tomatoConsensus'] != 'N/A') and omdb['tomatoConsensus'] or None,
+ 'omdbTomato': (omdb['tomatoMeter'] != 'N/A') and float(omdb['tomatoMeter']) or None,
+ 'omdbUserTomato': (omdb['tomatoUserMeter'] != 'N/A') and float(omdb['tomatoUserMeter']) or None,
+ 'omdbTomatoRating': (omdb['tomatoRating'] != 'N/A') and float(omdb['tomatoRating']) or None,
+ 'omdbTomatoUserRating': (omdb['tomatoUserRating'] != 'N/A') and float(omdb['tomatoUserRating']) or None,
+ 'omdbTomatoFresh': (omdb['tomatoFresh'] != 'N/A') and int(omdb['tomatoFresh']) or None,
+ 'tmdbId': tmdb['id'],
+ }
+
+ filename = ".index.html/js/data.js"
+ if not os.path.exists(os.path.dirname(filename)):
+ os.makedirs(os.path.dirname(filename))
+
+ out = open(filename, "w")
+ out.write(callbackName)
+ out.write("(")
+ json.dump(list(filter(bool, map(getInfo, imdb_ids))), out)
+ out.write(")")
+ out.close()
+ return filename
+
try:
from jinja2 import Template
except ImportError:
@@ -296,17 +358,12 @@ def do_index(args, imdb_ids):
template = Template(open(template_file, "r").read())
with TMDBCache() as tmdbcache:
mapping = {
- 'gmtime': time.gmtime(),
- 'input': imdb_ids,
- 'tmdbcache': Protector(tmdbcache),
'title': 'Movie overview',
- 'urlencode': urlencode,
- 'int': int,
- 'listMovieFiles': listMovieFiles,
- 'math': math,
- 'pathjoin': os.path.join,
+ 'install': install,
+ 'data': data,
}
- assert not os.path.exists("index.html"), "index.html already exists"
+ assert not os.path.exists("index.html") and not os.path.exists(".index.html"), \
+ "index.html or folder .index.html already exists"
stream = template.generate(mapping)
outfile = open("index.html", "wb")
for output in stream: