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