/
fotolog.lua
219 lines (187 loc) · 8.02 KB
/
fotolog.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
dofile("urlcode.lua")
dofile("table_show.lua")
local url_count = 0
local tries = 0
local item_type = os.getenv('item_type')
local item_value = os.getenv('item_value')
local item_dir = os.getenv('item_dir')
local warc_file_base = os.getenv('warc_file_base')
local profilepic = nil
local abortgrab = false
local downloaded = {}
local addedtolist = {}
local profiles = {}
for ignore in io.open("ignore-list", "r"):lines() do
downloaded[ignore] = true
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
wget.callbacks.download_child_p = function(urlpos, parent, depth, start_url_parsed, iri, verdict, reason)
local url = urlpos["url"]["url"]
local html = urlpos["link_expect_html"]
if string.match(url, "^https?://[^/]*fotolog%.com/[^/]+") and not string.match(url, "^https?://sp[0-9a-zA-Z]*%.fotolog%.com") then
profiles[string.match(url, "^https?://[^/]*fotolog%.com/([^/]+)")] = true
end
if (downloaded[url] ~= true and addedtolist[url] ~= true) and (string.match(url, "^https?://[^/]*fotolog%.com") and string.match(url, item_value) or html == 0) and not (string.match(url, "^https?://[^/]*fotolog%.com[^/]*/act/twitter_register") or string.match(url, "^https?://[^/]*fotolog%.com[^/]*/login/%?redirect=") or string.match(url, "^https?://[^/]*fotolog%.com[^/]*/act/lang_switch") or string.match(url, "^https?://[^/]*fotolog%.com[^/]*/act/friend_add") or string.match(url, "^https?://[^/]*fotolog%.com[^/]*/act/share_overlay") or string.match(url, "^https?://[^/]*fotolog%.com[^/]*/act/report_overlay") or string.match(url, "^https?://[^/]*fotolog%.com/account/photos/edit") or string.match(url, " ") or string.match(url, "%%20")) then
if string.match(url, "^https?://sp[0-9a-zA-Z]*%.fotolog%.com") and string.match(url, item_value) then
addedtolist[url] = true
return true
elseif not string.match(url, "^https?://sp[0-9a-zA-Z]*%.fotolog%.com") then
addedtolist[url] = true
return true
else
return false
end
else
return false
end
end
wget.callbacks.get_urls = function(file, url, is_css, iri)
local urls = {}
local html = nil
downloaded[url] = true
local function check(urla)
local url = string.match(urla, "^([^#]+)")
if string.match(url, "^https?://[^/]*fotolog%.com/[^/]+") and not string.match(url, "^https?://sp[0-9a-zA-Z]*%.fotolog%.com") then
profiles[string.match(url, "^https?://[^/]*fotolog%.com/([^/]+)")] = true
end
if (downloaded[url] ~= true and addedtolist[url] ~= true) and string.match(url, "^https?://[^/]*fotolog%.com") and string.match(url, item_value) and not (string.match(url, "^https?://[^/]*fotolog%.com[^/]*/act/twitter_register") or string.match(url, "^https?://[^/]*fotolog%.com[^/]*/login/%?redirect=") or string.match(url, "^https?://[^/]*fotolog%.com[^/]*/act/lang_switch") or string.match(url, "^https?://[^/]*fotolog%.com[^/]*/act/friend_add") or string.match(url, "^https?://[^/]*fotolog%.com[^/]*/act/share_overlay") or string.match(url, "^https?://[^/]*fotolog%.com[^/]*/act/report_overlay") or string.match(url, "^https?://[^/]*fotolog%.com/account/photos/edit") or string.match(url, " ") or string.match(url, "%%20")) then
if string.match(url, "&") then
table.insert(urls, { url=string.gsub(url, "&", "&") })
addedtolist[url] = true
addedtolist[string.gsub(url, "&", "&")] = true
else
table.insert(urls, { url=url })
addedtolist[url] = true
end
end
end
local function checknewurl(newurl)
if string.match(newurl, "^https?://") then
check(newurl)
elseif string.match(newurl, "^//") then
check("http:"..newurl)
elseif string.match(newurl, "^/") then
check(string.match(url, "^(https?://[^/]+)")..newurl)
end
end
local function checknewshorturl(newurl)
if not (string.match(newurl, "^https?://") or string.match(newurl, "^/") or string.match(newurl, "^javascript:") or string.match(newurl, "^mailto:") or string.match(newurl, "^%${")) then
check(string.match(url, "^(https?://.+/)")..newurl)
end
end
if string.match(url, "^https?://sp[0-9a-zA-Z]*%.fotolog%.com") and string.match(url, "_[a-z]%.?[0-9a-zA-Z]*$") then
check(string.gsub(url, "_[a-z](%.?[a-zA-Z]*)$", "_f%1"))
check(string.gsub(url, "_[a-z](%.?[a-zA-Z]*)$", "_m%1"))
check(string.gsub(url, "_[a-z](%.?[a-zA-Z]*)$", "_t%1"))
end
if item_type == 'profile' and string.match(url, "^https?://[^/]*fotolog%.com") and not string.match(url, "^https?://sp[A-Za-z0-9]*%.fotolog%.com") then
html = read_file(file)
if profilepic == nil then
profilepic = string.match(html, '([0-9]+)_t%.?[0-9a-zA-Z]*"%s+alt="Avatar%s+'.. item_value ..'"')
end
for newurl in string.gmatch(html, '([^"]+)') do
checknewurl(newurl)
end
for newurl in string.gmatch(html, "([^']+)") do
checknewurl(newurl)
end
for newurl in string.gmatch(html, ">([^<]+)") 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
if string.match(html, "Sorry,%s+Fotolog%s+is%s+over%s+capacity%.") or string.match(html, "Please%s+try%s+again%s+later,%s+in%s+the%s+meantime%s+why%s+don't%s+you%s+go%s+out%s+and%s+take%s+some%s+pictures%s*%?") or string.match(html, "erreur%-500") or string.match(html, "error_box") then
io.stdout:write("Fotolog is overloaded! ABORTING.\n")
io.stdout:flush()
abortgrab = true
end
end
return urls
end
wget.callbacks.httploop_result = function(url, err, http_stat)
-- NEW for 2014: Slightly more verbose messages because people keep
-- complaining that it's not moving or not working
status_code = http_stat["statcode"]
url_count = url_count + 1
io.stdout:write(url_count .. "=" .. status_code .. " " .. url["url"] .. ". \n")
io.stdout:flush()
if downloaded[url["url"]] == true then
return wget.actions.EXIT
end
if status_code ~= 200 and status_code ~= 404 and url["url"] == "http://www.fotolog.com/"..item_value.."/" then
return wget.actions.ABORT
end
if abortgrab == true then
return wget.actions.ABORT
end
if string.match(url["url"], "%%20https?://[^/]*fotolog%.com[^/]*/") or string.match(url["url"], "%s+https?://[^/]*fotolog%.com[^/]*/") then
return wget.actions.EXIT
end
if (status_code >= 200 and status_code <= 399) then
if string.match(url.url, "https://") then
local newurl = string.gsub(url.url, "https://", "http://")
downloaded[newurl] = true
else
downloaded[url.url] = true
end
end
if status_code >= 500 or
(status_code >= 400 and status_code ~= 404) or
status_code == 0 then
io.stdout:write("Server returned "..http_stat.statcode.." ("..err.."). Sleeping.\n")
io.stdout:flush()
os.execute("sleep 1")
tries = tries + 1
if profilepic ~= nil then
if string.match(url["url"], profilepic) then
return wget.actions.EXIT
end
end
if tries >= 5 then
io.stdout:write("\nI give up...\n")
io.stdout:flush()
tries = 0
if string.match(url["url"], "^https?://[^/]*fotolog%.com") then
return wget.actions.ABORT
else
return wget.actions.EXIT
end
else
return wget.actions.CONTINUE
end
end
tries = 0
local sleep_time = 0
if sleep_time > 0.001 then
os.execute("sleep " .. sleep_time)
end
return wget.actions.NOTHING
end
wget.callbacks.finish = function(start_time, end_time, wall_time, numurls, total_downloaded_bytes, total_download_time)
local usersfile = io.open(item_dir..'/'..warc_file_base..'_data.txt', 'w')
for url, _ in pairs(profiles) do
usersfile:write(url.."\n")
end
usersfile:close()
end
wget.callbacks.before_exit = function(exit_status, exit_status_string)
if abortgrab == true then
io.stdout:write("Fotolog is overloaded! ABORTING.\n")
io.stdout:flush()
return wget.exits.IO_FAIL
end
return exit_status
end