@ikenxuan/amagi 1.5.5 → 1.5.6
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.
- package/lib/business/bilibili/getdata.js +3 -25
- package/lib/business/bilibili/getid.js +3 -3
- package/lib/business/bilibili/result.js +1 -1
- package/lib/business/bilibili/sign/wbi.js +5 -5
- package/lib/business/douyin/getdata.js +14 -14
- package/lib/business/douyin/getid.js +3 -3
- package/lib/business/douyin/result.js +1 -1
- package/lib/business/douyin/sign/a_bogus.js +32 -31
- package/lib/index.d.ts +5 -5
- package/lib/index.js +5 -5
- package/lib/model/logger.js +15 -15
- package/lib/model/networks.d.ts +30 -30
- package/lib/model/networks.js +211 -209
- package/lib/server/client.d.ts +6 -6
- package/lib/server/client.js +97 -111
- package/lib/server/listen.js +5 -5
- package/package.json +9 -10
- package/amagi.openapi.json +0 -1
package/lib/model/networks.js
CHANGED
|
@@ -2,227 +2,229 @@ import fetch, { Response } from 'node-fetch';
|
|
|
2
2
|
import { logger } from '../model/index.js';
|
|
3
3
|
const controller = new AbortController();
|
|
4
4
|
export default class Networks {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
23
|
-
else
|
|
24
|
-
this.Headers = {};
|
|
25
|
-
this.url = data.url || '';
|
|
26
|
-
this.type = data.type || 'json';
|
|
27
|
-
this.method = data.method || 'GET';
|
|
28
|
-
this.body = data.body || ''; // 用于POST请求
|
|
29
|
-
this.data = {};
|
|
30
|
-
this.timeout = data.timeout || 15000;
|
|
31
|
-
this.isGetResult = false;
|
|
32
|
-
this.timer = undefined;
|
|
33
|
-
this.redirect = 'follow'; // 默认跟随重定向
|
|
34
|
-
}
|
|
35
|
-
get config() {
|
|
36
|
-
let data = {
|
|
37
|
-
headers: this.Headers,
|
|
38
|
-
method: this.method,
|
|
39
|
-
};
|
|
40
|
-
if (this.method === 'POST') {
|
|
41
|
-
data = { ...data, body: JSON.stringify(this.body) || '' };
|
|
42
|
-
}
|
|
43
|
-
return data;
|
|
44
|
-
}
|
|
45
|
-
async getfetch() {
|
|
46
|
-
try {
|
|
47
|
-
const result = await this.returnResult();
|
|
48
|
-
if (result.status === 504) {
|
|
49
|
-
return result;
|
|
50
|
-
}
|
|
51
|
-
this.isGetResult = true;
|
|
52
|
-
return result;
|
|
53
|
-
}
|
|
54
|
-
catch (error) {
|
|
55
|
-
logger.info(error);
|
|
56
|
-
return false;
|
|
57
|
-
}
|
|
5
|
+
url;
|
|
6
|
+
method;
|
|
7
|
+
Headers;
|
|
8
|
+
type;
|
|
9
|
+
body;
|
|
10
|
+
fetch;
|
|
11
|
+
isGetResult;
|
|
12
|
+
timeout;
|
|
13
|
+
timer;
|
|
14
|
+
data;
|
|
15
|
+
redirect;
|
|
16
|
+
constructor(data) {
|
|
17
|
+
this.Headers = new Headers();
|
|
18
|
+
if (data.headers && Object.keys(data.headers).length > 0) {
|
|
19
|
+
for (const [key, value] of Object.entries(data.headers)) {
|
|
20
|
+
this.Headers.append(key, value);
|
|
21
|
+
}
|
|
58
22
|
}
|
|
59
|
-
|
|
60
|
-
|
|
23
|
+
else
|
|
24
|
+
this.Headers = {};
|
|
25
|
+
this.url = data.url || '';
|
|
26
|
+
this.type = data.type || 'json';
|
|
27
|
+
this.method = data.method || 'GET';
|
|
28
|
+
this.body = data.body || ''; // 用于POST请求
|
|
29
|
+
this.data = {};
|
|
30
|
+
this.timeout = data.timeout || 15000;
|
|
31
|
+
this.isGetResult = false;
|
|
32
|
+
this.timer = undefined;
|
|
33
|
+
this.redirect = 'follow'; // 默认跟随重定向
|
|
34
|
+
}
|
|
35
|
+
get config() {
|
|
36
|
+
let data = {
|
|
37
|
+
headers: this.Headers,
|
|
38
|
+
method: this.method
|
|
39
|
+
};
|
|
40
|
+
if (this.method === 'POST') {
|
|
41
|
+
data = { ...data, body: JSON.stringify(this.body) || '' };
|
|
61
42
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
43
|
+
return data;
|
|
44
|
+
}
|
|
45
|
+
async getfetch() {
|
|
46
|
+
try {
|
|
47
|
+
const result = await this.returnResult();
|
|
48
|
+
if (result.status === 504) {
|
|
49
|
+
return result;
|
|
50
|
+
}
|
|
51
|
+
this.isGetResult = true;
|
|
52
|
+
return result;
|
|
72
53
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
const response = await fetch(this.url, {
|
|
77
|
-
method: 'GET',
|
|
78
|
-
redirect: 'manual', // 不跟随重定向
|
|
79
|
-
});
|
|
80
|
-
// 取location返回
|
|
81
|
-
return response.headers.get('location');
|
|
82
|
-
}
|
|
83
|
-
catch (error) {
|
|
84
|
-
logger.error(error);
|
|
85
|
-
return '';
|
|
86
|
-
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
logger.info(error);
|
|
56
|
+
return false;
|
|
87
57
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
logger.warn('HTTP 响应状态码: 429');
|
|
98
|
-
throw new Error('ratelimit triggered, 触发 https://www.douyin.com/ 的速率限制!!!');
|
|
99
|
-
}
|
|
100
|
-
this.fetch = result;
|
|
101
|
-
this.isGetResult = true;
|
|
102
|
-
}
|
|
103
|
-
else {
|
|
104
|
-
this.fetch = new_fetch;
|
|
105
|
-
}
|
|
106
|
-
switch (this.type) {
|
|
107
|
-
case 'json':
|
|
108
|
-
await this.Tojson();
|
|
109
|
-
break;
|
|
110
|
-
case 'text':
|
|
111
|
-
await this.ToText();
|
|
112
|
-
break;
|
|
113
|
-
case 'arrayBuffer':
|
|
114
|
-
await this.ToArrayBuffer();
|
|
115
|
-
break;
|
|
116
|
-
case 'blob':
|
|
117
|
-
await this.ToBlob();
|
|
118
|
-
break;
|
|
119
|
-
}
|
|
120
|
-
return this.fetch;
|
|
121
|
-
}
|
|
122
|
-
catch (error) {
|
|
123
|
-
logger.error(error);
|
|
124
|
-
return false;
|
|
125
|
-
}
|
|
58
|
+
}
|
|
59
|
+
async returnResult() {
|
|
60
|
+
return await fetch(this.url, this.config);
|
|
61
|
+
}
|
|
62
|
+
/** 最终地址(跟随重定向) */
|
|
63
|
+
async getLongLink() {
|
|
64
|
+
try {
|
|
65
|
+
const result = await this.returnResult();
|
|
66
|
+
return result.url;
|
|
126
67
|
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
this.fetch = await this.returnResult();
|
|
131
|
-
if (this.fetch) {
|
|
132
|
-
if (this.fetch.headers) {
|
|
133
|
-
const headers = this.fetch.headers;
|
|
134
|
-
const headersObject = {};
|
|
135
|
-
for (const [key, value] of headers.entries()) {
|
|
136
|
-
headersObject[key] = value;
|
|
137
|
-
}
|
|
138
|
-
return headersObject;
|
|
139
|
-
}
|
|
140
|
-
else {
|
|
141
|
-
logger.error('未获取到响应头');
|
|
142
|
-
return null;
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
else {
|
|
146
|
-
logger.error('未获取到响应对象');
|
|
147
|
-
return null;
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
catch (error) {
|
|
151
|
-
logger.error('获取响应头失败:', error);
|
|
152
|
-
return null;
|
|
153
|
-
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
logger.error(error);
|
|
70
|
+
return '';
|
|
154
71
|
}
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
72
|
+
}
|
|
73
|
+
/** 获取首个302 */
|
|
74
|
+
async getLocation() {
|
|
75
|
+
try {
|
|
76
|
+
const response = await fetch(this.url, {
|
|
77
|
+
method: 'GET',
|
|
78
|
+
redirect: 'manual' // 不跟随重定向
|
|
79
|
+
});
|
|
80
|
+
// 取location返回
|
|
81
|
+
return response.headers.get('location');
|
|
82
|
+
}
|
|
83
|
+
catch (error) {
|
|
84
|
+
logger.error(error);
|
|
85
|
+
return '';
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
/** 获取数据并处理数据的格式化,默认json */
|
|
89
|
+
async getData(new_fetch = '') {
|
|
90
|
+
try {
|
|
91
|
+
if (!new_fetch) {
|
|
92
|
+
const result = await this.returnResult();
|
|
93
|
+
if (result.status === 504) {
|
|
94
|
+
return result;
|
|
95
|
+
}
|
|
96
|
+
if (result.status === 429) {
|
|
97
|
+
logger.warn('HTTP 响应状态码: 429');
|
|
98
|
+
throw new Error('ratelimit triggered, 触发 https://www.douyin.com/ 的速率限制!!!');
|
|
99
|
+
}
|
|
100
|
+
this.fetch = result;
|
|
101
|
+
this.isGetResult = true;
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
this.fetch = new_fetch;
|
|
105
|
+
}
|
|
106
|
+
switch (this.type) {
|
|
107
|
+
case 'json':
|
|
108
|
+
await this.Tojson();
|
|
109
|
+
break;
|
|
110
|
+
case 'text':
|
|
111
|
+
await this.ToText();
|
|
112
|
+
break;
|
|
113
|
+
case 'arrayBuffer':
|
|
114
|
+
await this.ToArrayBuffer();
|
|
115
|
+
break;
|
|
116
|
+
case 'blob':
|
|
117
|
+
await this.ToBlob();
|
|
118
|
+
break;
|
|
119
|
+
default:
|
|
120
|
+
}
|
|
121
|
+
return this.fetch;
|
|
200
122
|
}
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
123
|
+
catch (error) {
|
|
124
|
+
logger.error(error);
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
/** 获取响应头 */
|
|
129
|
+
async getHeaders() {
|
|
130
|
+
try {
|
|
131
|
+
this.fetch = await this.returnResult();
|
|
132
|
+
if (this.fetch) {
|
|
133
|
+
if (this.fetch.headers) {
|
|
134
|
+
const headers = this.fetch.headers;
|
|
135
|
+
const headersObject = {};
|
|
136
|
+
for (const [key, value] of headers.entries()) {
|
|
137
|
+
headersObject[key] = value;
|
|
138
|
+
}
|
|
139
|
+
return headersObject;
|
|
204
140
|
}
|
|
205
141
|
else {
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
}
|
|
142
|
+
logger.error('未获取到响应头');
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
else {
|
|
147
|
+
logger.error('未获取到响应对象');
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
catch (error) {
|
|
152
|
+
logger.error('获取响应头失败:', error);
|
|
153
|
+
return null;
|
|
209
154
|
}
|
|
210
|
-
|
|
211
|
-
|
|
155
|
+
}
|
|
156
|
+
/** 一次性获取响应头和响应体 */
|
|
157
|
+
async getHeadersAndData() {
|
|
158
|
+
try {
|
|
159
|
+
// 发起网络请求获取响应对象
|
|
160
|
+
this.fetch = await this.returnResult();
|
|
161
|
+
// 初始化响应头和响应数据
|
|
162
|
+
let headers = null;
|
|
163
|
+
let data = null;
|
|
164
|
+
if (this.fetch) {
|
|
165
|
+
// 获取响应头
|
|
166
|
+
if (this.fetch.headers) {
|
|
167
|
+
headers = {};
|
|
168
|
+
const fetchHeaders = this.fetch.headers;
|
|
169
|
+
for (const [key, value] of fetchHeaders.entries()) {
|
|
170
|
+
headers[key] = value;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
else {
|
|
174
|
+
logger.error('未获取到响应头');
|
|
175
|
+
}
|
|
176
|
+
// 获取响应数据
|
|
177
|
+
switch (this.type) {
|
|
178
|
+
case 'json':
|
|
179
|
+
data = await this.fetch.json();
|
|
180
|
+
break;
|
|
181
|
+
case 'text':
|
|
182
|
+
data = await this.fetch.text();
|
|
183
|
+
break;
|
|
184
|
+
case 'arrayBuffer':
|
|
185
|
+
data = await this.ToArrayBuffer();
|
|
186
|
+
break;
|
|
187
|
+
case 'blob':
|
|
188
|
+
data = await this.ToBlob();
|
|
189
|
+
break;
|
|
190
|
+
default:
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
else {
|
|
194
|
+
logger.error('未获取到响应对象');
|
|
195
|
+
}
|
|
196
|
+
return { headers, data };
|
|
212
197
|
}
|
|
213
|
-
|
|
214
|
-
|
|
198
|
+
catch (error) {
|
|
199
|
+
logger.error('获取响应头和数据失败:', error);
|
|
200
|
+
return { headers: null, data: null };
|
|
215
201
|
}
|
|
216
|
-
|
|
217
|
-
|
|
202
|
+
}
|
|
203
|
+
async Tojson() {
|
|
204
|
+
if (this.fetch.headers.get('content-type').includes('json')) {
|
|
205
|
+
this.fetch = await this.fetch.json();
|
|
218
206
|
}
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
logger.info('执行力');
|
|
223
|
-
controller.abort();
|
|
224
|
-
resolve(new Response('timeout', { status: 504, statusText: 'timeout' }));
|
|
225
|
-
}, timeout);
|
|
226
|
-
});
|
|
207
|
+
else {
|
|
208
|
+
this.fetch = await this.fetch.text();
|
|
209
|
+
this.type = 'text';
|
|
227
210
|
}
|
|
211
|
+
}
|
|
212
|
+
async ToText() {
|
|
213
|
+
this.fetch = await this.fetch.text();
|
|
214
|
+
}
|
|
215
|
+
async ToArrayBuffer() {
|
|
216
|
+
this.fetch = await this.fetch.arrayBuffer();
|
|
217
|
+
}
|
|
218
|
+
async ToBlob() {
|
|
219
|
+
this.fetch = await this.fetch.blob();
|
|
220
|
+
}
|
|
221
|
+
timeoutPromise(timeout) {
|
|
222
|
+
return new Promise((resolve, reject) => {
|
|
223
|
+
this.timer = setTimeout(() => {
|
|
224
|
+
logger.info('执行力');
|
|
225
|
+
controller.abort();
|
|
226
|
+
resolve(new Response('timeout', { status: 504, statusText: 'timeout' }));
|
|
227
|
+
}, timeout);
|
|
228
|
+
});
|
|
229
|
+
}
|
|
228
230
|
}
|
package/lib/server/client.d.ts
CHANGED
|
@@ -6,18 +6,18 @@ interface initClientParams {
|
|
|
6
6
|
bilibili: string;
|
|
7
7
|
}
|
|
8
8
|
export declare class client {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
private readonly douyin;
|
|
10
|
+
private readonly bilibili;
|
|
11
|
+
/**
|
|
12
12
|
*
|
|
13
13
|
* @param cookies 包含抖音和B站cookie的参数对象
|
|
14
14
|
*/
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
constructor(cookies: initClientParams);
|
|
16
|
+
/**
|
|
17
17
|
* 初始化 fastify 实例
|
|
18
18
|
* @param log 是否启用日志
|
|
19
19
|
* @returns fastify 实例
|
|
20
20
|
*/
|
|
21
|
-
|
|
21
|
+
initServer(log?: boolean): Promise<FastifyInstance>;
|
|
22
22
|
}
|
|
23
23
|
export {};
|