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