summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYves Fischer <yvesf-git@xapek.org>2014-11-20 23:17:32 +0100
committerYves Fischer <yvesf-git@xapek.org>2014-11-20 23:17:32 +0100
commitb493cb9432e5ef7a2baeeab3af155a3929c8914c (patch)
treedf03282878db84734c9e11861cdcfbbf55c247e2
parent9f182d5676b89087b03939b528e9c5607dabd283 (diff)
downloadscripts-b493cb9432e5ef7a2baeeab3af155a3929c8914c.tar.gz
scripts-b493cb9432e5ef7a2baeeab3af155a3929c8914c.zip
better help and python3 compatible
-rwxr-xr-ximdb-lookup/imdbinfo.py77
1 files changed, 53 insertions, 24 deletions
diff --git a/imdb-lookup/imdbinfo.py b/imdb-lookup/imdbinfo.py
index f5d56b2..2835eb8 100755
--- a/imdb-lookup/imdbinfo.py
+++ b/imdb-lookup/imdbinfo.py
@@ -31,7 +31,7 @@ def read_key():
class TMDBCache(object):
def __enter__(self):
- self.db = dbm.open(self._get_db_filename(),"rw")
+ self.db = dbm.open(self._get_db_filename(),"c")
return self
def __exit__(self, type, value, traceback):
@@ -45,9 +45,10 @@ class TMDBCache(object):
return os.path.join(cachedir, "tmdbmovie.dbm")
def _cache(self, key, callable_func):
- if not self.db.has_key(key):
+ if key not in self.db:
self.db[key] = json.dumps(callable_func())
- return json.loads(self.db[key])
+ d = self.db[key].decode('utf-8')
+ return json.loads(d)
def infos(self, movie_id):
try:
@@ -58,8 +59,8 @@ class TMDBCache(object):
def alternative_title(self, movie_id, locale):
"""Returns the title in selected locale or original title otherwise"""
try:
- alt_title = filter(lambda l: l["iso_3166_1"] == locale,
- self._cache(movie_id + "movies.alt_titles", tmdb.Movies(movie_id).alternative_titles)["titles"])
+ alt_title = list(filter(lambda l: l["iso_3166_1"] == locale,
+ self._cache(movie_id + "movies.alt_titles", tmdb.Movies(movie_id).alternative_titles)["titles"]))
if alt_title:
return alt_title[0]["title"]
else:
@@ -72,27 +73,27 @@ class TMDBCache(object):
keys = [movie_id + "movies.info", movie_id + "movies.alt_titles"]
for key in keys:
if key in self.db:
- print "Remove {}".format(key)
+ print("Remove {}".format(key))
del self.db[key]
def do_aka(args, imdb_ids):
with TMDBCache() as tmdbcache:
for imdb_id in imdb_ids:
- print tmdbcache.alternative_title(imdb_id, locale=args.lang)
+ print(tmdbcache.alternative_title(imdb_id, locale=args.lang))
def do_data(args, imdb_ids):
with TMDBCache() as tmdbcache:
for imdb_id in imdb_ids:
selected_properties = ["imdb_id", "revenue", "vote_average", "vote_count", "runtime", "budget", "vote_avarage", "release_date", "popularity", ]
kv = map(lambda kv: u"{}={}".format(*kv),
- filter(lambda (k,v): k in selected_properties,
+ filter(lambda kv: kv[0] in selected_properties,
tmdbcache.infos(imdb_id).items()))
- print u" ".join(kv)
+ print(u" ".join(kv))
def do_year(args, imdb_ids):
with TMDBCache() as tmdbcache:
for imdb_id in imdb_ids:
- print tmdbcache.infos(imdb_id)["release_date"].split("-")[0]
+ print(tmdbcache.infos(imdb_id)["release_date"].split("-")[0])
def do_prune(args, imdb_ids):
with TMDBCache() as tmdbcache:
@@ -102,40 +103,68 @@ def do_prune(args, imdb_ids):
def do_rating(args, imdb_ids):
"""Calculates a rating based on vote_average and vote_count"""
with TMDBCache() as tmdbcache:
- infos = map(lambda imdb_id: tmdbcache.infos(imdb_id), imdb_ids)
- infos = filter(lambda i: "vote_average" in i and "vote_count" in i, infos)
+ infos = list(filter(lambda i: "vote_average" in i and "vote_count" in i,
+ map(lambda imdb_id: tmdbcache.infos(imdb_id), imdb_ids)))
maxvotes = max(map(lambda i: i["vote_count"], infos))
for info in infos:
f = math.sin(math.pi * ( info["vote_average"]/10.0 ) )
d = (float(info["vote_count"]) / maxvotes) - 0.5
info['rating'] = info["vote_average"] + 2 * d * f
- print "{rating:.02f} {imdb_id} {title:30s} avg={vote_average:.1f} count={vote_count:.0f}".format(**info)
+ print("{rating:.02f} {imdb_id} {title:30s} avg={vote_average:.1f} count={vote_count:.0f}".format(**info))
+
+
+
+class HelpAction(argparse._HelpAction):
+
+ def __call__(self, parser, namespace, values, option_string=None):
+ parser.print_help()
+ print("")
+
+ # retrieve subparsers from parser
+ subparsers_actions = [
+ action for action in parser._actions
+ if isinstance(action, argparse._SubParsersAction)]
+ # there will probably only be one subparser_action,
+ # but better save than sorry
+ for subparsers_action in subparsers_actions:
+ # get all subparsers and print help
+ for choice, subparser in subparsers_action.choices.items():
+ formatter = argparse.HelpFormatter(prog=choice)
+ print("subcommand {}:".format(choice))
+ for action_group in subparser._action_groups:
+ formatter.add_arguments(action_group._group_actions)
+ print(formatter.format_help())
+
+ parser.exit()
if __name__ == u"__main__":
tmdb.API_KEY = read_key()
- parser = argparse.ArgumentParser(description="get movie data")
+ parser = argparse.ArgumentParser(description="get movie data", add_help=False)
+ parser.add_argument("--help", action=HelpAction, help="Display full help")
+ parser.add_argument("-h", action=argparse._HelpAction, help="Display short help")
+
subparsers = parser.add_subparsers()
- parser_aka = subparsers.add_parser("aka")
+ parser_aka = subparsers.add_parser("aka", add_help=False)
parser_aka.add_argument("--lang", help="Language code (default 'DE')")
parser_aka.set_defaults(func=do_aka)
- parser_aka.add_argument("files", action="append", nargs="+")
+ parser_aka.add_argument("files", action="append", nargs="+", help="Files containing distinct movie-ids")
- parser_data = subparsers.add_parser("data")
+ parser_data = subparsers.add_parser("data", add_help=False)
parser_data.set_defaults(func=do_data)
- parser_data.add_argument("files", action="append", nargs="+")
+ parser_data.add_argument("files", action="append", nargs="+", help="Files containing distinct movie-ids")
- parser_year = subparsers.add_parser("year")
+ parser_year = subparsers.add_parser("year", add_help=False)
parser_year.set_defaults(func=do_year)
- parser_year.add_argument("files", action="append", nargs="+")
+ parser_year.add_argument("files", action="append", nargs="+", help="Files containing distinct movie-ids")
- parser_prune = subparsers.add_parser("prune")
+ parser_prune = subparsers.add_parser("prune", add_help=False)
parser_prune.set_defaults(func=do_prune)
- parser_prune.add_argument("files", action="append", nargs="+")
+ parser_prune.add_argument("files", action="append", nargs="+", help="Files containing distinct movie-ids")
- parser_rating = subparsers.add_parser("rating")
+ parser_rating = subparsers.add_parser("rating", add_help=False)
parser_rating.set_defaults(func=do_rating)
- parser_rating.add_argument("files", action="append", nargs="+")
+ parser_rating.add_argument("files", action="append", nargs="+", help="Files containing distinct movie-ids")
args = parser.parse_args(sys.argv[1:])