X-Git-Url: https://code.delx.au/webdl/blobdiff_plain/6de079c2ee77a4dcf990155be6634fa91e35286c..a4fb45b6dfae5fcc969ab351a5aac1fcff5e1264:/grabber.py diff --git a/grabber.py b/grabber.py index 9fa546f..61cfcec 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/python2 -from common import load_root_node +from common import load_root_node, natural_sort 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.encode('utf-8')) + reverse_map[i+1] = value + print " 0) Back" + while True: + try: + values = map(int, raw_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 >>sys.stderr, "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.get_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(): + raw_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..."