罗宁的博客

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
--
-- Created by IntelliJ IDEA.
-- User: root
-- Date: 14-1-8
-- Time: 下午7:27
-- To change this template use File | Settings | File Templates.
--
local mmdd5 = require("md5")
local base64 = require("mime")
local bit = require("bit")
local unicode = require("unicode")
local utf8 = unicode.utf8
local http = require("socket.http")
local ltn12 = require("ltn12")
local cjson = require("cjson")
local cjson2 = cjson.new()
local string = require("string")
--------------------------------------
---- error status
--------------------------------------
local errorStatus = {
[0x01] = "get page failed",
[0x02] = "parse page failed",
[0x03] = "unknown error",
[0x04] = "url is not right",
[0x05] = "timeout",
[0x06] = "video is not exists",
[0x07] = "urls is empty",
}
--------------------------------------
-- user-agent
--------------------------------------
XP_USERAGENT = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"
PHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16"
IPAD_USERAGENT = "Mozilla/5.0 (iPad; CPU OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B176 Safari/7534.48.3"
FIREFOX_USERAGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:16.0) Gecko/20100101 Firefox/16.0"
--------------------------------------
---- 解密字符串
--------------------------------------
function encypt(str, key)
local str = base64.unb64(str)
local flag = string.sub(key, 0, 2)
local result = flag
if flag == "ea" then
result = str
elseif flag == "eb" then
result = bbSecond(str, ccFirst(key, key));
elseif flag == "ed" then
result = bbSecond(str, mmdd5.sumhexa(key));
else
result = str
end
return result
end
function ccFirst(h, l)
local k = {}
local e = 0
local g = ""
local hh = {}
local ll = {}
for i = 1, #h do
hh[i] = string.sub(h, i, i)
end
for i = 1, #l do
ll[i] = string.sub(h, i, i)
end
for f = 1, 256 do
k[f] = f;
end
local d
for f = 1, 256 do
e = (e + k[f] + utf8.byte(h[(f - 1) % #h + 1])) % 256
d = k[f]
k[f] = k[e + 1]
k[e + 1] = d
end
local f = 0
local e = 0
for m = 1, #l do
f = (f + 1) % 256
e = (e + k[f + 1]) % 256
d = k[f + 1]
k[f + 1] = k[e + 1]
k[e + 1] = d
g = g .. utf8.char(bit.bxor(utf8.byte(ll[m]), k[(k[f + 1] + k[e + 1]) % 256 + 1]))
end
return g;
end
function bbSecond(string64, key)
local length = #string64
local k = #key
local strTable = {}
local keyTable = {}
local result = ""
local j
local m
for i = 1, length do
strTable[i] = string.sub(string64, i, i)
end
for i = 1, k do
keyTable[i] = string.sub(key, i, i)
end
for i, v in pairs(strTable) do
m = keyTable[((i - 1) % k + 1)]
j = bit.bxor(utf8.byte(v), utf8.byte(m))
result = result .. utf8.char(j)
end
return result
end
--------------------------------------
-- 抓取url
--------------------------------------
function fetchUrl(url, userAgent, sessioncookie, postData, proxy)
local t = {}
--header头
local headers = {}
local url = url
headers["Cookie"] = sessioncookie
--设置agent
if userAgent ~= nil then
headers["User-Agent"] = userAgent
end
--postData
local r, c, h
if postData == nil then
local requestData = {
url = url,
sink = ltn12.sink.table(t),
headers = headers,
method = "GET",
timeout = 5,
}
if proxy ~= nil then
requestData["proxy"] = proxy
end
r, c, h = http.request(requestData)
else
headers["Content-Type"] = "application/x-www-form-urlencoded"
headers["Content-Length"] = string.len(postData)
local requestData = {
url = url,
source = ltn12.source.string(postData),
sink = ltn12.sink.table(t),
method = "POST",
headers = headers,
timeout = 5,
}
if proxy ~= nil then
requestData["proxy"] = proxy
end
r, c, h = http.request(requestData, postData)
end
r = table.concat(t, "")
local cookie
local location
if (h) then
t = {}
for k, v in pairs(h) do
--print(k .. "----" .. v)
if k == "set-cookie" then
v = string.gsub(v, "(expires=.-; )", "")
v = v .. ", "
for cookie in string.gmatch(v, "(.-), ") do
cookie = string.match(cookie, "(.-);")
table.insert(t, cookie)
end
end
if k == "location" then
location = v;
end
end
cookie = table.concat(t, "; ")
else
cookie = nil
end
return r, c, cookie, location
end
function getByUrl(url)
local errorMsg = {}
local content = fetchUrl(url, FIREFOX_USERAGENT)
if content == nil then
return { ["code"] = 0x01, ["error"] = errorStatus[0x01] }
end
--errorMsg['content'] = content
local a1, b1 = string.find(content, 'var globallinkdata = ')
local a2, b2 = string.find(content, "}}}")
if b1 == nil or a2 == nil then
return { ["code"] = 0x02, ["error"] = errorStatus[0x02] }, errorMsg
end
local jsonStr = string.sub(content, b1 + 1, a2 + 2)
jsonStr = cjson2.decode(jsonStr)
if jsonStr == nil or jsonStr['retcode'] ~= '0000' then
return { ["code"] = 0x02, ["error"] = errorStatus[0x02] }, errorMsg
end
local downUrl = jsonStr['data']['resource']['files'][1]['downloadurl']
local key = jsonStr['data']['encryKey']
--print(key)
if downUrl == nil or key == nil then
return { ["code"] = 0x02, ["error"] = errorStatus[0x02] }, errorMsg
end
local res = encypt(downUrl, key);
a1, b1 = string.find(res, "vmall")
if a1 == nil then
return { ["code"] = 0x02, ["error"] = errorStatus[0x02] }, errorMsg
end
print(res)
local result = {
["result"] = {
["shd"] = res
}
}
--检查链接是否为空
if not checkUrlsType(result['result']) then
return { ["code"] = 0x07, ["error"] = errorStatus[0x07] }, errorMsg
end
--print(res)
return {["code"] = 0, ["content"] = result}
end
-------------------------------------
--检查是否有空链接
-------------------------------------
function checkUrlsType(urls)
if type(urls) ~= 'table' then
return false
end
local i = 0;
local urlsMaps = {'sd', 'shd', 'hd', 'ori'}
for _, type in pairs(urlsMaps) do
if checkIsset(urls, type) then
i = i + 1;
end
end
if i == 0 then
return false
end
return true;
end
-------------------------------------
--检查table中是否有某个值
-------------------------------------
function checkIsset(checkTable, item)
if type(checkTable) ~= 'table' then
return false;
end
local flag = false
for key, value in pairs(checkTable) do
if key == item then
flag = true
end
end
return flag
end
---------------------------
-- getByUrlJson
---------------------------
function getByUrlJson(params)
local params = cjson2.decode(params)
local result, errorMsg = getByUrl(params['url'])
local res
local info = {};
if params['server'] == 1 or params['errorVate'] == 1 then
if result["code"] == 0 then
info["type"] = params["type"]
info["site"] = params["site"]
info["meid"] = params["meid"]
info["mid"] = params["mid"]
info["vate"] = params["server"]
info["result"] = result["content"]["result"]
else
info["msg"] = result["error"]
info["site"] = params["site"]
info["url"] = params["url"]
info["vate"] = params["errorVate"]
if true and errorMsg ~= nil then
info['errorMsg'] = errorMsg
end
end
info["domain"] = params["domain"]
info["code"] = result["code"]
end
if result["code"] == 0 then
local msg = {}
local i = 1
for k, v in pairs(result["content"]["result"]) do
msg[i] = { ["format"] = k, ["url"] = v, ["urlType"] = 0 }
i = i + 1
end
res = { ["code"] = 0, ["msg"] = msg, ["postToServer"] = info }
else
res = { ["code"] = result["code"], ["error"] = result["error"] , ["postToServer"] = info}
end
return cjson2.encode(res)
end
local result = getByUrl('http://dl.vmall.com/c0kn104gzf#')

抓取百度云脚本

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
--------------------------------------
-- 百度云抓取
-- User: luoning
-- Date: 14-1-7
-- Time: 上午11:41
--------------------------------------
local http = require("socket.http")
local ltn12 = require("ltn12")
local cjson = require("cjson")
local cjson2 = cjson.new()
local string = require("string")
local table = require("table")
local base64 = require("mime")
--------------------------------------
---- error status
--------------------------------------
-- DOMAIN_URL = "http://api.wanhuatong.tv/lua/getvideo"
DOMAIN_URL = "http://http:192.168.1.65:81/lua/getvideo"
local errorStatus = {
[0x01] = "get page failed",
[0x02] = "parse page failed",
[0x03] = "unknown error",
[0x04] = "url is not right",
[0x05] = "timeout",
[0x06] = "video is not exists",
[0x07] = "urls is empty",
}
--------------------------------------
-- user-agent
--------------------------------------
XP_USERAGENT = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"
PHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16"
IPAD_USERAGENT = "Mozilla/5.0 (iPad; CPU OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B176 Safari/7534.48.3"
FIREFOX_USERAGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:16.0) Gecko/20100101 Firefox/16.0"
--------------------------------------
-- 抓取百度云
--------------------------------------
function getByUrl(url, code)
local errorMsg = {}
local tmpUrl = url
local content, status, cookie, location = fetchUrl(url, FIREFOX_USERAGENT);
if content == nil then
return { ["code"] = 0x01, ["error"] = errorStatus[0x01] };
end
--errorMsg['content'] = content
local urlf = string.find(url, "shareid=")
if urlf == nil then
if location ~= nil then
tmpUrl = location
else
local a1, b1 = string.find(content, "FileUtils.share_uk\=\"")
local a2, b2 = string.find(content, "\"\;FileUtils.share_id\=\"")
local a3, b3 = string.find(content, "\"\;FileUtils.share_sekey\=")
local uk = string.sub(content, b1 + 1, a2 - 1)
local shareid = string.sub(content, b2 + 1, a3 - 1)
tmpUrl = "?shareid=" .. shareid .. "&uk=" .. uk
end
end
urlf = string.find(tmpUrl, "shareid=")
if urlf == nil then
return { ["code"] = 2, ["error"] = errorStatus[2] }, errorMsg
end
urlf = string.find(tmpUrl, "?")
local headerStr
if code ~= nil then
local postData = "pwd=" .. code .. "&vcode="
local verfyUrl = "http://pan.baidu.com/share/verify" .. string.sub(url, urlf, #url)
content, status, cookie = fetchUrl(verfyUrl, PHONE_USERAGENT, nil, postData)
content = fetchUrl(url, FIREFOX_USERAGENT, cookie);
headerStr = cookie
end
--得到fid,sign,time
local f1, s1 = string.find(content, "\\\"fs_id\\\":\\\"")
local f2, s2 = string.find(content, "\\\",\\\"app_id\\\"")
if s1 == nil or f2 == nil then
errorMsg['secondContent'] = content
return { ["code"] = 0x02, ["error"] = errorStatus[0x02] }, errorMsg
end
local fid = string.sub(content, s1 + 1, f2 - 1)
f1, s1 = string.find(content, "FileUtils.share_timestamp=\"")
f2, s2 = string.find(content, "\";FileUtils.share_sign=\"")
if s1 == nil or f2 == nil then
return { ["code"] = 0x02, ["error"] = errorStatus[0x02] }, errorMsg
end
local stime = string.sub(content, s1 + 1, f2 - 1)
local f3, s3 = string.find(content, "\";disk.ui.Utilities.BETA_STR")
if f3 == nil then
return { ["code"] = 0x02, ["error"] = errorStatus[0x02] }, errorMsg
end
local sign = string.sub(content, s2 + 1, f3 - 1)
local tmpJsonUrl = "http://pan.baidu.com/share/download?" .. string.sub(tmpUrl, urlf + 1) .. "&fid_list=" .. escapes("[" .. fid .. "]") .. "&sign=" .. sign .. "&timestamp=" .. stime
local tmpJsonResult = fetchUrl(tmpJsonUrl, PHONE_USERAGENT, headerStr)
local tmpJsonTable = cjson.decode(tmpJsonResult)
if tmpJsonTable["img"] ~= nil then
errorMsg['thrCon'] = tmpJsonResult;
return { ["code"] = 0x02, ["error"] = errorStatus[0x02] }, errorMsg
end
local result = {}
if tmpJsonTable["dlink"] ~= nil then
result['result'] = { ["shd"] = tmpJsonTable["dlink"] }
--检查链接是否为空
if not checkUrlsType(result['result']) then
return { ["code"] = 0x07, ["error"] = errorStatus[0x07] }, errorMsg
end
else
return { ["code"] = 0x02, ["error"] = errorStatus[0x02] }, errorMsg
end
return { ["code"] = 0, ["content"] = result }
end
-------------------------------------
--检查是否有空链接
-------------------------------------
function checkUrlsType(urls)
if type(urls) ~= 'table' then
return false
end
local i = 0;
local urlsMaps = {'sd', 'shd', 'hd', 'ori'}
for _, type in pairs(urlsMaps) do
if checkIsset(urls, type) then
i = i + 1;
end
end
if i == 0 then
return false
end
return true;
end
-------------------------------------
--检查table中是否有某个值
-------------------------------------
function checkIsset(checkTable, item)
if type(checkTable) ~= 'table' then
return false;
end
local flag = false
for key, value in pairs(checkTable) do
if key == item then
flag = true
end
end
return flag
end
--------------------------------------
-- 抓取url
--------------------------------------
function fetchUrl(url, userAgent, sessioncookie, postData, proxy)
local t = {}
--header头
local headers = {}
local url = url
headers["Cookie"] = sessioncookie
--设置agent
if userAgent ~= nil then
headers["User-Agent"] = userAgent
end
--postData
local r, c, h
if postData == nil then
local requestData = {
url = url,
sink = ltn12.sink.table(t),
headers = headers,
method = "GET",
timeout = 5,
}
if proxy ~= nil then
requestData["proxy"] = proxy
end
r, c, h = http.request(requestData)
else
headers["Content-Type"] = "application/x-www-form-urlencoded"
headers["Content-Length"] = string.len(postData)
local requestData = {
url = url,
source = ltn12.source.string(postData),
sink = ltn12.sink.table(t),
method = "POST",
headers = headers,
timeout = 5,
}
if proxy ~= nil then
requestData["proxy"] = proxy
end
r, c, h = http.request(requestData, postData)
end
r = table.concat(t, "")
local cookie
local location
if (h) then
t = {}
for k, v in pairs(h) do
--print(k .. "----" .. v)
if k == "set-cookie" then
v = string.gsub(v, "(expires=.-; )", "")
v = v .. ", "
for cookie in string.gmatch(v, "(.-), ") do
cookie = string.match(cookie, "(.-);")
table.insert(t, cookie)
end
end
if k == "location" then
location = v;
end
end
cookie = table.concat(t, "; ")
else
cookie = nil
end
return r, c, cookie, location
end
---------------------------------------
-- urlencode
---------------------------------------
function escapes(s)
return string.gsub(s, "([^A-Za-z0-9_])", function(c)
return string.format("%%%02x", string.byte(c))
end)
end
---------------------------
-- getByUrlJson
---------------------------
function getByUrlJson(params)
local params = cjson2.decode(params)
local result, errorMsg = getByUrl(params['url'], params['code'])
local res
local info = {};
if params['server'] == 1 or params['errorVate'] == 1 then
if result["code"] == 0 then
info["type"] = params["type"]
info["site"] = params["site"]
info["meid"] = params["meid"]
info["mid"] = params["mid"]
info["vate"] = params["server"]
info["result"] = result["content"]["result"]
else
info["msg"] = result["error"]
info["site"] = params["site"]
info["url"] = params["url"]
info["vate"] = params["errorVate"]
if true and errorMsg ~= nil then
info['errorMsg'] = errorMsg
end
end
info["domain"] = params["domain"]
info["code"] = result["code"]
end
if result["code"] == 0 then
local msg = {}
local i = 1
for k, v in pairs(result["content"]["result"]) do
msg[i] = { ["format"] = k, ["url"] = v, ["urlType"] = 0 }
i = i + 1
end
res = { ["code"] = 0, ["msg"] = msg, ["postToServer"] = info }
else
res = { ["code"] = result["code"], ["error"] = result["error"], ["postToServer"] = info }
end
return cjson2.encode(res)
end
-----------------------------------
-- test
-----------------------------------
--getByUrl("http://pan.baidu.com/s/1mgiCtXQ", "5m6w")
-- result = getByUrl("http://pan.baidu.com/share/init?shareid=206527666&uk=3540312294", "3udl")
--result = getByUrl("http://pan.baidu.com/share/init?shareid=3239464838&uk=3540312294", "y887");
--print(result)