diff options
author | Yves Fischer <yvesf-git@xapek.org> | 2014-11-22 16:51:53 +0100 |
---|---|---|
committer | Yves Fischer <yvesf-git@xapek.org> | 2014-11-22 16:51:53 +0100 |
commit | 377a4dd4ae8563a6882858dd6c4e1063d039f400 (patch) | |
tree | 452a28243acbba354e176fd802b27238dcd7c322 /imdb-lookup | |
parent | 808bd6e9cb9e07e2ae1d1f116b6b540e36bd64bc (diff) | |
download | scripts-377a4dd4ae8563a6882858dd6c4e1063d039f400.tar.gz scripts-377a4dd4ae8563a6882858dd6c4e1063d039f400.zip |
exception handling and p27/py3 compatible encoding
Diffstat (limited to 'imdb-lookup')
-rwxr-xr-x | imdb-lookup/imdbinfo.py | 30 | ||||
-rw-r--r-- | imdb-lookup/index.jinja2.html | 4 |
2 files changed, 26 insertions, 8 deletions
diff --git a/imdb-lookup/imdbinfo.py b/imdb-lookup/imdbinfo.py index f5efe17..54c4816 100755 --- a/imdb-lookup/imdbinfo.py +++ b/imdb-lookup/imdbinfo.py @@ -85,12 +85,9 @@ class TMDBCache(object): keyContentType = "poster_{}_{}_content_type".format(format, poster_path) url = "http://image.tmdb.org/t/p/{}/{}".format(format, poster_path) if key not in self.db_images: - try: - r = requests.get(url) - self.db_images[key] = r.content - self.db_images[keyContentType] = r.headers['content-type'] - except: - return None + r = requests.get(url) + self.db_images[key] = r.content + 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"): @@ -101,6 +98,22 @@ class TMDBCache(object): filter(lambda c: c!='\n', base64.encodestring(data)))) return "data:{};base64,{}".format(contentType, data64) + +class Protector(object): + def __init__(self, child): + self.child = child + def __getattr__(self, name): + attr = getattr(self.child, name) + def protected(*a, **kw): + try: + return attr(*a,**kw) + except: + return None + if callable(attr): + return protected + else: + return o + def do_aka(args, imdb_ids): with TMDBCache() as tmdbcache: for (filename, imdb_id) in imdb_ids: @@ -156,11 +169,12 @@ def do_index(args, imdb_ids): #posters = list(map(asBase64, map(tmdbcache.poster, map(lambda info: info['poster_path'], infos)))) mapping = { 'input' : imdb_ids, - 'tmdbcache' : tmdbcache, + 'tmdbcache' : Protector(tmdbcache), 'title' : 'Movie overview', } assert not os.path.exists("index.html"), "index.html already exists" - open("index.html", "w").write(template.render(mapping).encode('utf-8')) + output = template.render(mapping).encode("utf-8") + open("index.html", "wb").write(output) class HelpAction(argparse._HelpAction): def __call__(self, parser, namespace, values, option_string=None): diff --git a/imdb-lookup/index.jinja2.html b/imdb-lookup/index.jinja2.html index 7271e91..0dbf973 100644 --- a/imdb-lookup/index.jinja2.html +++ b/imdb-lookup/index.jinja2.html @@ -11,6 +11,7 @@ {% for (filename, imdb_id) in input %} {% set info = tmdbcache.infos(imdb_id) %}</h2> {% set posterBase64 = tmdbcache.poster_base64(info['poster_path']) %} + {% if info %} <div style="clear: left; padding-top: 3em;"> <h2 style="">{{ info.title }}</h2> <div style="width: 185px; float: left;"> @@ -36,6 +37,9 @@ </table> </div> </div> + {% else %} + <span>No infos for {{filename}} {{imdb_id}}</span> + {% endif %} {% endfor %} </body> </html> |