From a4657a5aff417e0a03d6794c59725ddad0875ced Mon Sep 17 00:00:00 2001 From: Yves Fischer Date: Tue, 9 Dec 2014 23:04:51 +0100 Subject: more compact --- imdb-lookup/imdbinfo.py | 20 ++++++------- imdb-lookup/index.jinja2.html | 65 +++++++++++++++++++++++++------------------ 2 files changed, 47 insertions(+), 38 deletions(-) diff --git a/imdb-lookup/imdbinfo.py b/imdb-lookup/imdbinfo.py index eeea947..4290dac 100755 --- a/imdb-lookup/imdbinfo.py +++ b/imdb-lookup/imdbinfo.py @@ -17,7 +17,7 @@ from io import BytesIO from urllib.parse import quote as urlencode try: - from PIL import Image + from PIL import Image, ImageFilter import tmdbsimple as tmdb import requests except ImportError as e: @@ -126,12 +126,11 @@ class TMDBCache(object): return None contentType, data = p image = Image.open(BytesIO(data)) + image = image.filter(ImageFilter.GaussianBlur(radius=1)) buf = BytesIO() - image.save(buf, "JPEG", quality=10, optimize=True) - data64 = base64.encodestring(buf.getvalue()) - data64 = "".join(map(lambda c: isinstance(c, int) and chr(c) or c, - filter(lambda c: c != '\n', data64))) - return "data:{};base64,{}".format(contentType, data64) + 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()) def _imdb_request(self, path, query): # see also https://github.com/richardasaurus/imdb-pie @@ -287,10 +286,6 @@ def do_index(args, imdb_ids): if sum(map(curfile.endswith, valid_extensions)): yield os.path.join(root, curfile) - def asBase64(poster): - contentType, data = poster - data64 = filter(lambda c: c != '\n', base64.encodestring(data)) - return "data:{};base64,{}".format(contentType, data64) try: from jinja2 import Template except ImportError: @@ -309,12 +304,15 @@ def do_index(args, imdb_ids): 'int': int, 'listMovieFiles': listMovieFiles, 'math': math, + 'pathjoin': os.path.join, } assert not os.path.exists("index.html"), "index.html already exists" stream = template.generate(mapping) outfile = open("index.html", "wb") for output in stream: - outfile.write(output.encode('utf-8')) + out = output.strip() + out = re.sub(" {2,}", " ", out) + outfile.write(out.encode('utf-8')) class HelpAction(argparse._HelpAction): diff --git a/imdb-lookup/index.jinja2.html b/imdb-lookup/index.jinja2.html index dfdb9c7..b09abf1 100644 --- a/imdb-lookup/index.jinja2.html +++ b/imdb-lookup/index.jinja2.html @@ -7,9 +7,9 @@ {{title}}