-
Notifications
You must be signed in to change notification settings - Fork 1
/
curiouscat.lua
513 lines (431 loc) · 15.8 KB
/
curiouscat.lua
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
dofile("table_show.lua")
dofile("urlcode.lua")
dofile("strict.lua")
local urlparse = require("socket.url")
local luasocket = require("socket") -- Used to get sub-second time
local http = require("socket.http")
JSON = assert(loadfile "JSON.lua")()
local start_urls = JSON:decode(os.getenv("start_urls"))
local items_table = JSON:decode(os.getenv("item_names_table"))
local item_dir = os.getenv("item_dir")
local warc_file_base = os.getenv("warc_file_base")
local url_count = 0
local tries = 0
local downloaded = {}
local addedtolist = {}
local abortgrab = false
discovered_items = {}
local last_main_site_time = 0
local current_item_type = nil
local current_item_value = nil
local next_start_url_index = 1
io.stdout:setvbuf("no") -- So prints are not buffered - http://lua.2524044.n2.nabble.com/print-stdout-and-flush-td6406981.html
if urlparse == nil or http == nil then
io.stdout:write("socket not corrently installed.\n")
io.stdout:flush()
abortgrab = true
end
do_debug = true
print_debug = function(a)
if do_debug then
print(a)
end
end
print_debug("This grab script is running in debug mode. You should not see this in production.")
local start_urls_inverted = {}
for _, v in pairs(start_urls) do
start_urls_inverted[v] = true
end
-- Function to be called whenever an item's download ends.
end_of_item = function()
-- Empty right now
end
set_new_item = function(url)
if url == start_urls[next_start_url_index] then
end_of_item()
current_item_type = items_table[next_start_url_index][1]
current_item_value = items_table[next_start_url_index][2]
next_start_url_index = next_start_url_index + 1
print_debug("Setting CIT to " .. current_item_type)
print_debug("Setting CIV to " .. current_item_value)
end
assert(current_item_type)
assert(current_item_value)
end
discover_item = function(item_type, item_name)
assert(item_type)
assert(item_name)
if not discovered_items[item_type .. ":" .. item_name] then
print_debug("Queuing for discovery " .. item_type .. ":" .. item_name)
end
discovered_items[item_type .. ":" .. item_name] = true
end
add_ignore = function(url)
if url == nil then -- For recursion
return
end
if downloaded[url] ~= true then
downloaded[url] = true
else
return
end
add_ignore(string.gsub(url, "^https", "http", 1))
add_ignore(string.gsub(url, "^http:", "https:", 1))
add_ignore(string.match(url, "^ +([^ ]+)"))
local protocol_and_domain_and_port = string.match(url, "^([a-zA-Z0-9]+://[^/]+)$")
if protocol_and_domain_and_port then
add_ignore(protocol_and_domain_and_port .. "/")
end
add_ignore(string.match(url, "^(.+)/$"))
end
for ignore in io.open("ignore-list", "r"):lines() do
add_ignore(ignore)
end
read_file = function(file)
if file then
local f = assert(io.open(file))
local data = f:read("*all")
f:close()
return data
else
return ""
end
end
allowed = function(url, parenturl)
assert(parenturl ~= nil)
if start_urls_inverted[url] then
return false
end
local tested = {}
for s in string.gmatch(url, "([^/]+)") do
if tested[s] == nil then
tested[s] = 0
end
if tested[s] == 6 then
return false
end
tested[s] = tested[s] + 1
end
if string.match(url, "^https?://curiouscat%.qa/api/") -- Do not allow the initial pages, instead only let them in through the wget args
or string.match(url, "^https?://curiouscat%.qa/[^/]+/post/[0-9]+$")
or string.match(url, "^https?://m%.curiouscat%.qa/")
or string.match(url, "^https://media%.tenor%.com/images/") then
print_debug("allowing " .. url .. " from " .. parenturl)
return true
end
return false
--return false
--assert(false, "This segment should not be reachable")
end
wget.callbacks.lookup_host = function(host)
if host == "curiouscat.qa" then
return "172.67.75.111"
elseif host == "m.curiouscat.qa" then
return "104.26.8.190"
end
return nil
end
wget.callbacks.download_child_p = function(urlpos, parent, depth, start_url_parsed, iri, verdict, reason)
local url = urlpos["url"]["url"]
--print_debug("DCP on " .. url)
if downloaded[url] == true or addedtolist[url] == true then
return false
end
if allowed(url, parent["url"]) then
addedtolist[url] = true
--set_derived_url(url)
return true
end
return false
end
wget.callbacks.get_urls = function(file, url, is_css, iri)
local urls = {}
local html = nil
downloaded[url] = true
local function check(urla, force)
assert(not force or force == true) -- Don't accidentally put something else for force
local origurl = url
local url = string.match(urla, "^([^#]+)")
local url_ = string.match(url, "^(.-)%.?$")
url_ = string.gsub(url_, "&", "&")
url_ = string.match(url_, "^(.-)%s*$")
url_ = string.match(url_, "^(.-)%??$")
url_ = string.match(url_, "^(.-)&?$")
-- url_ = string.match(url_, "^(.-)/?$") # Breaks dl.
if (downloaded[url_] ~= true and addedtolist[url_] ~= true)
and (allowed(url_, origurl) or force) then
table.insert(urls, { url=url_, headers={["Accept-Language"]="en-US,en;q=0.5"}})
--set_derived_url(url_)
addedtolist[url_] = true
addedtolist[url] = true
end
end
local function checknewurl(newurl)
-- Being caused to fail by a recursive call on "../"
if not newurl then
return
end
if string.match(newurl, "\\[uU]002[fF]") then
return checknewurl(string.gsub(newurl, "\\[uU]002[fF]", "/"))
end
if string.match(newurl, "^https?:////") then
check((string.gsub(newurl, ":////", "://")))
elseif string.match(newurl, "^https?://") then
check(newurl)
elseif string.match(newurl, "^https?:\\/\\?/") then
check((string.gsub(newurl, "\\", "")))
elseif string.match(newurl, "^\\/") then
checknewurl(string.gsub(newurl, "\\", ""))
elseif string.match(newurl, "^//") then
check(urlparse.absolute(url, newurl))
elseif string.match(newurl, "^/") then
check(urlparse.absolute(url, newurl))
elseif string.match(newurl, "^%.%./") then
if string.match(url, "^https?://[^/]+/[^/]+/") then
check(urlparse.absolute(url, newurl))
else
checknewurl(string.match(newurl, "^%.%.(/.+)$"))
end
elseif string.match(newurl, "^%./") then
check(urlparse.absolute(url, newurl))
end
end
local function checknewshorturl(newurl)
if string.match(newurl, "^%?") then
check(urlparse.absolute(url, newurl))
elseif not (string.match(newurl, "^https?:\\?/\\?//?/?")
or string.match(newurl, "^[/\\]")
or string.match(newurl, "^%./")
or string.match(newurl, "^[jJ]ava[sS]cript:")
or string.match(newurl, "^[mM]ail[tT]o:")
or string.match(newurl, "^vine:")
or string.match(newurl, "^android%-app:")
or string.match(newurl, "^ios%-app:")
or string.match(newurl, "^%${")) then
check(urlparse.absolute(url, "/" .. newurl))
end
end
local function load_html()
if html == nil then
html = read_file(file)
end
return html
end
if current_item_type == "user" then
-- Starting point
if string.match(url, "https?://curiouscat%.qa/[^/]+$") and status_code == 200 then
assert(string.match(load_html(), "<title>CuriousCat</title><link")) -- To make sure it's still up
check("https://curiouscat.qa/api/v2.1/profile?username=" .. current_item_value .. "&_ob=registerOrSignin2")
check("https://curiouscat.qa/api/v2/ad/check?path=/" .. current_item_value .. "&_ob=registerOrSignin2")
end
if string.match(url, "^https?://curiouscat%.qa/api/v2%.1/profile%?") and status_code == 200 then
print_debug("API on " .. url)
local json = JSON:decode(load_html())
if json["error"] == 404 then
print_debug("Profile req indicates user does not exist")
else
local lowest_ts = 100000000000000
for _, post in pairs(json["posts"]) do
local content_block = nil
local time = nil
if post["type"] == "post" then
content_block = post["post"]
time = post["post"]["timestamp"]
elseif post["type"] == "status" then
content_block = post["status"]
time = post["status"]["timestamp"]
elseif post["type"] == "shared_post" then
discover_item("user", string.lower(post["post"]["addresseeData"]["username"]))
content_block = post["post"]
time = post["shared_timestamp"]
else
error("Unknown post type " .. post["type"])
end
if content_block then
check((content_block["addresseeData"] or content_block["author"])["avatar"])
check((content_block["addresseeData"] or content_block["author"])["banner"])
if content_block["media"] then
assert(allowed(content_block["media"]["img"], url), content_block["media"]["img"]) -- Don't just want to silently discard this on a failed assumption
check(content_block["media"]["img"])
end
assert(content_block["likes"])
if content_block["likes"] > 0 then
discover_item("postlikes", content_block["id"])
end
-- Remove this block if the project looks uncertain
if post["type"] ~= "shared_post" then
check("https://curiouscat.qa/" .. current_item_value .. "/post/" .. tostring(content_block["id"]))
check("https://curiouscat.qa/api/v2.1/profile/single_post?username=" .. current_item_value .. "&post_id=" .. tostring(content_block["id"]) .. "&_ob=registerOrSignin2")
check("https://curiouscat.qa/api/v2/ad/check?path=/" .. current_item_value .. "/post/" .. tostring(content_block["id"]) .. "&_ob=registerOrSignin2")
end
end
if time and time < lowest_ts then
lowest_ts = time
end
end
if lowest_ts == 100000000000000 then
assert(not string.match(url, "&max_timestamp=")) -- Something is wrong if we get an empty on a page other than the first
else
check("https://curiouscat.qa/api/v2.1/profile?username=" .. current_item_value .. "&max_timestamp=" .. tostring(lowest_ts) .. "&_ob=registerOrSignin2") -- Following Jodizzle's scheme, this just uses the queued URLs as a set, and "detects" the last page by the fact that the lowest is it itself
end
end
end
end
if current_item_type == "postlikes" then
if string.match(url, "^https?://curiouscat%.qa/api/v2/post/likes") and status_code == 200 then
json = JSON:decode(load_html())
if json["error"] ~= "No likes" then
for _, obj in pairs(json["users"]) do
discover_item("user", string.lower(obj["username"]))
end
end
end
end
if status_code == 200 and not (string.match(url, "%.jpe?g$") or string.match(url, "%.png$")) then
-- Completely disabled because I can't be bothered
--[[load_html()
for newurl in string.gmatch(string.gsub(html, """, '"'), '([^"]+)') do
checknewurl(newurl)
end
for newurl in string.gmatch(string.gsub(html, "'", "'"), "([^']+)") do
checknewurl(newurl)
end
for newurl in string.gmatch(html, ">%s*([^<%s]+)") do
checknewurl(newurl)
end
for newurl in string.gmatch(html, "[^%-]href='([^']+)'") do
checknewshorturl(newurl)
end
for newurl in string.gmatch(html, '[^%-]href="([^"]+)"') do
checknewshorturl(newurl)
end
for newurl in string.gmatch(html, ":%s*url%(([^%)]+)%)") do
checknewurl(newurl)
end]]
end
return urls
end
wget.callbacks.httploop_result = function(url, err, http_stat)
status_code = http_stat["statcode"]
url_count = url_count + 1
io.stdout:write(url_count .. "=" .. status_code .. " " .. url["url"] .. " \n")
io.stdout:flush()
if status_code >= 200 and status_code <= 399 then
downloaded[url["url"]] = true
end
assert(not (string.match(url["url"], "^https?://[^/]*google%.com/sorry") or string.match(url["url"], "^https?://consent%.google%.com/")))
if abortgrab == true then
io.stdout:write("ABORTING...\n")
io.stdout:flush()
return wget.actions.ABORT
end
--[[
-- Handle redirects not in download chains
if status_code >= 300 and status_code <= 399 then
local newloc = urlparse.absolute(url["url"], http_stat["newloc"])
print_debug("newloc is " .. newloc)
if downloaded[newloc] == true or addedtolist[newloc] == true then
tries = 0
print_debug("Already encountered newloc " .. newloc)
tries = 0
return wget.actions.EXIT
elseif not allowed(newloc, url["url"]) then
print_debug("Disallowed URL " .. newloc)
-- Continue on to the retry cycle
else
tries = 0
print_debug("Following redirect to " .. newloc)
assert(not (string.match(newloc, "^https?://[^/]*google%.com/sorry") or string.match(newloc, "^https?://consent%.google%.com/")))
assert(not string.match(url["url"], "^https?://drive%.google%.com/file/d/.*/view$")) -- If this is a redirect, it will mess up initialization of file: items
assert(not string.match(url["url"], "^https?://drive%.google%.com/drive/folders/[0-9A-Za-z_%-]+/?$")) -- Likewise for folder:
addedtolist[newloc] = true
return wget.actions.NOTHING
end
end]]
local do_retry = false
local maxtries = 12
local url_is_essential = true
-- Whitelist instead of blacklist status codes
if status_code ~= 200 then
print("Server returned " .. http_stat.statcode .. " (" .. err .. "). Sleeping.\n")
do_retry = true
end
-- Check for rate limiting in the API (status code == 200)
if string.match(url["url"], "^https?://curiouscat%.qa/api/") then
if string.match(read_file(http_stat["local_file"]), "^{'error': 'Wait") then
print("API rate-limited, sleeping")
do_retry = true
end
end
if do_retry then
if tries >= maxtries then
print("I give up...\n")
tries = 0
if not url_is_essential then
return wget.actions.EXIT
else
print("Failed on an essential URL, aborting...")
return wget.actions.ABORT
end
else
sleep_time = math.floor(math.pow(2, tries))
tries = tries + 1
end
end
if do_retry and sleep_time > 0.001 then
print("Sleeping " .. sleep_time .. "s").
os.execute("sleep " .. sleep_time)
return wget.actions.CONTINUE
end
tries = 0
return wget.actions.NOTHING
end
queue_list_to = function(list, key)
if do_debug then
for item, _ in pairs(list) do
print("Would have sent discovered item " .. item)
end
else
local to_send = nil
for item, _ in pairs(list) do
assert(string.match(item, ":")) -- Message from EggplantN, #binnedtray (search "colon"?)
if to_send == nil then
to_send = item
else
to_send = to_send .. "\0" .. item
end
print("Queued " .. item)
end
if to_send ~= nil then
local tries = 0
while tries < 10 do
local body, code, headers, status = http.request(
"http://blackbird-amqp.meo.ws:23038/" .. key .. "/",
to_send
)
if code == 200 or code == 409 then
break
end
os.execute("sleep " .. math.floor(math.pow(2, tries)))
tries = tries + 1
end
if tries == 10 then
abortgrab = true
end
end
end
end
wget.callbacks.finish = function(start_time, end_time, wall_time, numurls, total_downloaded_bytes, total_download_time)
end_of_item()
queue_list_to(discovered_items, "fill-me-in")
end
wget.callbacks.write_to_warc = function(url, http_stat)
set_new_item(url["url"])
return true
end
wget.callbacks.before_exit = function(exit_status, exit_status_string)
if abortgrab == true then
return wget.exits.IO_FAIL
end
return exit_status
end