@@ -191,11 +191,12 @@ function setCookieEval(jscode: string, site: string) {
191191}
192192
193193const getFiles = async function ( page : string , parentPath = '' , files : LanZouFile [ ] ) {
194- const doc = await getDocument ( page ) ;
194+ const doc = await fetch3 ( page , { } , 'document' ) as Document ;
195195 const script = doc . getElementsByTagName ( 'script' ) . find ( s =>
196196 s . innerHTML . includes ( '$.ajax' )
197197 ) ;
198198 if ( ! script ) {
199+ console . warn ( doc . body . innerHTML ) ;
199200 throw new Error ( '找不到script部分,确保这是蓝奏云分享链接!' ) ;
200201 }
201202
@@ -210,10 +211,10 @@ const getFiles = async function (page: string, parentPath = '', files: LanZouFil
210211 let lastNum = 50 ; // 每页显示50个文件
211212 while ( lastNum == 50 ) {
212213 formData . set ( 'pg' , pgnum . toString ( ) ) ;
213- const list = await fetch2 ( new URL ( url , page ) , {
214+ const list = await fetch3 ( new URL ( url , page ) , {
214215 method : 'POST' ,
215216 body : formData
216- } ) . then ( r => r . json ( ) ) ;
217+ } , ' json' )
217218 if ( list . info != 'sucess' ) {
218219 if ( typeof list . info === 'string' && list . info . includes ( '重试' ) ) {
219220 console . warn ( `获取第 ${ pgnum } 页文件列表出现问题:蓝奏云限制!` ) ;
@@ -250,6 +251,54 @@ const getFiles = async function (page: string, parentPath = '', files: LanZouFil
250251 return files ;
251252}
252253
254+ async function fetch3 ( urlRaw : URL | string , fetchOps ?: any , expect : 'document' | 'binary' | 'json' = 'json' ) {
255+ let textpath = new URL ( urlRaw ) ;
256+ let text2 = await fetch2 ( urlRaw , fetchOps ) ;
257+ while ( true ) {
258+ let document : Document ;
259+ if ( expect == 'binary' ) {
260+ if ( text2 . headers . get ( 'Content-Type' ) ?. startsWith ( 'text/html' ) )
261+ document = new DOMParser ( ) . parseFromString ( await text2 . text ( ) , 'text/html' ) ;
262+ else return text2 ;
263+ } else {
264+ const text = await text2 . text ( ) ;
265+ document = new DOMParser ( ) . parseFromString ( text , 'text/html' ) ;
266+ if ( document . body . innerText . trim ( ) )
267+ if ( expect == 'json' ) return JSON . parse ( text ) ;
268+ else return document ;
269+ }
270+ const script = document . getElementsByTagName ( 'script' ) . at ( - 1 ) ! ;
271+
272+ // 处理acw_sc__v2
273+ if ( script . innerHTML . includes ( 'acw_sc' ) ) {
274+ setCookieEval ( script . innerHTML , textpath . href ) ;
275+ text2 = await fetch2 ( textpath ) ;
276+ continue ; // retry
277+ }
278+
279+ const func = extractFunctionByName ( script . innerHTML , 'down_r' ) ! ;
280+ const { url, data } = sandboxEval ( func , 'var el = 2;' + script . innerHTML ) ;
281+ const formData = new FormData ( ) ;
282+ for ( const [ key , value ] of Object . entries ( data ) ) {
283+ formData . append ( key , String ( value ) ) ;
284+ }
285+ await delay ( 2241 + 1000 * Math . random ( ) ) ;
286+ const file2 = await fetch2 ( new URL ( url , textpath ) , {
287+ body : formData ,
288+ method : 'POST' ,
289+ referrer : textpath . href ,
290+ headers : {
291+ Origin : textpath . origin ,
292+ "X-Requested-With" : "XMLHttpRequest"
293+ }
294+ } ) . then ( r => r . json ( ) ) ;
295+ if ( file2 . zt != 1 ) throw new Error ( '验证网络:链接超时' ) ;
296+ const urlreal = new URL ( file2 . url , textpath ) ;
297+ await delay ( 1000 * Math . random ( ) + 621 ) ;
298+ return await fetch2 ( urlreal ) ;
299+ }
300+ }
301+
253302async function downloadFile ( docurl : string ) {
254303 const document1 = await getDocument ( docurl ) ;
255304 for ( const iframe of document1 . getElementsByTagName ( 'iframe' ) ) {
@@ -264,56 +313,22 @@ async function downloadFile(docurl: string) {
264313 formData . append ( key , String ( value ) ) ;
265314 }
266315 await delay ( 143 + 1000 * Math . random ( ) ) ;
267- const file = await fetch2 ( new URL ( url , docurl ) , {
316+ const file = await fetch3 ( new URL ( url , docurl ) , {
268317 body : formData ,
269318 method : 'POST' ,
270319 referrer : docurl2 . href ,
271320 headers : {
272321 Origin : docurl2 . origin ,
273322 "X-Requested-With" : "XMLHttpRequest"
274323 }
275- } ) . then ( r => r . json ( ) ) ;
324+ } , ' json' )
276325 if ( file . zt != 1 ) throw new Error ( '下载 ' + file . name + ' 失败: 链接超时' ) ;
277326 const realpath = file . dom + '/file/' + file . url ;
278327
279328 await delay ( 324 + 1000 * Math . random ( ) ) ;
280329 const textpath = new URL ( realpath , docurl ) ;
281- let text2 = await fetch2 ( textpath ) ;
282- // 网络验证
283- if ( text2 . headers . get ( 'Content-Type' ) ?. includes ( 'text/html' ) ) while ( true ) {
284- const document = new DOMParser ( ) . parseFromString ( await text2 . text ( ) , 'text/html' ) ;
285- const script = document . getElementsByTagName ( 'script' ) . at ( - 1 ) ! ;
286-
287- // 处理acw_sc__v2
288- if ( script . innerHTML . includes ( 'acw_sc' ) ) {
289- setCookieEval ( script . innerHTML , textpath . href ) ;
290- text2 = await fetch2 ( textpath ) ;
291- continue ; // retry
292- }
293-
294- const func = extractFunctionByName ( script . innerHTML , 'down_r' ) ! ;
295- const { url, data } = sandboxEval ( func , 'var el = 2;' + script . innerHTML ) ;
296- const formData = new FormData ( ) ;
297- for ( const [ key , value ] of Object . entries ( data ) ) {
298- formData . append ( key , String ( value ) ) ;
299- }
300- await delay ( 2241 + 1000 * Math . random ( ) ) ;
301- const file2 = await fetch2 ( new URL ( url , textpath ) , {
302- body : formData ,
303- method : 'POST' ,
304- referrer : textpath . href ,
305- headers : {
306- Origin : textpath . origin ,
307- "X-Requested-With" : "XMLHttpRequest"
308- }
309- } ) . then ( r => r . json ( ) ) ;
310- if ( file2 . zt != 1 ) throw new Error ( '下载 ' + file . name + ' 失败: 验证网络:链接超时' ) ;
311- const urlreal = new URL ( file2 . url , textpath ) ;
312- await delay ( 1000 * Math . random ( ) + 621 ) ;
313- return await fetch2 ( urlreal ) ;
314- } else {
315- return text2 ;
316- }
330+ let text2 = await fetch3 ( textpath , { } , 'binary' ) ;
331+ return text2 ;
317332 }
318333 throw new Error ( '下载 ' + docurl + ' 失败: 找不到文件下载链接!' ) ;
319334}
0 commit comments