-
Notifications
You must be signed in to change notification settings - Fork 1
/
grabber.py
executable file
·65 lines (58 loc) · 1.88 KB
/
grabber.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python3
from common import load_root_node, natural_sort
def choose(options, allow_multi):
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:
str_values = input("Choose> ").split()
if len(str_values) == 0:
continue
if "0" in str_values:
return
values = []
for s in str_values:
if s.isdigit():
values.append(int(s))
else:
low, high = s.split("-", 1)
values.extend(range(int(low), int(high) + 1))
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):
print("Invalid input, please try again")
pass
def main():
node = load_root_node()
while True:
options = []
will_download = True
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:
node = node.parent
else:
break
elif will_download:
for n in result:
if not n.download():
input("Press return to continue...\n")
else:
node = result
if __name__ == "__main__":
try:
main()
except (KeyboardInterrupt, EOFError):
print("\nExiting...")