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

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.
@@ -8,7 +8,7 @@ class WeiboApi {
8
8
  this.USER_AGENT = WeiboApi.WEIBO_USER_AGENT;
9
9
  this.initialize();
10
10
  }
11
- static WEIBO_USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36';
11
+ static WEIBO_USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36';
12
12
  //初始化User-Agent
13
13
  async initialize() {
14
14
  await this.initUserAgent();
@@ -22,25 +22,22 @@ class WeiboApi {
22
22
  }
23
23
  get WEIBO_API() {
24
24
  return {
25
- weiboGetIndex: 'https://m.weibo.cn/api/container/getIndex',
26
- //通过关键词${upKeyword}搜索博主 parama = { q: 'Keyword'},
27
- weiboAjaxSearch: 'https://weibo.com/ajax/side/search'
25
+ weiboGetIndex: 'https://m.weibo.cn/api/container/getIndex'
28
26
  };
29
27
  }
30
28
  /**统一设置header */
31
29
  get WEIBO_HEADERS() {
32
30
  return {
33
31
  'Accept': 'application/json, text/plain, */*',
34
- 'Accept-Language': 'zh-CN,en-US;q=0.5',
35
32
  'Accept-Encoding': 'gzip, deflate, br, zstd',
36
- 'X-Requested-With': 'XMLHttpRequest',
37
- 'MWeibo-Pwa': '1',
38
- 'X-XSRF-TOKEN': '',
33
+ 'Accept-Language': 'zh-CN,zh;q=0.9',
34
+ 'Connection': 'keep-alive',
39
35
  'Sec-Fetch-Dest': 'empty',
40
36
  'Sec-Fetch-Mode': 'cors',
41
37
  'Sec-Fetch-Site': 'same-origin',
42
- 'Connection': 'keep-alive',
43
- 'User-agent': this.USER_AGENT
38
+ 'X-Requested-With': 'XMLHttpRequest',
39
+ 'MWeibo-Pwa': '1',
40
+ 'User-Agent': this.USER_AGENT
44
41
  };
45
42
  }
46
43
  get WEIBO_GET_X_CSRF_TOKEN_HEADERS() {
@@ -1,113 +1,171 @@
1
1
  import WeiboApi from './weibo.main.api.js';
2
- import { WeiboMainModels } from './weibo.main.models.js';
3
2
  import { WeiboQuery } from './weibo.main.query.js';
4
- import axios from 'axios';
5
- import { randomInt } from 'crypto';
3
+ import WeiboCookieManager from './weibo.risk.cookie.js';
4
+ import axioss from 'axios';
5
+ import { wrapper } from 'axios-cookiejar-support';
6
6
  import lodash from 'lodash';
7
7
 
8
- function generateRandomString(length = 6) {
9
- const characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
10
- let result = '';
11
- for (let i = 0; i < length; i++) {
12
- const randomIndex = randomInt(0, characters.length);
13
- result += characters[randomIndex];
14
- }
15
- return result;
16
- }
8
+ const axios = wrapper(axioss);
17
9
  class WeiboWebDataFetcher {
18
10
  e;
19
11
  constructor(e) { }
20
12
  /**通过uid获取博主信息 */
21
- async getBloggerInfo(target) {
22
- const param = { containerid: '100505' + target };
13
+ async getBloggerInfo(uid) {
14
+ const param = { type: 'uid', value: `${uid}`, containerid: '100505' + `${uid}` };
23
15
  const url = new URL(WeiboApi.WEIBO_API.weiboGetIndex);
24
16
  url.search = new URLSearchParams(param).toString();
25
- const { cookie, mark } = await WeiboMainModels.readSyncCookie();
26
- let COOKIE = '', XSRF_TOKEN = '';
27
- if (String(cookie).includes('XSRF-TOKEN')) {
28
- COOKIE = cookie;
29
- XSRF_TOKEN = await WeiboMainModels.readSavedCookieItems(cookie, ['XSRF-TOKEN'], false);
30
- }
31
- else {
32
- logger.warn(`优纪插件:获取博主信息,未登录,如获取报错或失败请先 #扫码微博登录`);
33
- XSRF_TOKEN = generateRandomString();
34
- COOKIE = `XSRF_TOKEN=${XSRF_TOKEN}`;
35
- }
17
+ const jar = await WeiboCookieManager.getSessionCookieJar();
18
+ const X_XSRF_TOKEN = await WeiboCookieManager.getCookieValueByKeyFromString(jar, 'X-XSRF-TOKEN', url.toString());
19
+ // (1) 保存初始状态
20
+ const initialCookies = await new Promise((resolve, reject) => {
21
+ jar.store.getAllCookies((err, cookies) => {
22
+ if (err)
23
+ reject(err);
24
+ else
25
+ resolve(cookies || []);
26
+ });
27
+ });
36
28
  const resp = await axios(url.toString(), {
37
29
  method: 'GET',
30
+ jar,
31
+ withCredentials: true,
38
32
  timeout: 10000,
39
- headers: lodash.merge(WeiboApi.WEIBO_HEADERS, { 'X-XSRF-TOKEN': `${XSRF_TOKEN}`, 'Cookie': `${COOKIE}`, 'referer': 'https://m.weibo.cn' })
33
+ headers: lodash.merge(WeiboApi.WEIBO_HEADERS, {
34
+ 'Host': 'm.weibo.cn',
35
+ 'X-XSRF-TOKEN': `${X_XSRF_TOKEN}`,
36
+ 'Referer': `https://m.weibo.cn/u/${uid}`
37
+ })
40
38
  });
41
- // 处理 Set-Cookie 响应头
42
- if (resp.headers['set-cookie']) {
43
- await WeiboMainModels.updateCookieWithSetCookie(resp.headers['set-cookie'], mark);
39
+ // (3) 获取请求完成后的 Cookie 状态
40
+ const updatedCookies = await new Promise((resolve, reject) => {
41
+ jar.store.getAllCookies((err, cookies) => {
42
+ if (err)
43
+ reject(err);
44
+ else
45
+ resolve(cookies || []);
46
+ });
47
+ });
48
+ // 构建初始状态的 Map
49
+ const initialCookieMap = new Map(initialCookies.map(c => [`${c.key}:${c.domain}`, c]));
50
+ // 构建更新后的 Map
51
+ const updatedCookieMap = new Map(updatedCookies.map(c => [`${c.key}:${c.domain}`, c]));
52
+ // 比较两个 Map
53
+ for (const [key, updatedCookie] of updatedCookieMap.entries()) {
54
+ const initialCookie = initialCookieMap.get(key);
55
+ if (!initialCookie || initialCookie.value !== updatedCookie.value) {
56
+ console.log(`Weibo Cookie ${updatedCookie.key} was updated.`);
57
+ await WeiboCookieManager.saveCookiesToRedis(jar); // 更新同步到 Redis
58
+ }
44
59
  }
45
60
  return resp;
46
61
  }
47
62
  /**通过关键词搜索微博大v */
48
63
  async searchBloggerInfo(keyword) {
49
- const url = WeiboApi.WEIBO_API.weiboAjaxSearch;
50
- const { cookie, mark } = await WeiboMainModels.readSyncCookie();
51
- let COOKIE = '', XSRF_TOKEN = '';
52
- const params = {
53
- q: keyword
54
- };
55
- if (String(cookie).includes('XSRF-TOKEN')) {
56
- COOKIE = cookie;
57
- XSRF_TOKEN = await WeiboMainModels.readSavedCookieItems(cookie, ['XSRF-TOKEN'], false);
58
- }
59
- else {
60
- logger.warn(`优纪插件:搜索微博大V,未登录,如获取报错或失败请先 #扫码微博登录`);
61
- XSRF_TOKEN = generateRandomString();
62
- COOKIE = `XSRF_TOKEN=${XSRF_TOKEN}`;
63
- }
64
- const resp = await axios(url, {
64
+ const params = { containerid: '100103', type: '3', q: `${keyword}`, t: '', page_type: 'searchall' };
65
+ const url = new URL(WeiboApi.WEIBO_API.weiboGetIndex);
66
+ url.search = new URLSearchParams(params).toString();
67
+ const jar = await WeiboCookieManager.getSessionCookieJar();
68
+ const X_XSRF_TOKEN = await WeiboCookieManager.getCookieValueByKeyFromString(jar, 'X-XSRF-TOKEN', url.toString());
69
+ // (1) 保存初始状态
70
+ const initialCookies = await new Promise((resolve, reject) => {
71
+ jar.store.getAllCookies((err, cookies) => {
72
+ if (err)
73
+ reject(err);
74
+ else
75
+ resolve(cookies || []);
76
+ });
77
+ });
78
+ const resp = await axios(url.toString(), {
65
79
  method: 'GET',
66
- params,
80
+ jar,
81
+ withCredentials: true,
67
82
  timeout: 10000,
68
- headers: lodash.merge(WeiboApi.WEIBO_HEADERS, { 'X-XSRF-TOKEN': `${XSRF_TOKEN}`, 'Cookie': `${COOKIE}`, 'referer': 'https://m.weibo.cn' })
83
+ headers: lodash.merge(WeiboApi.WEIBO_HEADERS, {
84
+ 'X-XSRF-TOKEN': `${X_XSRF_TOKEN}`,
85
+ 'Referer': 'https://m.weibo.cn/search?'
86
+ })
69
87
  });
70
- // 处理 Set-Cookie 响应头
71
- if (resp.headers['set-cookie']) {
72
- await WeiboMainModels.updateCookieWithSetCookie(resp.headers['set-cookie'], mark);
88
+ // (3) 获取请求完成后的 Cookie 状态
89
+ const updatedCookies = await new Promise((resolve, reject) => {
90
+ jar.store.getAllCookies((err, cookies) => {
91
+ if (err)
92
+ reject(err);
93
+ else
94
+ resolve(cookies || []);
95
+ });
96
+ });
97
+ // 构建初始状态的 Map
98
+ const initialCookieMap = new Map(initialCookies.map(c => [`${c.key}:${c.domain}`, c]));
99
+ // 构建更新后的 Map
100
+ const updatedCookieMap = new Map(updatedCookies.map(c => [`${c.key}:${c.domain}`, c]));
101
+ // 比较两个 Map
102
+ for (const [key, updatedCookie] of updatedCookieMap.entries()) {
103
+ const initialCookie = initialCookieMap.get(key);
104
+ if (!initialCookie || initialCookie.value !== updatedCookie.value) {
105
+ console.log(`Weibo Cookie ${updatedCookie.key} was updated.`);
106
+ await WeiboCookieManager.saveCookiesToRedis(jar); // 更新同步到 Redis
107
+ }
73
108
  }
74
109
  return resp;
75
110
  }
76
111
  /**获取主页动态资源相关数组 */
77
- async getBloggerDynamicList(target) {
78
- const params = { containerid: '107603' + target };
112
+ async getBloggerDynamicList(uid) {
113
+ const params = { containerid: '107603' + uid };
79
114
  const url = new URL(WeiboApi.WEIBO_API.weiboGetIndex);
80
115
  url.search = new URLSearchParams(params).toString();
81
116
  await new Promise(resolve => setTimeout(resolve, Math.floor(Math.random() * (6500 - 1000 + 1) + 1000)));
82
- const { cookie, mark } = await WeiboMainModels.readSyncCookie();
83
- let COOKIE = '', XSRF_TOKEN = '';
84
- if (String(cookie).includes('XSRF-TOKEN')) {
85
- COOKIE = cookie;
86
- XSRF_TOKEN = await WeiboMainModels.readSavedCookieItems(cookie, ['XSRF-TOKEN'], false);
87
- }
88
- else {
89
- logger.warn(`优纪插件:获取主页动态资源,未登录,如获取报错或失败请先 #扫码微博登录`);
90
- XSRF_TOKEN = generateRandomString();
91
- COOKIE = `XSRF_TOKEN=${XSRF_TOKEN}`;
92
- }
117
+ const jar = await WeiboCookieManager.getSessionCookieJar();
118
+ const X_XSRF_TOKEN = await WeiboCookieManager.getCookieValueByKeyFromString(jar, 'X-XSRF-TOKEN', url.toString());
119
+ // (1) 保存初始状态
120
+ const initialCookies = await new Promise((resolve, reject) => {
121
+ jar.store.getAllCookies((err, cookies) => {
122
+ if (err)
123
+ reject(err);
124
+ else
125
+ resolve(cookies || []);
126
+ });
127
+ });
93
128
  try {
94
129
  const response = await axios(url.toString(), {
95
130
  method: 'GET',
131
+ jar,
132
+ withCredentials: true,
96
133
  timeout: 10000,
97
- headers: lodash.merge(WeiboApi.WEIBO_HEADERS, { 'X-XSRF-TOKEN': `${XSRF_TOKEN}`, 'Cookie': `${COOKIE}`, 'referer': 'https://m.weibo.cn' })
134
+ headers: lodash.merge(WeiboApi.WEIBO_HEADERS, {
135
+ 'Host': 'm.weibo.cn',
136
+ 'X-XSRF-TOKEN': `${X_XSRF_TOKEN}`,
137
+ 'referer': `https://m.weibo.cn/u/${uid}`
138
+ })
98
139
  });
99
- // 处理 Set-Cookie 响应头
100
- if (response.headers['set-cookie']) {
101
- await WeiboMainModels.updateCookieWithSetCookie(response.headers['set-cookie'], mark);
102
- }
103
140
  const { ok, data, msg } = response?.data;
104
141
  if (!ok && msg !== '这里还没有内容') {
105
142
  throw new Error(response?.config.url);
106
143
  }
144
+ // (3) 获取请求完成后的 Cookie 状态
145
+ const updatedCookies = await new Promise((resolve, reject) => {
146
+ jar.store.getAllCookies((err, cookies) => {
147
+ if (err)
148
+ reject(err);
149
+ else
150
+ resolve(cookies || []);
151
+ });
152
+ });
153
+ // 构建初始状态的 Map
154
+ const initialCookieMap = new Map(initialCookies.map(c => [`${c.key}:${c.domain}`, c]));
155
+ // 构建更新后的 Map
156
+ const updatedCookieMap = new Map(updatedCookies.map(c => [`${c.key}:${c.domain}`, c]));
157
+ // 比较两个 Map
158
+ for (const [key, updatedCookie] of updatedCookieMap.entries()) {
159
+ const initialCookie = initialCookieMap.get(key);
160
+ if (!initialCookie || initialCookie.value !== updatedCookie.value) {
161
+ console.log(`Weibo Cookie ${updatedCookie.key} was updated.`);
162
+ await WeiboCookieManager.saveCookiesToRedis(jar); // 更新同步到 Redis
163
+ }
164
+ }
107
165
  return data.cards.filter(WeiboQuery.filterCardTypeCustom);
108
166
  }
109
167
  catch (error) {
110
- global?.logger?.mark('微博推送:Error fetching sub list:', error);
168
+ logger?.mark('微博推送:Error fetching sub list:', error);
111
169
  return [];
112
170
  }
113
171
  }