Skip to content

Commit 340b17d

Browse files
committed
update:简化各个库并提高复用性。完成番茄小说爬虫示例。
1 parent 49c6566 commit 340b17d

File tree

17 files changed

+2263
-871
lines changed

17 files changed

+2263
-871
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,12 @@ todo:
99
1. js里的源能否去除export开头,保持跟qjs一致
1010
2. js里的源,像一级这种异步js,里面调用未定义的函数,能否不通过函数参数传入直接注入调用
1111
3. 在源的各个函数调用的时候动态注入input、MY_URL等局部变量不影响全局。搞了半天没成功,有点难受,待解决
12+
13+
14+
写源的函数不可以使用箭头函数,箭头函数无法拿到this作用域就没法获取input和MY_URL变量
15+
16+
精简去除的库:
17+
1. axios
18+
2. jsonpath
19+
3. underscore
20+
4. pino-pretty

drop_code/inject_var.js

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
moduleObject.injectVars = (vars) => {
2+
// 遍历 vars 对象,将其中的键值对转化为局部变量
3+
for (let key in vars) {
4+
let value = vars[key];
5+
6+
// 根据类型判断并转化值
7+
if (value === undefined) {
8+
value = 'undefined'; // undefined转为 'undefined'
9+
} else if (value === null) {
10+
value = 'null'; // null 转为 'null'
11+
} else if (value === '') {
12+
value = "''"; // 空字符串转为 "''"
13+
} else if (typeof value === 'boolean') {
14+
value = value ? 'true' : 'false'; // 布尔值转为 'true' 或 'false'
15+
} else if (typeof value === 'object') {
16+
if (Array.isArray(value)) {
17+
value = JSON.stringify(value); // 数组转为 JSON 字符串
18+
} else if (value instanceof Date) {
19+
value = `new Date("${value.toISOString()}")`; // Date 对象转为日期字符串
20+
} else if (value instanceof RegExp) {
21+
value = value.toString(); // 正则表达式转为字符串表示
22+
} else {
23+
value = JSON.stringify(value); // 普通对象转为 JSON 字符串
24+
}
25+
}
26+
27+
// 构造赋值代码,并通过 eval 动态执行
28+
let _code = `moduleObject.${key} = ${value}`;
29+
console.log(_code); // 打印每个注入的变量代码
30+
eval(_code); // 使用 eval 在当前作用域中定义变量
31+
}
32+
}
33+
moduleObject.injectMethodVars = async function (method, args, vars) {
34+
async function _inner() {
35+
let input;
36+
let MY_URL;
37+
// 遍历 vars 对象,将其中的键值对转化为局部变量
38+
for (let key in vars) {
39+
let value = vars[key];
40+
41+
// 根据类型判断并转化值
42+
if (value === undefined) {
43+
value = 'undefined'; // undefined转为 'undefined'
44+
} else if (value === null) {
45+
value = 'null'; // null 转为 'null'
46+
} else if (value === '') {
47+
value = "''"; // 空字符串转为 "''"
48+
} else if (typeof value === 'boolean') {
49+
value = value ? 'true' : 'false'; // 布尔值转为 'true' 或 'false'
50+
} else if (typeof value === 'object') {
51+
if (Array.isArray(value)) {
52+
value = JSON.stringify(value); // 数组转为 JSON 字符串
53+
} else if (value instanceof Date) {
54+
value = `new Date("${value.toISOString()}")`; // Date 对象转为日期字符串
55+
} else if (value instanceof RegExp) {
56+
value = value.toString(); // 正则表达式转为字符串表示
57+
} else {
58+
value = JSON.stringify(value); // 普通对象转为 JSON 字符串
59+
}
60+
}
61+
62+
// 构造赋值代码,并通过 eval 动态执行
63+
let _code = `${key} = ${value}`;
64+
console.log(_code); // 打印每个注入的变量代码
65+
eval(_code); // 使用 eval 在当前作用域中定义变量
66+
}
67+
68+
// 打印 inject 的变量值,确保它们在 eval 中被正确注入
69+
console.log('=====inject vars=====');
70+
console.log(input); // 现在 input 应该是定义好的
71+
console.log(MY_URL); // MY_URL 应该被注入并可用
72+
73+
// 执行传入的 method
74+
return await method(...args);
75+
}
76+
77+
return await _inner();
78+
};
79+
80+
81+
const injectVars = {input: '你好', MY_URL: 'https://example.com'};
82+
const functions = ['class_parse', '预处理', '推荐', '一级', '二级', '搜索', 'lazy'];
83+
84+
for (let func of functions) {
85+
if (typeof moduleObject[func] === 'function') {
86+
// 如果是箭头函数
87+
if (moduleObject[func].toString().includes('=>')) {
88+
const originalMethod = moduleObject[func];
89+
90+
// 包装箭头函数,将其改为闭包函数
91+
moduleObject[func] = function (...args) {
92+
const self = injectVars; // 动态注入 self
93+
return (async (...innerArgs) => {
94+
// 调用原始的箭头函数
95+
return await originalMethod.apply(self, innerArgs);
96+
})(...args);
97+
};
98+
}
99+
}
100+
}

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Fastify from 'fastify';
2-
import * as drpy from './libs/drpy.js';
2+
import * as drpy from './libs/drpyS.js';
33
import path from 'path';
44
import {fileURLToPath} from 'url';
55
import {base64Encode, base64Decode, atob, btoa} from "./libs_drpy/crypto-util.js";
@@ -40,7 +40,7 @@ fastify.get('/api/:module', async (request, reply) => {
4040

4141
if ('ac' in query && 'ids' in query) {
4242
// 详情逻辑
43-
const result = await drpy.detail(modulePath, query.ids);
43+
const result = await drpy.detail(modulePath, query.ids.split(','));
4444
return reply.send(result);
4545
}
4646

js/test.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,17 @@ var rule = {
4343
{vod_name: '测试电影2', vod_pic: '2.png', vod_remarks: '测试描述2', vod_id: 'http://www.2.com'},
4444
]
4545
},
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
46+
一级: async function(tid, pg, filter, extend) {
47+
let {input,MY_URL} = this;
48+
console.log({tid,pg,filter,extend});
49+
console.log(`input:${input},MY_URL:${MY_URL}`);
50+
console.log(rule.host);
51+
console.log(rule.host.rstrip('/'));
52+
// log(typeof jsonpath)
53+
// log(jsonpath({path:'$.title',json:rule}))
54+
log(MOBILE_UA);
55+
log(jsonpath.query(rule,'$.title'));
56+
return `input:${input},MY_URL:${MY_URL},host:${rule.host.rstrip('/')}`
5357
},
5458
二级: async () => {
5559
return '这是二级:' + rule.title

0 commit comments

Comments
 (0)