]>
code.delx.au - webdl/blob - autograbber.py
2 # vim:ts=4:sts=4:sw=4:noet
4 from common
import load_root_node
9 DOWNLOAD_HISTORY_FILES
= [
10 ".downloaded_auto.txt",
11 "downloaded_auto.txt",
14 class DownloadList(object):
16 self
.seen_list
= set()
17 for filename
in DOWNLOAD_HISTORY_FILES
:
18 if os
.path
.isfile(filename
):
21 filename
= DOWNLOAD_HISTORY_FILES
[0]
23 self
.f
= open(filename
, "r")
25 self
.seen_list
.add(line
.decode("utf-8").strip())
28 print >>sys
.stderr
, "Could not open:", filename
, e
29 self
.f
= open(filename
, "a")
31 def has_seen(self
, node
):
32 return node
.title
in self
.seen_list
34 def mark_seen(self
, node
):
35 self
.seen_list
.add(node
.title
)
36 self
.f
.write(node
.title
.encode("utf-8") + "\n")
40 def match(download_list
, node
, pattern
, count
=0):
42 if not download_list
.has_seen(node
):
44 download_list
.mark_seen(node
)
46 print >>sys
.stderr
, "Failed to download!", node
.title
49 if count
>= len(pattern
):
50 print "No match found for pattern:", "/".join(pattern
)
53 for child
in node
.get_children():
54 if fnmatch
.fnmatch(child
.title
, p
):
55 match(download_list
, child
, pattern
, count
+1)
58 def main(destdir
, patternfile
):
60 node
= load_root_node()
61 download_list
= DownloadList()
63 for line
in open(patternfile
):
64 search
= line
.strip().split("/")
65 match(download_list
, node
, search
)
67 if __name__
== "__main__":
69 destdir
= os
.path
.abspath(sys
.argv
[1])
70 patternfile
= os
.path
.abspath(sys
.argv
[2])
72 print >>sys
.stderr
, "Usage: %s destdir patternfile" % sys
.argv
[0]
75 main(destdir
, patternfile
)
76 except (KeyboardInterrupt, EOFError):