Skip to content

Commit 09db012

Browse files
committed
optimize: risk code -352
1 parent acda6f3 commit 09db012

File tree

6 files changed

+81
-13
lines changed

6 files changed

+81
-13
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "yz-yuki-plugin",
3-
"version": "2.0.5-8",
3+
"version": "2.0.5-9",
44
"description": "优纪插件,yunzaijs 关于 微博推送、B站推送 等功能的拓展插件",
55
"author": "snowtafir",
66
"type": "module",

src/models/bilibili/bilibili.api.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export class BiliApi {
99
//通过uid获取up详情 parama = { mid: uid, jsonp: jsonp }
1010
biliSpaceUserInfo: `https://api.bilibili.com/x/space/acc/info`,
1111

12-
//parama = { mid: uid, jsonp: jsonp }
12+
//parama = { mid: uid, token: '',platform: 'web', web_location: 1550101, w_webid, w_rid, wts }
1313
biliSpaceUserInfoWbi: `https://api.bilibili.com/x/space/wbi/acc/info`,
1414

1515
//通过关键词${upKeyword}搜索up主 parama = { keyword: 'upKeyword', page: 1, search_type: 'bili_user', order: 'totalrank', pagesize: 5 }
@@ -24,10 +24,13 @@ export class BiliApi {
2424
biliOpusDetail: 'https://api.bilibili.com/x/polymer/web-dynamic/v1/opus/detail'
2525
};
2626

27+
static BILIBILI_USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36';
28+
2729
/**header */
2830
static BILIBILI_HEADERS = {
2931
'Accept': '*/*',
3032
'Accept-Language': 'zh-CN,en-US;q=0.5',
33+
'Connection': 'keep-alive',
3134
'Accept-Encoding': 'gzip, deflate, br, zstd',
3235
'Cookie': '',
3336
'pragma': 'no-cache',
@@ -42,7 +45,7 @@ export class BiliApi {
4245
'Sec-Fetch-User': '?0',
4346
'Priority': 'u=4',
4447
'TE': 'trailers',
45-
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:132.0) Gecko/20100101 Firefox/132.0'
48+
'User-Agent': this.BILIBILI_USER_AGENT
4649
};
4750
/**Login header */
4851
static BIlIBILI_LOGIN_HEADERS = {
@@ -76,6 +79,21 @@ export class BiliApi {
7679
'Sec-Fetch-User': '?1',
7780
'TE': 'trailers',
7881
'Upgrade-Insecure-Requests': '1',
79-
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:132.0) Gecko/20100101 Firefox/132.0'
82+
'User-Agent': this.BILIBILI_USER_AGENT
83+
};
84+
85+
static BILIBILI_DYNAMIC_SPACE_HEADERS = {
86+
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
87+
'Accept-Encoding': 'gzip, deflate, br, zstd',
88+
'Accept-Language': 'zh-CN,en-US;q=0.5',
89+
'Connection': 'keep-alive',
90+
'Priority': 'u=0, i',
91+
'Sec-Fetch-Dest': 'document',
92+
'Sec-Fetch-Mode': 'navigate',
93+
'Sec-Fetch-Site': 'none',
94+
'Sec-Fetch-User': '?1',
95+
'Sec-GPC': '1',
96+
'Upgrade-Insecure-Requests': '1',
97+
'User-Agent': this.BILIBILI_USER_AGENT
8098
};
8199
}

src/models/bilibili/bilibili.dm.img.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ export async function getDmImg() {
55
const dm_img_str = 'V2ViR0wgMS';
66
//webgl unmasked renderer的值拼接webgl unmasked vendor的值的base64编码
77
const dm_cover_img_str = 'QU5HTEUgKEludGVsLCBJbnRlbChSKSBIRCBHcmFwaGljcyBEaXJlY3QzRDExIHZzXzVfMCBwc181XzApLCBvciBzaW1pbGFyR29vZ2xlIEluYy4gKEludGVsKQ';
8+
const dm_img_inter = { ds: [], wh: [0, 0, 0], of: [0, 0, 0] };
89
return {
910
dm_img_list: dm_img_list,
1011
dm_img_str: dm_img_str,
11-
dm_cover_img_str: dm_cover_img_str
12+
dm_cover_img_str: dm_cover_img_str,
13+
dm_img_inter: dm_img_inter
1214
};
1315
}

src/models/bilibili/bilibili.get.web.data.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { BiliApi } from '@src/models/bilibili//bilibili.api';
44
import { cookieWithBiliTicket, readSavedCookieItems, readSavedCookieOtherItems, readSyncCookie } from '@src/models/bilibili/bilibili.models';
55
import { getWbiSign } from '@src/models/bilibili//bilibili.wbi';
66
import { getDmImg } from '@src/models/bilibili/bilibili.dm.img';
7+
import { getWebId } from '@src/models/bilibili/bilibili.w_webid';
78

89
export class BiliGetWebData {
910
constructor(e?) {}
@@ -20,7 +21,7 @@ export class BiliGetWebData {
2021
'host_mid': uid,
2122
'timezone_offset': -480,
2223
'platform': 'web',
23-
'features': 'itemOpusStyle,listOnlyfans,opusBigCover,onlyfansVote',
24+
'features': 'itemOpusStyle,listOnlyfans,opusBigCover,onlyfansVote,decorationCard,forwardListHidden,ugcDelete,onlyfansQaCard',
2425
'web_location': '333.999',
2526
...dmImg,
2627
'x-bili-device-req-json': { platform: 'web', device: 'pc' },
@@ -51,15 +52,21 @@ export class BiliGetWebData {
5152
const url = BiliApi.BILIBIL_API.biliSpaceUserInfoWbi;
5253
let { cookie } = await readSyncCookie();
5354
cookie = await cookieWithBiliTicket(cookie);
55+
const dmImg = await getDmImg();
5456

5557
const data = {
5658
mid: uid,
57-
jsonp: 'jsonp'
59+
token: '',
60+
platform: 'web',
61+
web_location: 1550101,
62+
...dmImg
5863
};
5964
let signCookie = (await readSavedCookieItems(cookie, ['SESSDATA'], false)) || (await readSavedCookieOtherItems(cookie, ['SESSDATA']));
65+
const w_webid = await getWebId(uid);
6066
const { w_rid, time_stamp } = await getWbiSign(data, BiliApi.BILIBILI_HEADERS, signCookie);
6167
const params = {
6268
...data,
69+
w_webid: w_webid,
6370
w_rid: w_rid,
6471
wts: time_stamp
6572
};
@@ -69,8 +76,8 @@ export class BiliGetWebData {
6976
headers: lodash.merge(BiliApi.BILIBILI_HEADERS, {
7077
Cookie: `${cookie}`,
7178
Host: `api.bilibili.com`,
72-
Origin: 'https://www.bilibili.com',
73-
Referer: `https://www.bilibili.com/`
79+
Origin: 'https://space.bilibili.com',
80+
Referer: `https://space.bilibili.com/${uid}/dynamic`
7481
})
7582
});
7683
return res;

src/models/bilibili/bilibili.task.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export class BiliTask {
2828

2929
if (!resjson || resjson.code !== 0 || resjson.code === -352) {
3030
await postGateway(cookie);
31-
if (count < 3) {
31+
if (count < 2) {
3232
await this.randomDelay(2000, 8000); // 随机延时2-8秒
3333
await this.hendleEventDynamicData(uid, count + 1);
3434
logger.error(`获取 ${uid} 动态,Gateway count:${String(count)}`);
@@ -111,10 +111,10 @@ export class BiliTask {
111111
const dynamicData = resp.data?.items || [];
112112
dynamicList[subInfoOfup.uid] = dynamicData;
113113
} else if (resp.code === -352) {
114-
logger.error(`获取 ${subInfoOfup.uid} 动态失败,resCode:-352`);
115-
continue;
114+
logger.error(`获取 ${subInfoOfup.uid} 动态失败,resCode:-352,请待下次任务自动重试`);
115+
return;
116116
} else if (resp.code !== 0) {
117-
logger.error(`获取 ${subInfoOfup.uid} 动态失败,resCode:${resp.code}`);
117+
logger.error(`获取 ${subInfoOfup.uid} 动态失败,resCode:${resp.code},请待下次任务自动重试`);
118118
return;
119119
}
120120
} else {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import axios from 'axios';
2+
import lodash from 'lodash';
3+
import { Redis } from 'yunzaijs';
4+
import { BiliApi } from '@src/models/bilibili//bilibili.api';
5+
import { cookieWithBiliTicket, readSyncCookie } from '@src/models/bilibili/bilibili.models';
6+
7+
export async function getWebId(uid?: number) {
8+
const w_webid_key = 'Yz:yuki:bili:w_webid';
9+
const w_webid = await Redis.get(w_webid_key);
10+
if (w_webid) {
11+
return w_webid;
12+
} else {
13+
const url = `https://space.bilibili.com/${uid ? uid : 401742377}/dynamic`;
14+
let { cookie } = await readSyncCookie();
15+
cookie = await cookieWithBiliTicket(cookie);
16+
17+
const res = await axios.get(url, {
18+
timeout: 8000,
19+
headers: lodash.merge(BiliApi.BILIBILI_DYNAMIC_SPACE_HEADERS, {
20+
Cookie: `${cookie}`,
21+
Host: `space.bilibili.com`
22+
})
23+
});
24+
const htmlContent: string = await res.data;
25+
const htmlContentRegex = /="__RENDER_DATA__"\s*type="application\/json">(.*?)<\/script>/;
26+
const __RENDER_DATA__ = htmlContent.match(htmlContentRegex);
27+
28+
if (__RENDER_DATA__ && __RENDER_DATA__[1]) {
29+
const decoded__RENDER_DATA__JsonString = decodeURIComponent(__RENDER_DATA__[1]);
30+
const accessIdRegex = /"access_id":"(.*?)"/;
31+
const access_id = decoded__RENDER_DATA__JsonString.match(accessIdRegex);
32+
if (access_id && access_id[1]) {
33+
await Redis.set(w_webid_key, access_id[1], { EX: 43197 * 1000 });
34+
return access_id[1];
35+
} else {
36+
console.error('Failed to get access_id from __RENDER_DATA__');
37+
return null;
38+
}
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)