Skip to content

Commit 543faec

Browse files
committed
update:把drpy所需要的全部的库封装了一遍
1 parent 00dbd81 commit 543faec

26 files changed

+19666
-47
lines changed

Diff for: index.js

+20-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Fastify from 'fastify';
22
import * as drpy from './libs/drpy.js';
33
import path from 'path';
44
import {fileURLToPath} from 'url';
5+
import {base64Encode, base64Decode, atob, btoa} from "./libs_drpy/crypto-util.js";
56

67
const fastify = Fastify({logger: true});
78

@@ -18,25 +19,36 @@ fastify.get('/api/:module', async (request, reply) => {
1819
// 根据 query 参数决定执行逻辑
1920
if ('play' in query) {
2021
// 处理播放逻辑
21-
const result = await drpy.play(modulePath);
22+
const result = await drpy.play(modulePath, query.flag, query.play);
2223
return reply.send(result);
2324
}
2425

2526
if ('ac' in query && 't' in query) {
27+
let ext = query.ext;
28+
let extend = {};
29+
if (ext) {
30+
try {
31+
extend = JSON.parse(base64Decode(ext))
32+
} catch (e) {
33+
}
34+
}
2635
// 分类逻辑
27-
const result = await drpy.cate(modulePath);
36+
const result = await drpy.cate(modulePath, query.t, query.pg || 1, extend);
2837
return reply.send(result);
2938
}
3039

3140
if ('ac' in query && 'ids' in query) {
3241
// 详情逻辑
33-
const result = await drpy.detail(modulePath);
42+
const result = await drpy.detail(modulePath, query.ids);
3443
return reply.send(result);
3544
}
3645

3746
if ('wd' in query) {
3847
// 搜索逻辑
39-
const result = await drpy.search(modulePath);
48+
if (!('quick' in query)) {
49+
query.quick = 0
50+
}
51+
const result = await drpy.search(modulePath, query.wd, query.quick, query.pg || 1);
4052
return reply.send(result);
4153
}
4254

@@ -45,9 +57,11 @@ fastify.get('/api/:module', async (request, reply) => {
4557
const refreshedObject = await drpy.init(modulePath, true);
4658
return reply.send(refreshedObject);
4759
}
48-
60+
if (!('filter' in query)) {
61+
query.filter = 1
62+
}
4963
// 默认逻辑,返回 home + homeVod 接口
50-
const result_home = await drpy.home(modulePath);
64+
const result_home = await drpy.home(modulePath, query.filter);
5165
const result_homeVod = await drpy.homeVod(modulePath);
5266
const result = {
5367
class: result_home,

Diff for: js/qq.js

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
var rule = {
2+
title: '标题1',
3+
description: '这是描述',
4+
category: '视频',
5+
class_parse: async () => {
6+
console.log('执行了分类获取')
7+
return [
8+
{type_id: '1', type_name: '电影'},
9+
{type_id: '2', type_name: '电视剧'},
10+
{type_id: '3', type_name: '综艺'},
11+
{type_id: '4', type_name: '动漫'},
12+
]
13+
},
14+
预处理: async () => {
15+
console.log('执行了预处理')
16+
rule.title = 'qq影视'
17+
await local.set(rule.title, 'cookie', 'qwer1234')
18+
19+
},
20+
推荐: async () => {
21+
console.log('执行了推荐函数')
22+
console.log(typeof (jsp))
23+
console.log(typeof (pdfh))
24+
console.log('pako:', typeof (pako))
25+
let html = (await req('https://www.baidu.com/', {})).content
26+
console.log(html)
27+
let cookie = await local.get(rule.title, 'cookie')
28+
console.log(cookie)
29+
console.log(base64Encode(cookie))
30+
let url = pdfh(html, 'script&&Html')
31+
console.log('url:', url)
32+
// console.log(template.getMubans())
33+
console.log(typeof template.getMubans)
34+
return [
35+
{vod_name: '测试电影1', vod_pic: '1.png', vod_remarks: '测试描述1', vod_id: 'http://www.1.com'},
36+
{vod_name: '测试电影2', vod_pic: '2.png', vod_remarks: '测试描述2', vod_id: 'http://www.2.com'},
37+
]
38+
},
39+
一级: async (args) => {
40+
// await sleep(200);
41+
sleepSync(200);
42+
let html = await req('123');
43+
console.log('title:', rule.title);
44+
console.log('html:' + html);
45+
console.log(typeof (btoa));
46+
console.log(btoa('123456'));
47+
console.log(base64Encode('123456'));
48+
// console.log({tid, pg, filter, extend})
49+
console.log(args)
50+
return html + '\n' + '这是一级:' + rule.title
51+
},
52+
二级: async () => {
53+
return '这是二级:' + rule.title
54+
},
55+
搜索: async () => {
56+
return '这是搜索:' + rule.title
57+
},
58+
lazy: async () => {
59+
return template.getMubans()
60+
},
61+
};

Diff for: js/test.js

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// js/test.js
2+
console.log('加载test...')
3+
globalThis.check = ()=>{
4+
console.log('check...')
5+
}
6+
check1 = ()=>{
7+
console.log('check1...')
8+
}
9+
sleepSync(2000);
10+
console.log('睡眠了200ms')
11+
async function test1(){
12+
await sleep(200);
13+
console.log('test1睡眠完毕11')
14+
}
15+
console.log('test1定义完毕')
16+
var rule = {
17+
title: '标题1',
18+
description: '这是描述',
19+
category: '视频',
20+
class_parse: async () => {
21+
console.log('执行了分类获取')
22+
return [
23+
{type_id: '1', type_name: '电影'},
24+
{type_id: '2', type_name: '电视剧'},
25+
{type_id: '3', type_name: '综艺'},
26+
{type_id: '4', type_name: '动漫'},
27+
]
28+
},
29+
预处理: async () => {
30+
console.log('执行了预处理')
31+
check()
32+
check1()
33+
await test1()
34+
eval('console.log("这是测试eval打印日志")')
35+
rule.title = 'test影视'
36+
},
37+
推荐: async () => {
38+
sleepSync(2000);
39+
console.log('进入了推荐')
40+
console.log(`这是推荐:${rule.title}`);
41+
return [
42+
{vod_name: '测试电影1', vod_pic: '1.png', vod_remarks: '测试描述1', vod_id: 'http://www.1.com'},
43+
{vod_name: '测试电影2', vod_pic: '2.png', vod_remarks: '测试描述2', vod_id: 'http://www.2.com'},
44+
]
45+
},
46+
一级: async () => {
47+
// await sleep(200);
48+
sleepSync(200);
49+
let html = await req('123');
50+
console.log('title:', rule.title);
51+
console.log('html:' + html);
52+
return html + '\n' + '这是一级:' + rule.title
53+
},
54+
二级: async () => {
55+
return '这是二级:' + rule.title
56+
},
57+
搜索: async () => {
58+
return '这是搜索:' + rule.title
59+
},
60+
lazy: async () => {
61+
return '这是播放:' + rule.title
62+
},
63+
};

Diff for: libs/drpy.js

+110-33
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,34 @@
1-
import * as utils from '../utils/utils.js'; // 使用 import 引入工具类
21
import {readFile} from 'fs/promises';
3-
import vm from 'vm'; // Node.js 的 vm 模块
4-
5-
const {req} = await import('../utils/req.js');
6-
const {sleep, sleepSync} = await import('../utils/utils.js');
7-
8-
// 缓存已初始化的模块
2+
import crypto from 'crypto';
3+
import vm from 'vm';
4+
import * as utils from '../utils/utils.js';
5+
// const { req } = await import('../utils/req.js');
6+
import {matchesAll, stringUtils, cut} from '../libs_drpy/external.js'
7+
import {gbkTool} from '../libs_drpy/gbk.js'
8+
import {atob, btoa} from "../libs_drpy/crypto-util.js";
9+
import template from '../libs_drpy/template.js'
10+
import '../libs_drpy/drpyInject.js'
11+
import '../libs_drpy/crypto-js.js';
12+
import '../libs_drpy/jsencrypt.js';
13+
import '../libs_drpy/node-rsa.js';
14+
import '../libs_drpy/pako.min.js';
15+
import '../libs_drpy/json5.js'
16+
import '../libs_drpy/jinja.js'
17+
18+
const {sleep, sleepSync} = utils
19+
20+
// 缓存已初始化的模块和文件 hash 值
921
const moduleCache = new Map();
1022

23+
/**
24+
* 计算文件内容的 hash 值
25+
* @param {string} content - 文件内容
26+
* @returns {string} - 文件内容的 hash 值
27+
*/
28+
function computeHash(content) {
29+
return crypto.createHash('sha256').update(content, 'utf8').digest('hex');
30+
}
31+
1132
/**
1233
* 初始化模块:加载并执行模块文件,存储初始化后的 rule 对象
1334
* 如果存在 `预处理` 属性且为函数,会在缓存前执行
@@ -16,24 +37,75 @@ const moduleCache = new Map();
1637
* @returns {Promise<object>} - 返回初始化后的模块对象
1738
*/
1839
export async function init(filePath, refresh) {
19-
if (moduleCache.has(filePath) && !refresh) {
20-
console.log(`Module ${filePath} already initialized, returning cached instance.`);
21-
return moduleCache.get(filePath);
22-
}
23-
2440
try {
25-
let t1 = utils.getNowTime();
26-
// 读取 JS 文件的内容
41+
// 读取文件内容
2742
const fileContent = await readFile(filePath, 'utf-8');
2843

29-
// 创建一个沙箱上下文,注入需要的全局变量和函数
30-
const sandbox = {
31-
console,
32-
req,
44+
// 计算文件的 hash 值
45+
const fileHash = computeHash(fileContent);
46+
47+
// 检查缓存:是否有文件且未刷新且文件 hash 未变化
48+
if (moduleCache.has(filePath) && !refresh) {
49+
const cached = moduleCache.get(filePath);
50+
if (cached.hash === fileHash) {
51+
console.log(`Module ${filePath} already initialized and unchanged, returning cached instance.`);
52+
return cached.moduleObject;
53+
}
54+
}
55+
56+
console.log(`Loading module: ${filePath}`);
57+
let t1 = utils.getNowTime();
58+
const utilsSanbox = {
3359
sleep,
3460
sleepSync,
3561
utils,
62+
}
63+
const drpySanbox = {
64+
jsp,
65+
pdfh,
66+
pd,
67+
pdfa,
68+
pdfl,
69+
pjfh,
70+
pj,
71+
pjfa,
72+
base64Encode,
73+
base64Decode,
74+
local,
75+
md5X,
76+
rsaX,
77+
aesX,
78+
desX,
79+
req,
80+
JSProxyStream,
81+
JSFile,
82+
js2Proxy,
83+
84+
}
85+
86+
const libsSanbox = {
87+
matchesAll,
88+
stringUtils,
89+
cut,
90+
gbkTool,
91+
CryptoJS,
92+
JSEncrypt,
93+
NODERSA,
94+
pako,
95+
JSON5,
96+
jinja,
97+
template,
98+
atob,
99+
btoa,
100+
}
101+
102+
// 创建一个沙箱上下文,注入需要的全局变量和函数
103+
const sandbox = {
104+
console,
36105
rule: {}, // 用于存放导出的 rule 对象
106+
...utilsSanbox,
107+
...drpySanbox,
108+
...libsSanbox,
37109
};
38110

39111
// 创建一个上下文
@@ -52,56 +124,61 @@ export async function init(filePath, refresh) {
52124
await moduleObject.预处理();
53125
}
54126

55-
// 缓存初始化后的模块
56-
moduleCache.set(filePath, moduleObject);
57-
58127
let t2 = utils.getNowTime();
59128
moduleObject.cost = t2 - t1;
60129

130+
// 缓存模块和文件的 hash 值
131+
moduleCache.set(filePath, {moduleObject, hash: fileHash});
132+
61133
return moduleObject;
62134
} catch (error) {
63135
console.error('Error in drpy.init:', error);
64136
throw new Error('Failed to initialize module');
65137
}
66138
}
67139

140+
68141
/**
69142
* 调用模块的指定方法
70143
* @param {string} filePath - 模块文件路径
71144
* @param {string} method - 要调用的属性方法名称
145+
* @param args
72146
* @returns {Promise<any>} - 方法调用的返回值
73147
*/
74-
async function invokeMethod(filePath, method) {
148+
async function invokeMethod(filePath, method, ...args) {
75149
const moduleObject = await init(filePath); // 确保模块已初始化
76150
if (moduleObject[method] && typeof moduleObject[method] === 'function') {
77-
return await moduleObject[method](); // 调用对应的方法
151+
return await moduleObject[method](...args); // 调用对应的方法并传递参数
78152
} else {
79153
throw new Error(`Method ${method} not found in module ${filePath}`);
80154
}
81155
}
82156

83157
// 各种接口调用方法
84158

85-
export async function home(filePath) {
86-
return await invokeMethod(filePath, 'class_parse');
159+
export async function home(filePath, filter = 1) {
160+
return await invokeMethod(filePath, 'class_parse', {filter});
87161
}
88162

89163
export async function homeVod(filePath) {
90164
return await invokeMethod(filePath, '推荐');
91165
}
92166

93-
export async function cate(filePath) {
94-
return await invokeMethod(filePath, '一级');
167+
export async function cate(filePath, tid, pg = 1, filter = 1, extend = {}) {
168+
return await invokeMethod(filePath, '一级', {tid, pg, filter, extend});
95169
}
96170

97-
export async function detail(filePath) {
98-
return await invokeMethod(filePath, '二级');
171+
export async function detail(filePath, ids) {
172+
if (!Array.isArray(ids)) throw new Error('Parameter "ids" must be an array');
173+
return await invokeMethod(filePath, '二级', {ids});
99174
}
100175

101-
export async function search(filePath) {
102-
return await invokeMethod(filePath, '搜索');
176+
export async function search(filePath, wd, quick = 0, pg = 1) {
177+
return await invokeMethod(filePath, '搜索', {wd, quick, pg});
103178
}
104179

105-
export async function play(filePath) {
106-
return await invokeMethod(filePath, 'lazy');
180+
export async function play(filePath, flag, id, flags) {
181+
flags = flags || [];
182+
if (!Array.isArray(flags)) throw new Error('Parameter "flags" must be an array');
183+
return await invokeMethod(filePath, 'lazy', {flag, id, flags});
107184
}

0 commit comments

Comments
 (0)