]> code.delx.au - webdl/blob - grabber.py
iView and SBS webdl stuff gets its own project, now it just needs a good name!
[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
5 import sys
6
7 def choose(options, allow_multi):
8 skeys = sorted(options.keys())
9 for i, key in enumerate(skeys):
10 print " %d) %s" % (i+1, key)
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 = [options[skeys[value-1]] for value in values]
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 print "Loading episode data...",
31 sys.stdout.flush()
32 node = load_root_node()
33 print "done"
34 while True:
35 options = {}
36 will_download = True
37 for n in node.children:
38 options[n.title] = n
39 if not n.can_download:
40 will_download = False
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