]> code.delx.au - webdl/blob - autograbber.py
1c6ef0f0b44c42783c259f99d9c071bd3dadfbd2
[webdl] / autograbber.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 fnmatch
6 import sys
7
8 class DownloadList(object):
9 def __init__(self, filename):
10 self.f = open(filename, "a+")
11 self.seen_list = set()
12 for line in self.f:
13 self.seen_list.add(line.strip())
14
15 def has_seen(self, node):
16 return node.title in self.seen_list
17
18 def mark_seen(self, node):
19 self.seen_list.add(node.title)
20 self.f.write(node.title + "\n")
21
22
23 def match(download_list, node, pattern):
24 if node.can_download:
25 if not download_list.has_seen(node):
26 if node.download():
27 download_list.mark_seen(node)
28 else:
29 print >>sys.stderr, "Failed to download!", node.title
30 return
31
32 p = pattern[0]
33 for child in node.children:
34 if fnmatch.fnmatch(child.title, p):
35 match(download_list, child, pattern[1:])
36
37
38 def main():
39 print "Loading episode data...",
40 sys.stdout.flush()
41 node = load_root_node()
42 print "done"
43
44 download_list = DownloadList("downloaded_auto.txt")
45
46 for search in sys.argv[1:]:
47 search = search.split("/")
48 match(download_list, node, search)
49
50 if __name__ == "__main__":
51 try:
52 main()
53 except (KeyboardInterrupt, EOFError):
54 print "\nExiting..."
55