]> code.delx.au - webdl/blob - grabber.py
Fixed missing import
[webdl] / grabber.py
1 #!/usr/bin/env python3
2
3 from common import load_root_node, natural_sort
4
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 str_values = input("Choose> ").split()
15 if len(str_values) == 0:
16 continue
17 if "0" in str_values:
18 return
19 values = []
20 for s in str_values:
21 if s.isdigit():
22 values.append(int(s))
23 else:
24 low, high = s.split("-", 1)
25 values.extend(range(int(low), int(high) + 1))
26 values = [reverse_map[value] for value in values if value in reverse_map]
27 if allow_multi:
28 return values
29 else:
30 if len(values) == 1:
31 return values[0]
32 except (ValueError, IndexError):
33 print("Invalid input, please try again")
34 pass
35
36 def main():
37 node = load_root_node()
38
39 while True:
40 options = []
41 will_download = True
42 for n in node.get_children():
43 options.append((n.title, n))
44 if not n.can_download:
45 will_download = False
46 options = natural_sort(options, key=lambda x: x[0])
47 result = choose(options, allow_multi=will_download)
48 if result is None:
49 if node.parent is not None:
50 node = node.parent
51 else:
52 break
53 elif will_download:
54 for n in result:
55 if not n.download():
56 input("Press return to continue...\n")
57 else:
58 node = result
59
60 if __name__ == "__main__":
61 try:
62 main()
63 except (KeyboardInterrupt, EOFError):
64 print("\nExiting...")
65