]> code.delx.au - webdl/blob - grabber.py
Lazy loading plus support for SBS HTTP videos
[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 node = load_root_node()
31
32 while True:
33 options = {}
34 will_download = True
35 for n in node.get_children():
36 options[n.title] = n
37 if not n.can_download:
38 will_download = False
39 result = choose(options, allow_multi=will_download)
40 if result is None:
41 if node.parent is not None:
42 node = node.parent
43 else:
44 break
45 elif will_download:
46 for n in result:
47 if not n.download():
48 raw_input("Press return to continue...\n")
49 else:
50 node = result
51
52 if __name__ == "__main__":
53 try:
54 main()
55 except (KeyboardInterrupt, EOFError):
56 print "\nExiting..."
57