yz-yuki-plugin 2.0.8-9 → 2.0.9-1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,18 +1,21 @@
1
- import axios from 'axios';
2
- import lodash from 'lodash';
3
1
  import BiliApi from './bilibili.main.api.js';
4
- import { readSyncCookie, cookieWithBiliTicket, readSavedCookieItems, readSavedCookieOtherItems } from './bilibili.main.models.js';
5
- import { getWbiSign } from './bilibili.risk.wbi.js';
2
+ import BiliCookieManager from './bilibili.risk.cookie.js';
6
3
  import { getDmImg } from './bilibili.risk.dm.img.js';
4
+ import { getWbiSign } from './bilibili.risk.wbi.js';
5
+ import axioss from 'axios';
6
+ import { wrapper } from 'axios-cookiejar-support';
7
+ import lodash from 'lodash';
8
+ import * as tough from 'tough-cookie';
7
9
 
10
+ const axios = wrapper(axioss);
8
11
  class BilibiliWebDataFetcher {
9
12
  e;
10
13
  constructor(e) { }
11
14
  /**通过uid获取up动态数据表*/
12
15
  async getBiliDynamicListDataByUid(uid) {
13
16
  const url = BiliApi.BILIBIL_API.biliDynamicInfoList;
14
- let { cookie } = await readSyncCookie();
15
- cookie = await cookieWithBiliTicket(cookie);
17
+ const bili_jct = await BiliCookieManager.checkCookieBiliTicket();
18
+ const { cookie, mark } = await BiliCookieManager.readSyncCookie();
16
19
  const dmImg = await getDmImg();
17
20
  const data = {
18
21
  'offset': '',
@@ -25,31 +28,76 @@ class BilibiliWebDataFetcher {
25
28
  'x-bili-device-req-json': { platform: 'web', device: 'pc' },
26
29
  'x-bili-web-req-json': { spm_id: '333.999' }
27
30
  };
28
- let signCookie = (await readSavedCookieItems(cookie, ['SESSDATA'], false)) || (await readSavedCookieOtherItems(cookie, ['SESSDATA']));
31
+ // 根据 mark 的值计算 signCookie
32
+ const signCookie = mark === 'localCk'
33
+ ? (await BiliCookieManager.readSavedCookieItems(`${bili_jct}+${cookie}`, ['SESSDATA'], false)) ||
34
+ (await BiliCookieManager.readSavedCookieOtherItems(`${bili_jct}+${cookie}`, ['SESSDATA']))
35
+ : (await BiliCookieManager.readSavedCookieItems(await BiliCookieManager.getCookieStringForUrl(url), ['SESSDATA'], false)) ||
36
+ (await BiliCookieManager.readSavedCookieOtherItems(await BiliCookieManager.getCookieStringForUrl(url), ['SESSDATA']));
29
37
  const { w_rid, time_stamp } = await getWbiSign(data, BiliApi.BILIBILI_HEADERS, signCookie);
30
38
  const params = {
31
39
  ...data,
32
40
  w_rid: w_rid,
33
41
  wts: time_stamp
34
42
  };
35
- const res = await axios(url, {
36
- method: 'GET',
43
+ const headers = lodash.merge(BiliApi.BILIBILI_HEADERS, {
44
+ Cookie: mark === 'localCk' ? `${bili_jct}+${cookie}` : undefined,
45
+ Host: `api.bilibili.com`,
46
+ Origin: 'https://space.bilibili.com',
47
+ Referer: `https://space.bilibili.com/${uid}/dynamic`
48
+ });
49
+ const ck = cookie instanceof tough.CookieJar ? cookie : undefined;
50
+ // (1) 保存初始状态
51
+ let initialCookies;
52
+ if (ck) {
53
+ initialCookies = await new Promise((resolve, reject) => {
54
+ ck.store.getAllCookies((err, cookies) => {
55
+ if (err)
56
+ reject(err);
57
+ else
58
+ resolve(cookies || []);
59
+ });
60
+ });
61
+ }
62
+ const res = await axios.get(url, {
63
+ jar: ck, // 仅在非 localCk 时传递 jar
37
64
  params,
38
65
  timeout: 15000,
39
- headers: lodash.merge(BiliApi.BILIBILI_HEADERS, {
40
- Cookie: `${cookie}`,
41
- Host: `api.bilibili.com`,
42
- Origin: 'https://space.bilibili.com',
43
- Referer: `https://space.bilibili.com/${uid}/dynamic`
44
- })
66
+ headers
45
67
  });
68
+ // (3) 获取请求完成后的 Cookie 状态
69
+ let updatedCookies;
70
+ if (ck) {
71
+ updatedCookies = await new Promise((resolve, reject) => {
72
+ ck.store.getAllCookies((err, cookies) => {
73
+ if (err)
74
+ reject(err);
75
+ else
76
+ resolve(cookies || []);
77
+ });
78
+ });
79
+ }
80
+ if (ck && initialCookies instanceof Array && updatedCookies instanceof Array) {
81
+ // 构建初始状态的 Map
82
+ const initialCookieMap = new Map(initialCookies.map(c => [`${c.key}:${c.domain}`, c]));
83
+ // 构建更新后的 Map
84
+ const updatedCookieMap = new Map(updatedCookies.map(c => [`${c.key}:${c.domain}`, c]));
85
+ // 比较两个 Map
86
+ for (const [key, updatedCookie] of updatedCookieMap.entries()) {
87
+ const initialCookie = initialCookieMap.get(key);
88
+ if (!initialCookie || initialCookie.value !== updatedCookie.value) {
89
+ console.log(`Weibo Cookie ${updatedCookie.key} was updated.`);
90
+ await BiliCookieManager.saveCookiesToRedis(ck); // 更新同步到 Redis
91
+ }
92
+ }
93
+ }
46
94
  return res;
47
95
  }
48
96
  /**通过uid获取up详情*/
49
97
  async getBilibiUserInfoByUid(uid) {
50
98
  const url = BiliApi.BILIBIL_API.biliSpaceUserInfoWbi;
51
- let { cookie } = await readSyncCookie();
52
- cookie = await cookieWithBiliTicket(cookie);
99
+ const bili_jct = await BiliCookieManager.checkCookieBiliTicket();
100
+ const { cookie, mark } = await BiliCookieManager.readSyncCookie();
53
101
  const dmImg = await getDmImg();
54
102
  const data = {
55
103
  mid: uid,
@@ -58,81 +106,102 @@ class BilibiliWebDataFetcher {
58
106
  web_location: 1550101,
59
107
  ...dmImg
60
108
  };
61
- let signCookie = (await readSavedCookieItems(cookie, ['SESSDATA'], false)) || (await readSavedCookieOtherItems(cookie, ['SESSDATA']));
109
+ // 根据 mark 的值计算 signCookie
110
+ const signCookie = mark === 'localCk'
111
+ ? (await BiliCookieManager.readSavedCookieItems(`${bili_jct}+${cookie}`, ['SESSDATA'], false)) ||
112
+ (await BiliCookieManager.readSavedCookieOtherItems(`${bili_jct}+${cookie}`, ['SESSDATA']))
113
+ : (await BiliCookieManager.readSavedCookieItems(await BiliCookieManager.getCookieStringForUrl(url), ['SESSDATA'], false)) ||
114
+ (await BiliCookieManager.readSavedCookieOtherItems(await BiliCookieManager.getCookieStringForUrl(url), ['SESSDATA']));
62
115
  const { w_rid, time_stamp } = await getWbiSign(data, BiliApi.BILIBILI_HEADERS, signCookie);
63
116
  const params = {
64
117
  ...data,
65
118
  w_rid: w_rid,
66
119
  wts: time_stamp
67
120
  };
68
- const res = await axios(url, {
69
- method: 'GET',
121
+ const headers = lodash.merge(BiliApi.BILIBILI_HEADERS, {
122
+ Cookie: mark === 'localCk' ? `${bili_jct}+${cookie}` : undefined,
123
+ Host: `api.bilibili.com`,
124
+ Origin: 'https://space.bilibili.com',
125
+ Referer: `https://space.bilibili.com/${uid}/dynamic`
126
+ });
127
+ const ck = cookie instanceof tough.CookieJar ? cookie : undefined;
128
+ const res = await axios.get(url, {
129
+ jar: ck, // 仅在非 localCk 时传递 jar
70
130
  params,
71
- timeout: 10000,
72
- headers: lodash.merge(BiliApi.BILIBILI_HEADERS, {
73
- Cookie: `${cookie}`,
74
- Host: `api.bilibili.com`,
75
- Origin: 'https://space.bilibili.com',
76
- Referer: `https://space.bilibili.com/${uid}/dynamic`
77
- })
131
+ timeout: 15000,
132
+ headers
78
133
  });
79
134
  return res;
80
135
  }
81
136
  /**通过关键词搜索up*/
82
137
  async searchBiliUserInfoByKeyword(keyword) {
83
138
  const url = BiliApi.BILIBIL_API.biliSearchUpWbi;
84
- let { cookie } = await readSyncCookie();
85
- cookie = await cookieWithBiliTicket(cookie);
139
+ const bili_jct = await BiliCookieManager.checkCookieBiliTicket();
140
+ const { cookie, mark } = await BiliCookieManager.readSyncCookie();
86
141
  const data = {
87
142
  keyword: keyword,
88
143
  page: 1,
89
144
  search_type: 'bili_user',
90
145
  order: 'totalrank'
91
146
  };
92
- let signCookie = (await readSavedCookieItems(cookie, ['SESSDATA'], false)) || (await readSavedCookieOtherItems(cookie, ['SESSDATA']));
147
+ // 根据 mark 的值计算 signCookie
148
+ const signCookie = mark === 'localCk'
149
+ ? (await BiliCookieManager.readSavedCookieItems(`${bili_jct}+${cookie}`, ['SESSDATA'], false)) ||
150
+ (await BiliCookieManager.readSavedCookieOtherItems(`${bili_jct}+${cookie}`, ['SESSDATA']))
151
+ : (await BiliCookieManager.readSavedCookieItems(await BiliCookieManager.getCookieStringForUrl(url), ['SESSDATA'], false)) ||
152
+ (await BiliCookieManager.readSavedCookieOtherItems(await BiliCookieManager.getCookieStringForUrl(url), ['SESSDATA']));
93
153
  const { w_rid, time_stamp } = await getWbiSign(data, BiliApi.BILIBILI_HEADERS, signCookie);
94
154
  const params = {
95
155
  ...data,
96
156
  w_rid: w_rid,
97
157
  wts: time_stamp
98
158
  };
99
- const res = await axios(url, {
100
- method: 'GET',
159
+ const headers = lodash.merge(BiliApi.BILIBILI_HEADERS, {
160
+ Cookie: mark === 'localCk' ? `${bili_jct}+${cookie}` : undefined,
161
+ Host: `api.bilibili.com`,
162
+ Origin: 'https://www.bilibili.com',
163
+ Referer: `https://www.bilibili.com/`
164
+ });
165
+ const ck = cookie instanceof tough.CookieJar ? cookie : undefined;
166
+ const res = await axios.get(url, {
167
+ jar: ck, // 仅在非 localCk 时传递 jar
101
168
  params,
102
- timeout: 10000,
103
- headers: lodash.merge(BiliApi.BILIBILI_HEADERS, {
104
- Cookie: `${cookie}`,
105
- Host: `api.bilibili.com`,
106
- Origin: 'https://www.bilibili.com',
107
- Referer: `https://www.bilibili.com/`
108
- })
169
+ timeout: 15000,
170
+ headers
109
171
  });
110
172
  return res;
111
173
  }
112
174
  /*通过aid/bvid获取视频信息*/
113
175
  async getBiliVideoInfoByAid_BV(vedioID) {
114
176
  const url = BiliApi.BILIBIL_API.biliVideoInfoWbi;
115
- let { cookie } = await readSyncCookie();
116
- cookie = await cookieWithBiliTicket(cookie);
177
+ const bili_jct = await BiliCookieManager.checkCookieBiliTicket();
178
+ const { cookie, mark } = await BiliCookieManager.readSyncCookie();
117
179
  let referer = vedioID?.bvid ? `https://www.bilibili.com/video/${vedioID.bvid}` : `https://www.bilibili.com/video/av${vedioID.aid}`;
118
180
  let data = vedioID?.bvid ? { bvid: vedioID.bvid } : { aid: vedioID.aid };
119
- let signCookie = (await readSavedCookieItems(cookie, ['SESSDATA'], false)) || (await readSavedCookieOtherItems(cookie, ['SESSDATA']));
181
+ // 根据 mark 的值计算 signCookie
182
+ const signCookie = mark === 'localCk'
183
+ ? (await BiliCookieManager.readSavedCookieItems(`${bili_jct}+${cookie}`, ['SESSDATA'], false)) ||
184
+ (await BiliCookieManager.readSavedCookieOtherItems(`${bili_jct}+${cookie}`, ['SESSDATA']))
185
+ : (await BiliCookieManager.readSavedCookieItems(await BiliCookieManager.getCookieStringForUrl(url), ['SESSDATA'], false)) ||
186
+ (await BiliCookieManager.readSavedCookieOtherItems(await BiliCookieManager.getCookieStringForUrl(url), ['SESSDATA']));
120
187
  const { w_rid, time_stamp } = await getWbiSign(data, BiliApi.BILIBILI_HEADERS, signCookie);
121
188
  const params = {
122
189
  ...data,
123
190
  w_rid: w_rid,
124
191
  wts: time_stamp
125
192
  };
126
- const res = await axios(url, {
127
- method: 'GET',
193
+ const headers = lodash.merge(BiliApi.BILIBILI_HEADERS, {
194
+ Cookie: mark === 'localCk' ? `${bili_jct}+${cookie}` : undefined,
195
+ Host: `api.bilibili.com`,
196
+ Origin: 'https://www.bilibili.com',
197
+ Referer: referer
198
+ });
199
+ const ck = cookie instanceof tough.CookieJar ? cookie : undefined;
200
+ const res = await axios.get(url, {
201
+ jar: ck, // 仅在非 localCk 时传递 jar
128
202
  params,
129
- timeout: 5000,
130
- headers: lodash.merge(BiliApi.BILIBILI_HEADERS, {
131
- Cookie: `${cookie}`,
132
- Host: `api.bilibili.com`,
133
- Origin: 'https://www.bilibili.com',
134
- Referer: referer
135
- })
203
+ timeout: 15000,
204
+ headers
136
205
  });
137
206
  return res;
138
207
  }
@@ -140,14 +209,16 @@ class BilibiliWebDataFetcher {
140
209
  async getBVIDByShortUrl(tvUrlID) {
141
210
  const ShortVideoUrlApi = BiliApi.BILIBIL_API.biliShortVideoUrl;
142
211
  const url = `${ShortVideoUrlApi}${tvUrlID}`;
143
- let { cookie } = await readSyncCookie();
144
- cookie = await cookieWithBiliTicket(cookie);
145
- const res = await axios(url, {
146
- method: 'GET',
147
- timeout: 5000,
148
- headers: lodash.merge(BiliApi.BILIBILI_DYNAMIC_SPACE_HEADERS, {
149
- Cookie: `${cookie}`
150
- })
212
+ const bili_jct = await BiliCookieManager.checkCookieBiliTicket();
213
+ const { cookie, mark } = await BiliCookieManager.readSyncCookie();
214
+ const headers = lodash.merge(BiliApi.BILIBILI_DYNAMIC_SPACE_HEADERS, {
215
+ Cookie: mark === 'localCk' ? `${bili_jct}+${cookie}` : undefined
216
+ });
217
+ const ck = cookie instanceof tough.CookieJar ? cookie : undefined;
218
+ const res = await axios.get(url, {
219
+ jar: ck, // 仅在非 localCk 时传递 jar
220
+ timeout: 15000,
221
+ headers
151
222
  });
152
223
  const htmlContent = await res.data;
153
224
  const htmlContentRegex = /itemprop="url"\s*content="https:\/\/www.bilibili.com\/video\/(BV[a-zA-Z0-9]+)\/">/;
@@ -1,9 +1,10 @@
1
- import moment from 'moment';
2
- import { Segment } from 'yunzaijs';
3
- import { readSyncCookie, cookieWithBiliTicket } from './bilibili.main.models.js';
4
1
  import BiliApi from './bilibili.main.api.js';
5
- import axios from 'axios';
2
+ import BiliCookieManager from './bilibili.risk.cookie.js';
3
+ import axioss from 'axios';
6
4
  import lodash from 'lodash';
5
+ import moment from 'moment';
6
+ import * as tough from 'tough-cookie';
7
+ import { Segment } from 'yunzaijs';
7
8
 
8
9
  class BiliQuery {
9
10
  /**
@@ -246,11 +247,18 @@ class BiliQuery {
246
247
  * @returns 完整的B站文章内容json数据
247
248
  */
248
249
  static async getFullArticleContent(postUrl) {
249
- let { cookie } = await readSyncCookie();
250
- cookie = await cookieWithBiliTicket(cookie);
250
+ const bili_jct = await BiliCookieManager.checkCookieBiliTicket();
251
+ const { cookie, mark } = await BiliCookieManager.readSyncCookie();
251
252
  try {
252
- const response = await axios.get(postUrl, {
253
- headers: lodash.merge(BiliApi.BILIBILI_ARTICLE_HEADERS, { Cookie: `${cookie}`, Host: 'www.bilibili.com' }),
253
+ const headers = lodash.merge(BiliApi.BILIBILI_ARTICLE_HEADERS, {
254
+ Cookie: mark === 'localCk' ? `${bili_jct}+${cookie}` : undefined,
255
+ Host: 'www.bilibili.com'
256
+ });
257
+ const ck = cookie instanceof tough.CookieJar ? cookie : undefined;
258
+ const response = await axioss.get(postUrl, {
259
+ jar: ck, // 仅在非 localCk 时传递 jar
260
+ timeout: 15000,
261
+ headers,
254
262
  responseType: 'text'
255
263
  });
256
264
  const text = response.data;
@@ -1,10 +1,11 @@
1
- import QRCode from 'qrcode';
2
- import { Redis, Segment, Bot } from 'yunzaijs';
3
- import Config from '../../utils/config.js';
4
- import { renderPage } from '../../utils/image.js';
5
1
  import { BilibiliWebDataFetcher } from './bilibili.main.get.web.data.js';
6
- import { readSyncCookie, postGateway } from './bilibili.main.models.js';
7
2
  import { BiliQuery } from './bilibili.main.query.js';
3
+ import BiliCookieManager from './bilibili.risk.cookie.js';
4
+ import Config from '../../utils/config.js';
5
+ import { renderPage } from '../../utils/image.js';
6
+ import QRCode from 'qrcode';
7
+ import * as tough from 'tough-cookie';
8
+ import { Redis, Segment, Bot } from 'yunzaijs';
8
9
 
9
10
  class BiliTask {
10
11
  taskName;
@@ -17,11 +18,27 @@ class BiliTask {
17
18
  this.privateKey = 'Yz:yuki:bili:upPush:private:';
18
19
  }
19
20
  async hendleEventDynamicData(uid, count = 0) {
20
- let { cookie } = await readSyncCookie();
21
+ let { cookie, mark } = await BiliCookieManager.readSyncCookie();
21
22
  const resp = await new BilibiliWebDataFetcher().getBiliDynamicListDataByUid(uid);
22
23
  const resjson = await resp?.data;
23
24
  if (!resjson || resjson.code !== 0 || resjson.code === -352) {
24
- await postGateway(cookie);
25
+ let ck;
26
+ if (mark === 'localCk') {
27
+ ck = cookie;
28
+ }
29
+ else {
30
+ if (cookie instanceof tough.CookieJar) {
31
+ ck = await new Promise((resolve, reject) => {
32
+ cookie.getCookieString('https://www.bilibili.com', (err, cookieString) => {
33
+ if (err)
34
+ reject(err);
35
+ else
36
+ resolve(cookieString || '');
37
+ });
38
+ });
39
+ }
40
+ }
41
+ await BiliCookieManager.postGateway(ck);
25
42
  if (count < 2) {
26
43
  await this.randomDelay(2000, 8000); // 随机延时2-8秒
27
44
  await this.hendleEventDynamicData(uid, count + 1);