]> code.delx.au - webdl/blob - grabber.py
90708e72259fba92c92040b3c66677baa15ec1d3
[webdl] / grabber.py
1 #!/usr/bin/python2
2
3 from common import load_root_node, natural_sort
4 import sys
5
6 def choose(options, allow_multi):
7 reverse_map = {}
8 for i, (key, value) in enumerate(options):
9 print "%3d) %s" % (i+1, key)
10 reverse_map[i+1] = value
11 print " 0) Back"
12 while True:
13 try:
14 values = map(int, raw_input("Choose> ").split())
15 if len(values) == 0:
16 continue
17 if 0 in values:
18 return
19 values = [reverse_map[value] for value in values if value in reverse_map]
20 if allow_multi:
21 return values
22 else:
23 if len(values) == 1:
24 return values[0]
25 except (ValueError, IndexError):
26 print >>sys.stderr, "Invalid input, please try again"
27 pass
28
29 def main():
30 node = load_root_node()
31
32 while True:
33 options = []
34 will_download = True
35 for n in node.get_children():
36 options.append((n.title, n))
37 if not n.can_download:
38 will_download = False
39 options = natural_sort(options, key=lambda x: x[0])
40 result = choose(options, allow_multi=will_download)
41 if result is None:
42 if node.parent is not None:
43 node = node.parent
44 else:
45 break
46 elif will_download:
47 for n in result:
48 if not n.download():
49 raw_input("Press return to continue...\n")
50 else:
51 node = result
52
53 if __name__ == "__main__":
54 try:
55 main()
56 except (KeyboardInterrupt, EOFError):
57 print "\nExiting..."
58