-
Notifications
You must be signed in to change notification settings - Fork 283
Expand file tree
/
Copy pathdrpy2.js
More file actions
100 lines (83 loc) · 2.93 KB
/
drpy2.js
File metadata and controls
100 lines (83 loc) · 2.93 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
import {computeHash, deepCopy} from "../utils/utils.js";
import {readFile} from "fs/promises";
import {md5} from "../libs_drpy/crypto-util.js";
const moduleCache = new Map();
const getRule = async function (filePath, env) {
const moduleObject = await init(filePath, env);
return JSON.stringify(moduleObject);
}
const json2Object = function (json) {
if (!json) {
return {}
} else if (json && typeof json === 'object') {
return json
}
return JSON.parse(json);
}
const init = async function (filePath, env = {}, refresh) {
try {
const fileContent = await readFile(filePath, 'utf-8');
const fileHash = computeHash(fileContent);
let moduleExt = env.ext || '';
let hashMd5 = md5(filePath + '#pAq#' + moduleExt);
if (moduleCache.has(hashMd5) && !refresh) {
const cached = moduleCache.get(hashMd5);
if (cached.hash === fileHash) {
return cached.moduleObject;
}
}
log(`Loading module: ${filePath}`);
let rule = {};
await rule.init(moduleExt || {});
const moduleObject = deepCopy(rule);
moduleCache.set(hashMd5, {moduleObject, hash: fileHash});
return moduleObject;
} catch (error) {
console.log(`Error in drpy2.init :${filePath}`, error);
throw new Error(`Failed to initialize module:${error.message}`);
}
}
const home = async function (filePath, env, filter = 1) {
const moduleObject = await init(filePath, env);
return json2Object(await moduleObject.home(filter));
}
const homeVod = async function (filePath, env) {
const moduleObject = await init(filePath, env);
return json2Object(await moduleObject.homeVod());
}
const category = async function (filePath, env, tid, pg = 1, filter = 1, extend = {}) {
const moduleObject = await init(filePath, env);
return json2Object(await moduleObject.category(tid, pg, filter, extend));
}
const detail = async function (filePath, env, ids) {
const moduleObject = await init(filePath, env);
return json2Object(await moduleObject.detail(ids));
}
const search = async function (filePath, env, wd, quick = 0, pg = 1) {
const moduleObject = await init(filePath, env);
return json2Object(await moduleObject.search(wd, quick, pg));
}
const play = async function (filePath, env, flag, id, flags) {
const moduleObject = await init(filePath, env);
return json2Object(await moduleObject.play(flag, id, flags));
}
const proxy = async function (filePath, env, params) {
const moduleObject = await init(filePath, env);
return json2Object(await moduleObject.proxy(params));
}
const action = async function (filePath, env, action, value) {
const moduleObject = await init(filePath, env);
return json2Object(await moduleObject.action(action, value));
}
export default {
getRule,
init,
home,
homeVod,
category,
detail,
search,
play,
proxy,
action,
}