]>
code.delx.au - webdl/blob - autograbber.py
3 from common
import load_root_node
10 HISTORY_FILENAME
= ".history.txt"
11 PATTERN_FILENAME
= ".patterns.txt"
13 class DownloadList(object):
15 self
.seen_list
= set()
16 self
._load
_history
_file
()
17 self
.f
= open(HISTORY_FILENAME
, "a")
19 def _load_history_file(self
):
20 self
._move
_old
_file
("downloaded_auto.txt")
21 self
._move
_old
_file
(".downloaded_auto.txt")
24 with
open(HISTORY_FILENAME
, "r") as f
:
26 self
.seen_list
.add(line
.strip())
27 except Exception as e
:
28 logging
.error("Could not open history file: %s -- %s", HISTORY_FILENAME
, e
)
30 def _move_old_file(self
, old_filename
):
31 if os
.path
.isfile(old_filename
) and not os
.path
.isfile(HISTORY_FILENAME
):
32 logging
.info("Migrating download history from %s to %s", old_filename
, HISTORY_FILENAME
)
33 shutil
.move(old_filename
, HISTORY_FILENAME
)
35 def has_seen(self
, node
):
36 return node
.title
.strip() in self
.seen_list
38 def mark_seen(self
, node
):
39 self
.seen_list
.add(node
.title
.strip())
40 self
.f
.write(node
.title
.strip() + "\n")
44 def match(download_list
, node
, pattern
, count
=0):
46 if not download_list
.has_seen(node
):
48 download_list
.mark_seen(node
)
50 logging
.error("Failed to download! %s", node
.title
)
53 if count
>= len(pattern
):
54 logging
.error("No match found for pattern:", "/".join(pattern
))
57 for child
in node
.get_children():
58 if fnmatch
.fnmatch(child
.title
, p
):
59 match(download_list
, child
, pattern
, count
+1)
62 def process_one_dir(destdir
, patternfile
):
64 node
= load_root_node()
65 download_list
= DownloadList()
67 for line
in open(patternfile
):
68 search
= line
.strip().split("/")
69 match(download_list
, node
, search
)
71 def check_directories(download_dirs
):
75 for d
in download_dirs
:
76 d
= os
.path
.abspath(d
)
77 if not os
.path
.isdir(d
):
78 print("Not a directory!", d
)
81 pattern_filename
= os
.path
.join(d
, PATTERN_FILENAME
)
82 if not os
.path
.isfile(pattern_filename
):
83 print("Missing file!", pattern_filename
)
86 result
.append((d
, pattern_filename
))
94 def process_dirs(download_dirs
):
95 for download_dir
, pattern_filename
in check_directories(download_dirs
):
96 logging
.info("Processing directory: %s", download_dir
)
97 process_one_dir(download_dir
, pattern_filename
)
99 if __name__
== "__main__":
100 if len(sys
.argv
) <= 1:
101 print("Usage: %s download_dir [download_dir ...]" % sys
.argv
[0])
104 if len(sys
.argv
) == 3 and os
.path
.isfile(sys
.argv
[2]):
105 # Backwards compatibility with old argument format
106 destdir
= os
.path
.abspath(sys
.argv
[1])
107 patternfile
= os
.path
.abspath(sys
.argv
[2])
108 run
= lambda: process_one_dir(destdir
, patternfile
)
111 run
= lambda: process_dirs(sys
.argv
[1:])
115 except (KeyboardInterrupt, EOFError):
116 print("\nExiting...")