-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathurlx.jl
250 lines (225 loc) Β· 5.26 KB
/
urlx.jl
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
include("src/args.jl")
include("src/URL.jl")
DATA = String[] # used for other other parts of url
PARAMS = String[] # used for --keys to print query parameters
VALS = String[] # used for --values to print query parameters values
KEYPAIRS = String[] # used for --keypairs to print query keypairs
FORMAT_DATA = String[] # used for --format
JSON_DATA = Vector{OrderedDict{String, Any}}() # used for --json
# called when --format passed
function Format(urls::Vector{String}, format::String)
Threads.@threads for u in urls
try
url = URL(u)
push!(
FORMAT_DATA,
replace(
format,
"%sc" => url.scheme,
"%SC" => url._scheme,
"%un" => url.username,
"%pw" => url.password,
"%au" => url._auth,
"%ho" => url.host,
"%HO" => url._host,
"%sd" => url.subdomain,
"%do" => url.domain,
"%tl" => url.tld,
"%po" => url.port,
"%PO" => url._port,
"%pa" => url.path,
"%PA" => url._path,
"%di" => url.directory,
"%fi" => url.file,
"%fn" => url.file_name,
"%fe" => url.file_ext,
"%qu" => url.query,
"%QU" => url._query,
"%fr" => url.fragment,
"%FR" => url._fragment,
"%pr" => join(url.query_params, " "),
"%PR" => join(url.query_params, "\n"),
"%va" => join(url.query_values, " "),
"%VA" => join(url.query_values, "\n"),
),
)
catch
@error "something wrong in processing below url π but I did the rest π\nurl = $(u)"
continue
end
end
end
function PARTS(urls::Vector{String}, switch::String)
Threads.@threads for u in urls
try
global urle = URL(u)
eval(Meta.parse("!isempty(urle.$switch) && push!(DATA, urle.$switch)"))
catch
@error "something wrong in processing below url π but I did the rest π\nurl = $(u)"
continue
end
end
end
# called when --keys passed
function KEYS(urls::Vector{String})
Threads.@threads for u in urls
try
url = URL(u)
append!(PARAMS, url.query_params)
catch
@error "something wrong in processing below url π but I did the rest π\nurl = $(u)"
continue
end
end
end
# called when --values passed
function VALUES(urls::Vector{String})
Threads.@threads for u in urls
try
url = URL(u)
append!(VALS, url.query_values)
catch
@error "something wrong in processing below url π but I did the rest π\nurl = $(u)"
continue
end
end
end
# called when --keypairs passed
function keypairs(urls::Vector{String})
Threads.@threads for u in urls
try
# extract wanted url sections
url = URL(u)
for (key, value) in url.query_paires
println(key, "=", value)
end
catch
@error "something wrong in processing below url π but I did the rest π\nurl = $(u)"
continue
end
end
end
# Count & sort url items
function COUNT(list::Vector{String}, number::Bool)
data = Dict{String, Int32}()
# count url items
for item in list
haskey(data, item) ? (data[item] += 1) : (data[item] = 1)
end
# sort descending url items
for (key, value) in sort(data, byvalue = true, rev = true)
println(number ? "$key: $value" : key) # if number was true show number of items
end
end
function main()
args = ARGUMENTS() # passed user cli args
count::Bool = args["c"]
countNumber::Bool = args["cn"]
if !isempty(args["u"]) # in order not to interfere with the switches -u / -U
urls::Vector{String} = [args["u"]]
elseif !isempty(args["ul"])
try
urls = readlines(args["ul"])
catch err
@error "there is no file: $(args["ul"])"
exit(0)
end
elseif args["stdin"]
urls = unique(readlines(stdin))
end
urls = filter(!isempty, urls) # remove empty lines
switches = filter(item -> args[item],
[
"scheme",
"username",
"password",
"auth",
"host",
"domain",
"subdomain",
"tld",
"port",
"path",
"directory",
"file",
"file_name",
"file_ext",
"query",
"fragment",
],
)
if !isempty(switches)
PARTS(urls, switches[1])
if countNumber
COUNT(DATA, true)
elseif count
COUNT(DATA, false)
else
print(join(DATA, "\n"))
end
end
# manage --keys
if args["keys"]
KEYS(urls)
if countNumber
COUNT(PARAMS, true)
elseif count
COUNT(PARAMS, false)
else
print(join(unique(PARAMS), "\n"))
end
end
# manage --values
if args["values"]
VALUES(urls)
if countNumber
COUNT(VALS, true)
elseif count
COUNT(VALS, false)
else
print(join(unique(VALS), "\n"))
end
end
# manage --keypairs
if args["keypairs"]
keypairs(urls)
if countNumber
COUNT(KEYPAIRS, true)
elseif count
COUNT(KEYPAIRS, false)
else
print(join(unique(KEYPAIRS), "\n"))
end
end
# manage --format
if !isempty(args["format"])
Format(urls, args["format"])
if countNumber
COUNT(FORMAT_DATA, true)
elseif count
COUNT(FORMAT_DATA, false)
else
print(join(FORMAT_DATA, "\n"))
end
end
# manage --json
if args["json"]
for u in urls
try
url = URL(u)
Json(url)
catch
@error "something wrong in processing below url π\nurl = $(u)"
exit(0)
end
end
JSON.print(JSON_DATA, 4)
end
# manage --decode
if args["decode"]
for url in urls
println(url |> URL_Decode |> HTML_Decode)
end
end
end
main()