]>
code.delx.au - webdl/blob - autograbber.py
3 from common
import load_root_node
10 HISTORY_FILENAME
= ".history.txt"
11 PATTERN_FILENAME
= ".patterns.txt"
12 EXCLUDE_FILENAME
= ".excludes.txt"
14 class DownloadList(object):
16 self
.exclude_list
= set()
17 self
._load
_exclude
_list
()
19 self
.seen_list
= set()
20 self
._load
_history
_file
()
22 self
.f
= open(HISTORY_FILENAME
, "a")
24 def _load_exclude_list(self
):
26 with
open(EXCLUDE_FILENAME
, "r") as f
:
28 self
.exclude_list
.add(line
.strip())
29 except Exception as e
:
32 def _load_history_file(self
):
33 self
._move
_old
_file
("downloaded_auto.txt")
34 self
._move
_old
_file
(".downloaded_auto.txt")
37 with
open(HISTORY_FILENAME
, "r") as f
:
39 self
.seen_list
.add(line
.strip())
40 except Exception as e
:
41 logging
.error("Could not open history file: %s -- %s", HISTORY_FILENAME
, e
)
43 def _move_old_file(self
, old_filename
):
44 if os
.path
.isfile(old_filename
) and not os
.path
.isfile(HISTORY_FILENAME
):
45 logging
.info("Migrating download history from %s to %s", old_filename
, HISTORY_FILENAME
)
46 shutil
.move(old_filename
, HISTORY_FILENAME
)
48 def wants(self
, node
):
49 title
= node
.title
.strip()
50 if title
in self
.seen_list
:
52 for exclude
in self
.exclude_list
:
53 if fnmatch
.fnmatch(title
, exclude
):
57 def mark_seen(self
, node
):
58 self
.seen_list
.add(node
.title
.strip())
59 self
.f
.write(node
.title
.strip() + "\n")
63 def match(download_list
, node
, pattern
, count
=0):
65 if download_list
.wants(node
):
67 download_list
.mark_seen(node
)
69 logging
.error("Failed to download! %s", node
.title
)
72 if count
>= len(pattern
):
73 logging
.error("No match found for pattern:", "/".join(pattern
))
76 for child
in node
.get_children():
77 if fnmatch
.fnmatch(child
.title
, p
):
78 match(download_list
, child
, pattern
, count
+1)
81 def process_one_dir(destdir
, patternfile
):
83 node
= load_root_node()
84 download_list
= DownloadList()
86 for line
in open(patternfile
):
87 search
= line
.strip().split("/")
88 match(download_list
, node
, search
)
90 def check_directories(download_dirs
):
94 for d
in download_dirs
:
95 d
= os
.path
.abspath(d
)
96 if not os
.path
.isdir(d
):
97 print("Not a directory!", d
)
100 pattern_filename
= os
.path
.join(d
, PATTERN_FILENAME
)
101 if not os
.path
.isfile(pattern_filename
):
102 print("Missing file!", pattern_filename
)
105 result
.append((d
, pattern_filename
))
113 def process_dirs(download_dirs
):
114 for download_dir
, pattern_filename
in check_directories(download_dirs
):
115 logging
.info("Processing directory: %s", download_dir
)
116 process_one_dir(download_dir
, pattern_filename
)
118 if __name__
== "__main__":
119 if len(sys
.argv
) <= 1:
120 print("Usage: %s download_dir [download_dir ...]" % sys
.argv
[0])
123 if len(sys
.argv
) == 3 and os
.path
.isfile(sys
.argv
[2]):
124 # Backwards compatibility with old argument format
125 destdir
= os
.path
.abspath(sys
.argv
[1])
126 patternfile
= os
.path
.abspath(sys
.argv
[2])
127 run
= lambda: process_one_dir(destdir
, patternfile
)
130 run
= lambda: process_dirs(sys
.argv
[1:])
134 except (KeyboardInterrupt, EOFError):
135 print("\nExiting...")