]>
code.delx.au - webdl/blob - autograbber.py
2 # vim:ts=4:sts=4:sw=4:noet
4 from common
import load_root_node
9 class DownloadList(object):
10 def __init__(self
, filename
):
11 self
.seen_list
= set()
13 self
.f
= open(filename
, "r")
15 self
.seen_list
.add(line
.decode("utf-8").strip())
18 print >>sys
.stderr
, "Could not open:", filename
, e
19 self
.f
= open(filename
, "a")
21 def has_seen(self
, node
):
22 return node
.title
in self
.seen_list
24 def mark_seen(self
, node
):
25 self
.seen_list
.add(node
.title
)
26 self
.f
.write(node
.title
.encode("utf-8") + "\n")
30 def match(download_list
, node
, pattern
, count
=0):
32 if not download_list
.has_seen(node
):
34 download_list
.mark_seen(node
)
36 print >>sys
.stderr
, "Failed to download!", node
.title
39 if count
>= len(pattern
):
40 print "No match found for pattern:", "/".join(pattern
)
43 for child
in node
.get_children():
44 if fnmatch
.fnmatch(child
.title
, p
):
45 match(download_list
, child
, pattern
, count
+1)
48 def main(destdir
, patternfile
):
50 node
= load_root_node()
51 download_list
= DownloadList("downloaded_auto.txt")
53 for line
in open(patternfile
):
54 search
= line
.strip().split("/")
55 match(download_list
, node
, search
)
57 if __name__
== "__main__":
59 destdir
= os
.path
.abspath(sys
.argv
[1])
60 patternfile
= os
.path
.abspath(sys
.argv
[2])
62 print >>sys
.stderr
, "Usage: %s destdir patternfile" % sys
.argv
[0]
65 main(destdir
, patternfile
)
66 except (KeyboardInterrupt, EOFError):