-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathconfig.js
127 lines (120 loc) · 4.84 KB
/
config.js
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
import {readdirSync, readFileSync, writeFileSync, existsSync} from 'fs';
import path from 'path';
import * as drpy from '../libs/drpyS.js';
// 工具函数:生成 JSON 数据
async function generateSiteJSON(jsDir, requestHost) {
const files = readdirSync(jsDir);
const valid_files = files.filter((file) => file.endsWith('.js') && !file.startsWith('_')); // 筛选出不是 "_" 开头的 .js 文件
let sites = [];
for (const file of valid_files) {
const baseName = path.basename(file, '.js'); // 去掉文件扩展名
const key = `drpyS_${baseName}`;
const name = `${baseName}(DS)`;
const api = `${requestHost}/api/${baseName}`; // 使用请求的 host 地址,避免硬编码端口
let ruleObject = {
searchable: 1, // 固定值
filterable: 1, // 固定值
quickSearch: 0, // 固定值
};
try {
ruleObject = await drpy.getRuleObject(path.join(jsDir, file));
// log(file, ruleObject.title);
} catch (e) {
log(`file:${file} error:${e.message}`);
}
const site = {
key,
name,
type: 4, // 固定值
api,
searchable: ruleObject.searchable,
filterable: ruleObject.filterable,
quickSearch: ruleObject.quickSearch,
ext: "", // 固定为空字符串
};
sites.push(site);
}
return {sites};
}
function generateParseJSON(jxDir, requestHost) {
const files = readdirSync(jxDir);
const parses = files
.filter((file) => file.endsWith('.js') && !file.startsWith('_')) // 筛选出不是 "_" 开头的 .js 文件
.map((file) => {
const baseName = path.basename(file, '.js'); // 去掉文件扩展名
const api = `${requestHost}/parse/${baseName}?url=`; // 使用请求的 host 地址,避免硬编码端口
return {
name: baseName,
url: api,
type: 1,
ext: {
flag: [
"qiyi",
"imgo",
"爱奇艺",
"奇艺",
"qq",
"qq 预告及花絮",
"腾讯",
"youku",
"优酷",
"pptv",
"PPTV",
"letv",
"乐视",
"leshi",
"mgtv",
"芒果",
"sohu",
"xigua",
"fun",
"风行"
]
},
header: {
"User-Agent": "Mozilla/5.0"
}
}
});
return {parses};
}
export default (fastify, options, done) => {
fastify.get('/index', async (request, reply) => {
if (!existsSync(options.indexFilePath)) {
reply.code(404).send({error: 'index.json not found'});
return;
}
const content = readFileSync(options.indexFilePath, 'utf-8');
reply.send(JSON.parse(content));
});
// 接口:返回配置 JSON,同时写入 index.json
fastify.get('/config*', async (request, reply) => {
let t1 = (new Date()).getTime();
const cfg_path = request.params['*']; // 捕获整个路径
console.log(cfg_path);
try {
// 获取主机名,协议及端口
const protocol = request.protocol; // http 或 https
const hostname = request.hostname; // 主机名,不包含端口
const port = request.socket.localPort; // 获取当前服务的端口
console.log('port:', port);
let requestHost = cfg_path === '/1' ? `${protocol}://${hostname}` : `http://127.0.0.1:${options.PORT}`; // 动态生成根地址
const siteJSON = await generateSiteJSON(options.jsDir, requestHost);
const parseJSON = generateParseJSON(options.jxDir, requestHost);
const configObj = {...siteJSON, ...parseJSON};
const configStr = JSON.stringify(configObj, null, 2);
if (!process.env.VERCEL) { // Vercel 环境不支持写文件,关闭此功能
writeFileSync(options.indexFilePath, configStr, 'utf8'); // 写入 index.json
if (cfg_path === '/1') {
writeFileSync(options.customFilePath, configStr, 'utf8'); // 写入 index.json
}
}
let t2 = (new Date()).getTime();
configObj.cost = t2 - t1;
reply.send(configObj);
} catch (error) {
reply.status(500).send({error: 'Failed to generate site JSON', details: error.message});
}
});
done();
};