]> code.delx.au - webdl/blob - autograbber.py
Improved caching
[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 node = load_root_node()
40 download_list = DownloadList("downloaded_auto.txt")
41
42 for search in sys.argv[1:]:
43 search = search.split("/")
44 match(download_list, node, search)
45
46 if __name__ == "__main__":
47 try:
48 main()
49 except (KeyboardInterrupt, EOFError):
50 print "\nExiting..."
51