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