Repository navigation
Expand file tree
/
Copy pathBackupX.jl
More file actions
115 lines (95 loc) · 2.68 KB
/
Copy pathBackupX.jl
File metadata and controls
115 lines (95 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
include("src/args.jl")
include("src/URL.jl")
using JSON
using Printf
using OrderedCollections
struct Pattern
pattern::Printf.Format
vars::Vector{String}
end
const pat_vars = (
"scheme", "username", "password", "host",
"subdomain", "domain", "tld", "port",
"path", "directory", "file", "fileN",
"fileE", "query", "fragment", "word",
"ext", "num", "y", "m", "d",
)
function Pattern(p::String)::Pattern
edit_pat = replace(p, r"[\$\%]" => "%s", isletter => "") |> Printf.Format
found_vars = split(p, !isletter, keepempty = false)
for v in found_vars
(v ∉ pat_vars) && (@error "'$v' not supported in sent patterns ❎" p; exit(0))
end
return Pattern(edit_pat, found_vars)
end
@inline function url_vars(url::URL)
Dict{String, Vector{String}}(
"scheme" => String[url.scheme],
"username" => String[url.username],
"password" => String[url.password],
"host" => String[url.host],
"domain" => String[url.domain],
"tld" => String[url.tld],
"port" => String[url.port],
"path" => String[url.path],
"directory" => String[url.directory],
"file" => String[url.file],
"fileN" => String[url.file_name],
"fileE" => String[url.file_ext],
"query" => String[url.query],
"fragment" => String[url.fragment],
"subdomain" => SubCombination(url.subdomain),
)
end
function GenerateBackupNames(; url::URL, patterns::Vector{Pattern}, opt_data::Dict{String, Vector{String}})::OrderedSet{String}
result = OrderedSet{String}()
for pattern in patterns
try
vals = [opt_data[v] for v in pattern.vars]
for items in Iterators.product(vals...)
push!(result, Printf.format(pattern.pattern, items...))
end
catch err
@error sprint(showerror, err)
exit(0)
end
end
return result
end
function main()
args = ARGUMENTS()
result = Channel{String}(Inf)
patterns = Pattern[]
try
pfile = read(args["p"], String) |> JSON.parse
for key ∈ keys(pfile), val ∈ pfile[key]
push!(patterns, Pattern(val))
end
catch e
@warn sprint(showerror, e) file = args["p"]
exit(0)
end
urls = args["source"] .|> URL
small_args = Dict{String, Vector{String}}(
"word" => args["word"],
"ext" => args["ext"],
"num" => args["num"],
"d" => args["d"],
"m" => args["m"],
"y" => args["y"],
)
Threads.@threads for url in urls
opt_data = merge(url_vars(url), small_args)
res = GenerateBackupNames(url = url, patterns = patterns, opt_data = opt_data)
foreach(i -> put!(result, i), res)
end
close(result)
if !isempty(args["o"])
open(args["o"], "w+") do file
write(file, join(collect(result), "\n"))
end
else
print(join(collect(result), "\n"))
end
end
main()