11import { load } from 'cheerio' ;
22import CryptoJS from 'crypto-js' ;
33
4- import got from '@/utils/got' ;
4+ import { solveWafChallenge } from '@/routes/juejin/utils' ;
5+ import cache from '@/utils/cache' ;
6+ import ofetch from '@/utils/ofetch' ;
57
6- const rootUrl = 'https://www.36kr.com' ;
8+ export const rootUrl = 'https://www.36kr.com' ;
79
8- const ProcessItem = ( item , tryGet ) =>
10+ export const ProcessItem = ( item , tryGet ) =>
911 tryGet ( item . link , async ( ) => {
10- const detailResponse = await got ( {
11- method : 'get' ,
12- url : item . link ,
13- } ) ;
12+ const detailResponse = await ofetch ( item . link ) ;
1413
15- const cipherTextList = detailResponse . data . match ( / { " s t a t e " : " ( .* ) " , " i s E n c r y p t " : t r u e } / ) ?? [ ] ;
14+ const cipherTextList = detailResponse . match ( / { " s t a t e " : " ( .* ) " , " i s E n c r y p t " : t r u e } / ) ?? [ ] ;
1615
1716 if ( cipherTextList . length === 0 ) {
18- const $ = load ( detailResponse . body ) ;
17+ const $ = load ( detailResponse ) ;
1918 item . description = $ ( 'div.articleDetailContent' ) . html ( ) ;
2019 } else {
2120 const key = CryptoJS . enc . Utf8 . parse ( 'efabccee-b754-4c' ) ;
@@ -33,4 +32,33 @@ const ProcessItem = (item, tryGet) =>
3332 return item ;
3433 } ) ;
3534
36- export { ProcessItem , rootUrl } ;
35+ export const getWafTokenId = ( ) =>
36+ cache . tryGet (
37+ '36kr:_waftokenid' ,
38+ async ( ) => {
39+ const captchaResponse = await ofetch ( rootUrl ) ;
40+
41+ const $ = load ( captchaResponse ) ;
42+ const payload = $ ( 'script' )
43+ . text ( )
44+ . match ( / a t o b \( ' ( .* ?) ' \) \) , / ) ?. [ 1 ] ;
45+ const response = solveWafChallenge ( payload ) ;
46+
47+ const tokenIdResponse = await ofetch . raw ( rootUrl , {
48+ headers : {
49+ Cookie : `_wafchallengeid=${ response } ;` ,
50+ } ,
51+ redirect : 'manual' ,
52+ } ) ;
53+
54+ const _wafTokenId = tokenIdResponse . headers
55+ . getSetCookie ( )
56+ . find ( ( cookie ) => cookie . startsWith ( '_waftokenid=' ) )
57+ ?. split ( ';' ) [ 0 ]
58+ . split ( '=' ) [ 1 ] ;
59+
60+ return _wafTokenId as string ;
61+ } ,
62+ 300 , // server-provided value
63+ false
64+ ) ;
0 commit comments