-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
313 lines (273 loc) · 8.82 KB
/
Copy pathserver.js
File metadata and controls
313 lines (273 loc) · 8.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/**
* Get笔记 API 服务
* 将原 Tampermonkey 脚本包装成 HTTP API
*/
const express = require('express');
const cors = require('cors');
const { extractVideoInfo } = require('./videoExtractor');
const { GetBijiClient } = require('./getbijiService');
const { getAvailableVersions } = require('./prompts');
const app = express();
const PORT = process.env.PORT || 3000;
// 中间件 - 配置 CORS 允许所有需要的请求头
app.use(cors({
origin: '*',
methods: ['GET', 'POST', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization', 'X-Request-Id', 'xi-csrf-token']
}));
app.use(express.json());
// 静态文件服务 - 前端页面
app.use(express.static('public'));
// 请求日志
app.use((req, res, next) => {
console.log(`[${new Date().toISOString()}] ${req.method} ${req.path}`);
next();
});
/**
* GET / - 服务信息
*/
app.get('/', (req, res) => {
res.json({
name: 'Get笔记 API 服务',
version: '1.0.0',
description: '将B站/YouTube视频链接通过API发送到Get笔记进行AI总结',
endpoints: {
'POST /api/summarize': '提交视频链接进行总结',
'GET /api/templates': '获取可用的提示词模板列表',
'GET /api/health': '健康检查',
'POST /api/validate-auth': '验证Get笔记认证信息'
}
});
});
/**
* GET /api/health - 健康检查
*/
app.get('/api/health', (req, res) => {
res.json({
status: 'ok',
timestamp: new Date().toISOString()
});
});
/**
* GET /api/templates - 获取提示词模板列表
*/
app.get('/api/templates', (req, res) => {
res.json({
success: true,
templates: getAvailableVersions()
});
});
/**
* POST /api/validate-auth - 验证Get笔记认证
*/
app.post('/api/validate-auth', async (req, res) => {
try {
const { token, cookie } = req.body;
const client = new GetBijiClient({ token, cookie });
const isValid = await client.validateAuth();
res.json({
success: true,
valid: isValid,
message: isValid ? '认证信息有效' : '认证信息无效或缺失'
});
} catch (error) {
res.status(500).json({
success: false,
error: error.message
});
}
});
/**
* POST /api/summarize - 提交视频链接进行总结
*
* 请求体:
* {
* "url": "https://www.bilibili.com/video/BVxxxxx", // 必填:视频链接
* "token": "your-getbiji-token", // 可选:Get笔记token
* "cookie": "your-getbiji-cookie", // 可选:Get笔记cookie
* "template": "detailed", // 可选:提示词模板版本
* "customPrompt": "自定义提示词..." // 可选:自定义提示词
* }
*
* 响应:
* {
* "success": true,
* "noteId": "xxx",
* "noteUrl": "https://www.biji.com/note/xxx",
* "videoInfo": {
* "url": "...",
* "title": "...",
* "platform": "Bilibili"
* },
* "promptVersion": "detailed"
* }
*/
app.post('/api/summarize', async (req, res) => {
try {
const { url, token, cookie, template = 'detailed', customPrompt } = req.body;
// 参数验证
if (!url) {
return res.status(400).json({
success: false,
error: '缺少必填参数: url'
});
}
// 检查环境变量或请求参数中是否有认证信息
const authToken = token || process.env.GETBIJI_TOKEN;
const authCookie = cookie || process.env.GETBIJI_COOKIE;
if (!authToken && !authCookie) {
return res.status(401).json({
success: false,
error: '缺少认证信息:请提供Get笔记的token或cookie',
hint: '可以通过以下方式提供:1) 请求体中传入token/cookie 2) 设置环境变量GETBIJI_TOKEN/GETBIJI_COOKIE'
});
}
// 步骤1: 提取视频信息
console.log(`[INFO] 正在提取视频信息: ${url}`);
const videoInfo = await extractVideoInfo(url);
if (!videoInfo.success && videoInfo.platform === 'unknown') {
return res.status(400).json({
success: false,
error: videoInfo.error,
videoInfo
});
}
console.log(`[INFO] 视频信息提取完成: ${videoInfo.platform} - ${videoInfo.title}`);
// 步骤2: 创建Get笔记客户端
const client = new GetBijiClient({
token: authToken,
cookie: authCookie
});
// 步骤3: 调用Get笔记API创建笔记
console.log(`[INFO] 正在创建Get笔记,使用模板: ${template}`);
const result = await client.createNote(videoInfo, {
promptVersion: template,
customPrompt
});
console.log(`[INFO] 笔记创建成功: ${result.noteId}`);
// 返回成功响应
res.json(result);
} catch (error) {
console.error(`[ERROR] ${error.message}`);
// 根据错误类型返回不同状态码
let statusCode = 500;
if (error.message.includes('认证失败')) {
statusCode = 401;
} else if (error.message.includes('不支持的视频平台')) {
statusCode = 400;
} else if (error.message.includes('请求过于频繁')) {
statusCode = 429;
}
res.status(statusCode).json({
success: false,
error: error.message
});
}
});
/**
* POST /api/summarize/batch - 批量提交视频链接
*
* 请求体:
* {
* "urls": ["url1", "url2", ...],
* "token": "...",
* "cookie": "...",
* "template": "detailed"
* }
*/
app.post('/api/summarize/batch', async (req, res) => {
try {
const { urls, token, cookie, template = 'detailed' } = req.body;
if (!urls || !Array.isArray(urls) || urls.length === 0) {
return res.status(400).json({
success: false,
error: '缺少必填参数: urls (必须是数组)'
});
}
const authToken = token || process.env.GETBIJI_TOKEN;
const authCookie = cookie || process.env.GETBIJI_COOKIE;
if (!authToken && !authCookie) {
return res.status(401).json({
success: false,
error: '缺少认证信息:请提供Get笔记的token或cookie'
});
}
const results = [];
const client = new GetBijiClient({ token: authToken, cookie: authCookie });
// 串行处理每个URL(避免触发频率限制)
for (const url of urls) {
try {
const videoInfo = await extractVideoInfo(url);
if (!videoInfo.success && videoInfo.platform === 'unknown') {
results.push({
url,
success: false,
error: videoInfo.error
});
continue;
}
const result = await client.createNote(videoInfo, { promptVersion: template });
results.push(result);
// 添加延迟避免频率限制
if (urls.length > 1) {
await new Promise(resolve => setTimeout(resolve, 1000));
}
} catch (error) {
results.push({
url,
success: false,
error: error.message
});
}
}
res.json({
success: true,
total: urls.length,
succeeded: results.filter(r => r.success).length,
failed: results.filter(r => !r.success).length,
results
});
} catch (error) {
console.error(`[ERROR] ${error.message}`);
res.status(500).json({
success: false,
error: error.message
});
}
});
// 404 处理
app.use((req, res) => {
res.status(404).json({
success: false,
error: '接口不存在',
path: req.path
});
});
// 错误处理
app.use((err, req, res, next) => {
console.error(`[ERROR] ${err.stack}`);
res.status(500).json({
success: false,
error: '服务器内部错误',
message: err.message
});
});
// 启动服务器
app.listen(PORT, () => {
console.log(`
╔════════════════════════════════════════════════════════╗
║ Get笔记 API 服务已启动 ║
╠════════════════════════════════════════════════════════╣
║ 端口: ${PORT.toString().padEnd(45)} ║
║ 地址: http://localhost:${PORT.toString().padEnd(37)} ║
╠════════════════════════════════════════════════════════╣
║ 可用接口: ║
║ POST /api/summarize - 提交视频链接进行总结 ║
║ POST /api/summarize/batch - 批量提交视频链接 ║
║ GET /api/templates - 获取提示词模板列表 ║
║ POST /api/validate-auth - 验证认证信息 ║
║ GET /api/health - 健康检查 ║
╚════════════════════════════════════════════════════════╝
`);
});
module.exports = app;