diff options
author | Thomas Keck <thomas@thinkpad> | 2014-11-15 21:39:50 +0100 |
---|---|---|
committer | Thomas Keck <thomas@thinkpad> | 2014-11-15 21:39:50 +0100 |
commit | 7de2d0f788cd13e77f5855286c96982f49a22690 (patch) | |
tree | 435e76e177f3c69e3a63fa381e3f982ddf4be530 /imdb-lookup | |
parent | 3c6abfda4134079b52e41b8cc4bf20b15a6f6c2f (diff) | |
download | scripts-7de2d0f788cd13e77f5855286c96982f49a22690.tar.gz scripts-7de2d0f788cd13e77f5855286c96982f49a22690.zip |
Imdb rating script
Diffstat (limited to 'imdb-lookup')
-rwxr-xr-x | imdb-lookup/imdbrating.py | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/imdb-lookup/imdbrating.py b/imdb-lookup/imdbrating.py new file mode 100755 index 0000000..995324c --- /dev/null +++ b/imdb-lookup/imdbrating.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python2 +# coding: utf-8 +# python3 works as well +import os +import sys +import shutil +import dbm + +try: + import tmdbsimple as tmdb +except ImportError as e: + print(u"Missing dependency: {0}".format(str(e))) + print(u"Install using system package manager or `pip install --user <module>`") + sys.exit(1) + +def read_key(): + if u"TMDB_KEY" in os.environ.keys(): + return os.environ[u"TMDB_KEY"] + if u"XDG_CONFIG_HOME" in os.environ.keys(): + cfg_home = os.environ[u"XDG_CONFIG_HOME"] + else: + cfg_home = os.path.join(os.path.expanduser(u"~"), ".config") + if os.path.exists(os.path.join(cfg_home, u"tmdbkey")): + return open(os.path.join(cfg_home, u"tmdbkey"), "r").read().strip() + if os.path.exists(os.path.join(os.path.expanduser(u"~"), ".tmdbkey")): + return open(os.path.join(os.path.expanduser(u"~"), ".tmdbkey")).read().strip() + raise Exception(u"No TheMovieDB Key defined. Set Env. var. TMDB_KEY or .tmdbkey file") + + +def get_rating(imdb_id): + return tmdb.Find(id=imdb_id).info(external_source="imdb_id")['movie_results'][0]['vote_average'] + +if __name__ == u"__main__": + tmdb.API_KEY = read_key() + imdb_id = sys.argv[1] + + db = dbm.open('cache', 'rw') + if imdb_id not in db.keys(): + db[imdb_id] = str(get_rating(imdb_id)) + print db[imdb_id] + db.close() |