]>
code.delx.au - webdl/blob - autograbber.py
3 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
.strip())
27 except Exception as e
:
28 logging
.error("Could not open: %s -- %s", 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
+ "\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 logging
.error("Failed to download! %s", node
.title
)
49 if count
>= len(pattern
):
50 logging
.error("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("Usage: %s destdir patternfile" % sys
.argv
[0])
75 main(destdir
, patternfile
)
76 except (KeyboardInterrupt, EOFError):