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