]>
code.delx.au - webdl/blob - autograbber.py
3 from common
import load_root_node
8 DOWNLOAD_HISTORY_FILES
= [
9 ".downloaded_auto.txt",
10 "downloaded_auto.txt",
13 class DownloadList(object):
15 self
.seen_list
= set()
16 for filename
in DOWNLOAD_HISTORY_FILES
:
17 if os
.path
.isfile(filename
):
20 filename
= DOWNLOAD_HISTORY_FILES
[0]
22 self
.f
= open(filename
, "r")
24 self
.seen_list
.add(line
.decode("utf-8").strip())
27 print >>sys
.stderr
, "Could not open:", filename
, e
28 self
.f
= open(filename
, "a")
30 def has_seen(self
, node
):
31 return node
.title
in self
.seen_list
33 def mark_seen(self
, node
):
34 self
.seen_list
.add(node
.title
)
35 self
.f
.write(node
.title
.encode("utf-8") + "\n")
39 def match(download_list
, node
, pattern
, count
=0):
41 if not download_list
.has_seen(node
):
43 download_list
.mark_seen(node
)
45 print >>sys
.stderr
, "Failed to download!", node
.title
48 if count
>= len(pattern
):
49 print "No match found for pattern:", "/".join(pattern
)
52 for child
in node
.get_children():
53 if fnmatch
.fnmatch(child
.title
, p
):
54 match(download_list
, child
, pattern
, count
+1)
57 def main(destdir
, patternfile
):
59 node
= load_root_node()
60 download_list
= DownloadList()
62 for line
in open(patternfile
):
63 search
= line
.strip().split("/")
64 match(download_list
, node
, search
)
66 if __name__
== "__main__":
68 destdir
= os
.path
.abspath(sys
.argv
[1])
69 patternfile
= os
.path
.abspath(sys
.argv
[2])
71 print >>sys
.stderr
, "Usage: %s destdir patternfile" % sys
.argv
[0]
74 main(destdir
, patternfile
)
75 except (KeyboardInterrupt, EOFError):