X-Git-Url: https://code.delx.au/webdl/blobdiff_plain/9a03ccf9037c0c2dc2ad28e176c3847fed7a98c6..c79961c5fd06ddf7cc7ab5d7ee1e8fc8a464a8f1:/grabber.py diff --git a/grabber.py b/grabber.py index 2687c1d..cb71e0b 100755 --- a/grabber.py +++ b/grabber.py @@ -1,57 +1,58 @@ -#!/usr/bin/env python -# vim:ts=4:sts=4:sw=4:noet +#!/usr/bin/env python3 + +from common import load_root_node, natural_sort -from common import load_root_node -import sys def choose(options, allow_multi): - skeys = sorted(options.keys()) - for i, key in enumerate(skeys): - print " %d) %s" % (i+1, key) - print " 0) Back" - while True: - try: - values = map(int, raw_input("Choose> ").split()) - if len(values) == 0: - continue - if 0 in values: - return - values = [options[skeys[value-1]] for value in values] - if allow_multi: - return values - else: - if len(values) == 1: - return values[0] - except (ValueError, IndexError): - print >>sys.stderr, "Invalid input, please try again" - pass + reverse_map = {} + for i, (key, value) in enumerate(options): + print("%3d) %s" % (i+1, key)) + reverse_map[i+1] = value + print(" 0) Back") + while True: + try: + values = list(map(int, input("Choose> ").split())) + if len(values) == 0: + continue + if 0 in values: + return + values = [reverse_map[value] for value in values if value in reverse_map] + if allow_multi: + return values + else: + if len(values) == 1: + return values[0] + except (ValueError, IndexError): + print("Invalid input, please try again") + pass def main(): - node = load_root_node() + node = load_root_node() - while True: - options = {} - will_download = True - for n in node.children: - options[n.title] = n - if not n.can_download: - will_download = False - result = choose(options, allow_multi=will_download) - if result is None: - if node.parent is not None: - node = node.parent - else: - break - elif will_download: - for n in result: - if not n.download(): - raw_input("Press return to continue...\n") - else: - node = result + while True: + options = [] + will_download = True + for n in node.get_children(): + options.append((n.title, n)) + if not n.can_download: + will_download = False + options = natural_sort(options, key=lambda x: x[0]) + result = choose(options, allow_multi=will_download) + if result is None: + if node.parent is not None: + node = node.parent + else: + break + elif will_download: + for n in result: + if not n.download(): + input("Press return to continue...\n") + else: + node = result if __name__ == "__main__": - try: - main() - except (KeyboardInterrupt, EOFError): - print "\nExiting..." + try: + main() + except (KeyboardInterrupt, EOFError): + print("\nExiting...")