Skip to content

Commit 55a39dc

Browse files
committed
update:增加解析功能
1 parent 424bfc2 commit 55a39dc

File tree

9 files changed

+400
-179
lines changed

9 files changed

+400
-179
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ nodejs作为服务端的drpy实现。全面升级异步写法
1717
更新至V1.0.11
1818

1919
1. 支持vercel部署
20+
2. 增加自定义解析功能
2021

2122
[点此查看完整更新记录](docs/updateRecord.md)
2223

controllers/api.js

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export default (fastify, options, done) => {
106106
const proxyUrl = `${protocol}://${hostname}${request.url}`.split('?')[0].replace(proxyPath, '') + '?do=js';
107107
// console.log(`proxyUrl:${proxyUrl}`);
108108
const env = {
109-
proxyUrl,proxyPath, getProxyUrl: function () {
109+
proxyUrl, proxyPath, getProxyUrl: function () {
110110
return proxyUrl
111111
},
112112
};
@@ -161,5 +161,51 @@ export default (fastify, options, done) => {
161161
reply.status(500).send({error: `Failed to proxy module ${moduleName}: ${error.message}`});
162162
}
163163
});
164+
165+
166+
fastify.get('/parse/:jx', async (request, reply) => {
167+
let t1 = (new Date()).getTime();
168+
const jxName = request.params.jx;
169+
const query = request.query; // 获取 query 参数
170+
const jxPath = path.join(options.jxDir, `${jxName}.js`);
171+
if (!existsSync(jxPath)) {
172+
return reply.status(404).send({error: `解析 ${jxName} not found`});
173+
}
174+
const protocol = request.protocol;
175+
const hostname = request.hostname;
176+
const proxyUrl = `${protocol}://${hostname}${request.url}`.split('?')[0].replace('/parse/', '/proxy/') + '/?do=js';
177+
const env = {
178+
proxyUrl, getProxyUrl: function () {
179+
return proxyUrl
180+
}
181+
};
182+
try {
183+
const backResp = await drpy.jx(jxPath, env, query);
184+
const statusCode = 200;
185+
const mediaType = 'application/json; charset=utf-8';
186+
if (typeof backResp === 'object') {
187+
let t2 = (new Date()).getTime();
188+
backResp.cost = t2 - t1;
189+
return reply.code(statusCode).type(`${mediaType}; charset=utf-8`).send(JSON.stringify(backResp));
190+
} else if (typeof backResp === 'string') {
191+
let statusCode = backResp && backResp !== query.url ? 200 : 404;
192+
let msgState = backResp && backResp !== query.url ? '成功' : '失败';
193+
let t2 = (new Date()).getTime();
194+
let result = {
195+
code: statusCode,
196+
url: backResp,
197+
msg: `${jxName}解析${msgState}`,
198+
cost: t2 - t1
199+
}
200+
return reply.code(statusCode).type(`${mediaType}; charset=utf-8`).send(JSON.stringify(result));
201+
} else {
202+
return reply.status(404).send({error: `${jxName}解析失败`});
203+
}
204+
205+
} catch (error) {
206+
fastify.log.error(`Error proxy jx ${jxName}:${error.message}`);
207+
reply.status(500).send({error: `Failed to proxy jx ${jxName}: ${error.message}`});
208+
}
209+
});
164210
done();
165211
};

controllers/config.js

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,49 @@ function generateSiteJSON(jsDir, requestHost) {
2525
return {sites};
2626
}
2727

28+
function generateParseJSON(jxDir, requestHost) {
29+
const files = readdirSync(jxDir);
30+
const parses = files
31+
.filter((file) => file.endsWith('.js') && !file.startsWith('_')) // 筛选出不是 "_" 开头的 .js 文件
32+
.map((file) => {
33+
const baseName = path.basename(file, '.js'); // 去掉文件扩展名
34+
const api = `${requestHost}/parse/${baseName}?url=`; // 使用请求的 host 地址,避免硬编码端口
35+
return {
36+
name: baseName,
37+
url: api,
38+
type: 1,
39+
ext: {
40+
flag: [
41+
"qiyi",
42+
"imgo",
43+
"爱奇艺",
44+
"奇艺",
45+
"qq",
46+
"qq 预告及花絮",
47+
"腾讯",
48+
"youku",
49+
"优酷",
50+
"pptv",
51+
"PPTV",
52+
"letv",
53+
"乐视",
54+
"leshi",
55+
"mgtv",
56+
"芒果",
57+
"sohu",
58+
"xigua",
59+
"fun",
60+
"风行"
61+
]
62+
},
63+
header: {
64+
"User-Agent": "Mozilla/5.0"
65+
}
66+
}
67+
});
68+
return {parses};
69+
}
70+
2871
export default (fastify, options, done) => {
2972

3073
fastify.get('/index', async (request, reply) => {
@@ -49,14 +92,16 @@ export default (fastify, options, done) => {
4992
console.log('port:', port);
5093
let requestHost = cfg_path === '/1' ? `${protocol}://${hostname}` : `http://127.0.0.1:${options.PORT}`; // 动态生成根地址
5194
const siteJSON = generateSiteJSON(options.jsDir, requestHost);
52-
const siteStr = JSON.stringify(siteJSON, null, 2);
95+
const parseJSON = generateParseJSON(options.jxDir, requestHost);
96+
const configObj = {...siteJSON, ...parseJSON};
97+
const configStr = JSON.stringify(configObj, null, 2);
5398
if (!process.env.VERCEL) { // Vercel 环境不支持写文件,关闭此功能
54-
writeFileSync(options.indexFilePath, siteStr, 'utf8'); // 写入 index.json
99+
writeFileSync(options.indexFilePath, configStr, 'utf8'); // 写入 index.json
55100
if (cfg_path === '/1') {
56-
writeFileSync(options.customFilePath, siteStr, 'utf8'); // 写入 index.json
101+
writeFileSync(options.customFilePath, configStr, 'utf8'); // 写入 index.json
57102
}
58103
}
59-
reply.send(siteJSON);
104+
reply.send(configObj);
60105
} catch (error) {
61106
reply.status(500).send({error: 'Failed to generate site JSON', details: error.message});
62107
}

custom.json

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"key": "drpyS_PTT[优]",
55
"name": "PTT[优](DS)",
66
"type": 4,
7-
"api": "http://127.0.0.1:5757/api/PTT[优]",
7+
"api": "http://localhost:5757/api/PTT[优]",
88
"searchable": 1,
99
"filterable": 1,
1010
"quickSearch": 0,
@@ -14,7 +14,7 @@
1414
"key": "drpyS_xvideos[密]",
1515
"name": "xvideos[密](DS)",
1616
"type": 4,
17-
"api": "http://127.0.0.1:5757/api/xvideos[密]",
17+
"api": "http://localhost:5757/api/xvideos[密]",
1818
"searchable": 1,
1919
"filterable": 1,
2020
"quickSearch": 0,
@@ -24,7 +24,7 @@
2424
"key": "drpyS_人人视频",
2525
"name": "人人视频(DS)",
2626
"type": 4,
27-
"api": "http://127.0.0.1:5757/api/人人视频",
27+
"api": "http://localhost:5757/api/人人视频",
2828
"searchable": 1,
2929
"filterable": 1,
3030
"quickSearch": 0,
@@ -34,7 +34,7 @@
3434
"key": "drpyS_动漫巴士[漫]",
3535
"name": "动漫巴士[漫](DS)",
3636
"type": 4,
37-
"api": "http://127.0.0.1:5757/api/动漫巴士[漫]",
37+
"api": "http://localhost:5757/api/动漫巴士[漫]",
3838
"searchable": 1,
3939
"filterable": 1,
4040
"quickSearch": 0,
@@ -44,7 +44,7 @@
4444
"key": "drpyS_央视大全[官]",
4545
"name": "央视大全[官](DS)",
4646
"type": 4,
47-
"api": "http://127.0.0.1:5757/api/央视大全[官]",
47+
"api": "http://localhost:5757/api/央视大全[官]",
4848
"searchable": 1,
4949
"filterable": 1,
5050
"quickSearch": 0,
@@ -54,7 +54,7 @@
5454
"key": "drpyS_星芽短剧",
5555
"name": "星芽短剧(DS)",
5656
"type": 4,
57-
"api": "http://127.0.0.1:5757/api/星芽短剧",
57+
"api": "http://localhost:5757/api/星芽短剧",
5858
"searchable": 1,
5959
"filterable": 1,
6060
"quickSearch": 0,
@@ -64,7 +64,7 @@
6464
"key": "drpyS_番茄小说[书]",
6565
"name": "番茄小说[书](DS)",
6666
"type": 4,
67-
"api": "http://127.0.0.1:5757/api/番茄小说[书]",
67+
"api": "http://localhost:5757/api/番茄小说[书]",
6868
"searchable": 1,
6969
"filterable": 1,
7070
"quickSearch": 0,
@@ -74,7 +74,7 @@
7474
"key": "drpyS_老白故事[听]",
7575
"name": "老白故事[听](DS)",
7676
"type": 4,
77-
"api": "http://127.0.0.1:5757/api/老白故事[听]",
77+
"api": "http://localhost:5757/api/老白故事[听]",
7878
"searchable": 1,
7979
"filterable": 1,
8080
"quickSearch": 0,
@@ -84,7 +84,7 @@
8484
"key": "drpyS_腾云驾雾[官]",
8585
"name": "腾云驾雾[官](DS)",
8686
"type": 4,
87-
"api": "http://127.0.0.1:5757/api/腾云驾雾[官]",
87+
"api": "http://localhost:5757/api/腾云驾雾[官]",
8888
"searchable": 1,
8989
"filterable": 1,
9090
"quickSearch": 0,
@@ -94,7 +94,7 @@
9494
"key": "drpyS_色花堂[密+]",
9595
"name": "色花堂[密+](DS)",
9696
"type": 4,
97-
"api": "http://127.0.0.1:5757/api/色花堂[密+]",
97+
"api": "http://localhost:5757/api/色花堂[密+]",
9898
"searchable": 1,
9999
"filterable": 1,
100100
"quickSearch": 0,
@@ -104,7 +104,7 @@
104104
"key": "drpyS_色花堂[密]",
105105
"name": "色花堂[密](DS)",
106106
"type": 4,
107-
"api": "http://127.0.0.1:5757/api/色花堂[密]",
107+
"api": "http://localhost:5757/api/色花堂[密]",
108108
"searchable": 1,
109109
"filterable": 1,
110110
"quickSearch": 0,
@@ -114,7 +114,7 @@
114114
"key": "drpyS_草榴社区[密]",
115115
"name": "草榴社区[密](DS)",
116116
"type": 4,
117-
"api": "http://127.0.0.1:5757/api/草榴社区[密]",
117+
"api": "http://localhost:5757/api/草榴社区[密]",
118118
"searchable": 1,
119119
"filterable": 1,
120120
"quickSearch": 0,
@@ -124,7 +124,7 @@
124124
"key": "drpyS_金牌影院",
125125
"name": "金牌影院(DS)",
126126
"type": 4,
127-
"api": "http://127.0.0.1:5757/api/金牌影院",
127+
"api": "http://localhost:5757/api/金牌影院",
128128
"searchable": 1,
129129
"filterable": 1,
130130
"quickSearch": 0,
@@ -134,11 +134,45 @@
134134
"key": "drpyS_黑料不打烊[密]",
135135
"name": "黑料不打烊[密](DS)",
136136
"type": 4,
137-
"api": "http://127.0.0.1:5757/api/黑料不打烊[密]",
137+
"api": "http://localhost:5757/api/黑料不打烊[密]",
138138
"searchable": 1,
139139
"filterable": 1,
140140
"quickSearch": 0,
141141
"ext": ""
142142
}
143+
],
144+
"parses": [
145+
{
146+
"name": "虾米",
147+
"url": "http://localhost:5757/parse/虾米?url=",
148+
"type": 1,
149+
"ext": {
150+
"flag": [
151+
"qiyi",
152+
"imgo",
153+
"爱奇艺",
154+
"奇艺",
155+
"qq",
156+
"qq 预告及花絮",
157+
"腾讯",
158+
"youku",
159+
"优酷",
160+
"pptv",
161+
"PPTV",
162+
"letv",
163+
"乐视",
164+
"leshi",
165+
"mgtv",
166+
"芒果",
167+
"sohu",
168+
"xigua",
169+
"fun",
170+
"风行"
171+
]
172+
},
173+
"header": {
174+
"User-Agent": "Mozilla/5.0"
175+
}
176+
}
143177
]
144178
}

docs/updateRecord.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ log(qs.stringify({a: 1, b: 2}))
1919

2020
4. 在.env文件中加入 `LOG_WITH_FILE = 1` 可以使请求日志输出到文件,不配置则默认输出到控制台
2121
5. 支持vercel部署,首页报错找不到readme.md无关大雅,能用就行,直接访问部署好的服务地址/config/1
22+
6. 支持自定义解析。放在jx目录的js文件
2223

2324
### 20241213
2425

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ registerRoutes(fastify, {
2424
rootDir: __dirname,
2525
docsDir: path.join(__dirname, 'docs'),
2626
jsDir: path.join(__dirname, 'js'),
27+
jxDir: path.join(__dirname, 'jx'),
2728
viewsDir: path.join(__dirname, 'views'),
2829
PORT,
2930
MAX_TEXT_SIZE,

0 commit comments

Comments
 (0)