]> code.delx.au - webdl/blobdiff - grabber.py
Natural sorting + lazy loading of iView
[webdl] / grabber.py
index 9fa546fdf35d299e238627b0191df7dcaef0e6b1..165d5ea855eb20a06bd6d28e3503fc6625e281c7 100755 (executable)
@@ -1,14 +1,15 @@
 #!/usr/bin/env python
 # 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,7 +17,7 @@ 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 allow_multi:
                                return values
                        else:
@@ -30,12 +31,14 @@ def main():
        node = load_root_node()
 
        while True:
-               options = {}
+               options = []
                will_download = True
                for n in node.get_children():
-                       options[n.title] = n
+                       options.append((n.title, n))
                        if not n.can_download:
                                will_download = False
+               if node.sort_children:
+                       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: