]> code.delx.au - webdl/blobdiff - grabber.py
brightcove: seems to work well, no need to mark it experimental anymore
[webdl] / grabber.py
index b4b8310ae4827298020e96de013dd3a11d3fe7e1..5c3c948293139e0ba1234243eb4638e63e82b140 100755 (executable)
@@ -1,14 +1,15 @@
-#!/usr/bin/env python
+#!/usr/bin/python2
 # vim:ts=4:sts=4:sw=4:noet
 
-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"
+       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 = map(int, raw_input("Choose> ").split())
@@ -16,28 +17,27 @@ def choose(options, allow_multi):
                                continue
                        if 0 in values:
                                return
-                       values = [options[skeys[value-1]] for value in values]
+                       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:
+               except (ValueError, IndexError):
                        print >>sys.stderr, "Invalid input, please try again"
                        pass
 
 def main():
-       print "Loading episode data...",
-       sys.stdout.flush()
        node = load_root_node()
-       print "done"
+
        while True:
-               options = {}
+               options = []
                will_download = True
-               for n in node.children:
-                       options[n.title] = n
+               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: