diff options
Diffstat (limited to 'imdb-lookup/imdbinfo.py')
-rwxr-xr-x | imdb-lookup/imdbinfo.py | 24 |
1 files changed, 14 insertions, 10 deletions
diff --git a/imdb-lookup/imdbinfo.py b/imdb-lookup/imdbinfo.py index 504528a..163cec9 100755 --- a/imdb-lookup/imdbinfo.py +++ b/imdb-lookup/imdbinfo.py @@ -13,7 +13,7 @@ import argparse import math import logging import hashlib -from io import StringIO +from io import BytesIO from urllib.parse import quote as urlencode @@ -86,6 +86,7 @@ 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): + self.logger.debug("prune {}".format(movie_id)) keys = [ "imdb_maindetails_{}".format(movie_id), movie_id + "movies.info", @@ -93,8 +94,10 @@ class TMDBCache(object): ] for key in keys: if key in self.db: - print("Remove {}".format(key)) + self.logger.warn("Remove {}".format(key)) del self.db[key] + else: + self.logger.debug("Key not in db {}".format(key)) def poster(self, poster_path, format="w185"): self.logger.debug("poster %s", poster_path) @@ -111,8 +114,8 @@ class TMDBCache(object): p = self.poster(poster_path, format) if not p: return None contentType, data = p - image = Image.open(StringIO(data)) - buf = StringIO() + image = Image.open(BytesIO(data)) + buf = BytesIO() image.save(buf, "JPEG", quality=10, optimize=True) data64 = "".join(map(lambda c: isinstance(c,int) and chr(c) or c, filter(lambda c: c!='\n', base64.encodestring(buf.getvalue())))) @@ -137,13 +140,15 @@ class TMDBCache(object): } q = query.copy() q.update(PARAMS) - return requests.get("http://{}{}".format(BASE_URI, path), params=q, headers=HEADERS) + return requests.get("https://{}{}".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) + data = json.loads(r.text) + assert data != None and data['data'], "Data must not be empty" + return data key = "imdb_maindetails_{}".format(movie_id) return self._cache(key, do_request) @@ -242,7 +247,7 @@ def do_index(args, imdb_ids): print("Failed to import jinja2 library for html-templating") sys.exit(1) template_file = os.path.join(os.path.dirname(__file__), "index.jinja2.html") - template = Template(open(template_file, "rb").read().decode('utf-8')) + template = Template(open(template_file, "r").read()) with TMDBCache() as tmdbcache: mapping = { 'gmtime' : time.gmtime(), @@ -258,8 +263,7 @@ def do_index(args, imdb_ids): stream = template.generate(mapping) outfile = open("index.html", "wb") for output in stream: - output = output.encode("utf-8") - outfile.write(output.strip()) + outfile.write(output.encode('utf-8')) class HelpAction(argparse._HelpAction): def __call__(self, parser, namespace, values, option_string=None): @@ -340,6 +344,6 @@ if __name__ == "__main__": if "files" in args: ids = map(lambda filename: (lambda x: (filename, x.groups()[0]) if x else None)(re.match(".*#(tt[0-9]{7}).*", filename)), args.files[0]) - args.func(args, filter(lambda i: i is not None, ids)) + args.func(args, list(filter(lambda i: i is not None, ids))) else: args.func(args) |