Skip to content

Commit b29034d

Browse files
author
Taois
committed
fix: 修复bug,重新发布版本
1 parent b435a35 commit b29034d

File tree

6 files changed

+169
-30
lines changed

6 files changed

+169
-30
lines changed

controllers/cron-tasker.js

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@ export default (fastify, options, done) => {
1515
};
1616

1717
const taskRegistry = new Map();
18+
const format_task_object = (task) => {
19+
return {
20+
name: task.name,
21+
path: task.path,
22+
schedule: task.schedule,
23+
lastRun: toBeijingTime(task.lastRun),
24+
nextRun: toBeijingTime(task.nextRun),
25+
status: task.status,
26+
// cronTask: task.cronTask,
27+
}
28+
};
1829

1930
function getNextRunFromJob(job) {
2031
try {
@@ -206,7 +217,7 @@ export default (fastify, options, done) => {
206217
return {
207218
message: `Task "${taskName}" executed manually`,
208219
status: 'success',
209-
task: taskRegistry.get(taskName)
220+
task: format_task_object(taskRegistry.get(taskName))
210221
};
211222
}
212223

@@ -223,14 +234,7 @@ export default (fastify, options, done) => {
223234
});
224235

225236
fastify.get('/tasks', {preHandler: validateBasicAuth}, async (request, reply) => {
226-
const tasks = [...taskRegistry.values()].map(task => ({
227-
name: task.name,
228-
schedule: task.schedule,
229-
status: task.status,
230-
lastRun: toBeijingTime(task.lastRun),
231-
nextRun: toBeijingTime(task.nextRun),
232-
path: task.path
233-
}));
237+
const tasks = [...taskRegistry.values()].map(task => (format_task_object(task)));
234238

235239
return tasks;
236240
});
@@ -246,14 +250,7 @@ export default (fastify, options, done) => {
246250
}
247251

248252
const task = taskRegistry.get(taskName);
249-
return {
250-
name: task.name,
251-
schedule: task.schedule,
252-
status: task.status,
253-
lastRun: toBeijingTime(task.lastRun),
254-
nextRun: toBeijingTime(task.nextRun),
255-
path: task.path
256-
};
253+
return format_task_object(task);
257254
});
258255

259256
fastify.addHook('onClose', async () => {

docs/updateRecord.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
1. 增加 `hipy` 适配器
1111
2. 新增依赖 `python-shell` 需要手动安装
1212
3. 统一libs接口 `cate` 改为 `category`
13+
4. 修复了 定时任务 `execute-now` 接口返回数据错误
14+
5. 目前py源的T4支持 ext扩展、getDependence依赖、以及 本地代理 和 动作,凑合能用,代码实现很狗屎。
1315

1416
### 20250815
1517

libs/hipy.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const loadEsmWithHash = async function (filePath, fileHash, env) {
3232
const bridgePath = path.join(_lib_path, '_bridge.py'); // 桥接脚本路径
3333

3434
// 创建方法调用函数
35-
const callPythonMethod = async (methodName, ...args) => {
35+
const callPythonMethod = async (methodName, env, ...args) => {
3636

3737
const options = {
3838
mode: 'text', // 使用JSON模式自动解析
@@ -49,7 +49,7 @@ const loadEsmWithHash = async function (filePath, fileHash, env) {
4949
try {
5050
const results = await PythonShell.run(bridgePath, {
5151
...options,
52-
args: [filePath, methodName, ...jsonArgs]
52+
args: [filePath, methodName, JSON.stringify(env), ...jsonArgs]
5353
});
5454
// 取最后一条返回
5555
let vodResult = results.slice(-1)[0];
@@ -107,7 +107,7 @@ const loadEsmWithHash = async function (filePath, fileHash, env) {
107107
// 为代理对象添加方法
108108
spiderMethods.forEach(method => {
109109
spiderProxy[method] = async (...args) => {
110-
return callPythonMethod(method, ...args);
110+
return callPythonMethod(method, env, ...args);
111111
};
112112
});
113113

@@ -126,7 +126,6 @@ const getRule = async function (filePath, env) {
126126

127127
const init = async function (filePath, env = {}, refresh) {
128128
try {
129-
// console.log('execute init');
130129
const fileContent = await readFile(filePath, 'utf-8');
131130
const fileHash = computeHash(fileContent);
132131
const moduleName = path.basename(filePath, '.js');
@@ -153,6 +152,7 @@ const init = async function (filePath, env = {}, refresh) {
153152
const cached = moduleCache.get(hashMd5);
154153
// 除hash外还必须保证proxyUrl实时相等,避免本地代理url的尴尬情况
155154
if (cached.hash === fileHash && cached.proxyUrl === env.proxyUrl) {
155+
// console.log('cached init');
156156
return cached.moduleObject;
157157
}
158158
}

spider/py/_bridge.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,14 @@ def import_module(module_url):
2121
return importlib.import_module(module_url)
2222

2323

24-
def load_spider(script_path):
24+
def load_spider(script_path, env):
2525
"""动态加载指定路径的 Python 脚本并实例化 Spider 类"""
26+
try:
27+
env = json.loads(env)
28+
except json.JSONDecodeError:
29+
# 保持原始字符串
30+
pass
31+
2632
try:
2733
script_name = os.path.basename(script_path)[:-3]
2834
print('load_spider:', script_name)
@@ -32,8 +38,9 @@ def load_spider(script_path):
3238
if not hasattr(module, 'Spider'):
3339
raise AttributeError(f"Script {script_path} does not contain a 'Spider' class")
3440

41+
proxyUrl = env.get('proxyUrl') if isinstance(env, dict) else ''
3542
# 实例化 Spider
36-
spider = module.Spider()
43+
spider = module.Spider(t4_api=proxyUrl)
3744
return spider
3845

3946
except Exception as e:
@@ -54,7 +61,7 @@ def t4_spider_init(spider, ext=''):
5461
module_names = []
5562
for lib in depends:
5663
try:
57-
module = import_module(lib).Spider()
64+
module = import_module(lib).Spider(t4_api=ext)
5865
modules.append(module)
5966
module_names.append(lib)
6067
except Exception as e:
@@ -68,7 +75,7 @@ def t4_spider_init(spider, ext=''):
6875
return spider, result
6976

7077

71-
def call_spider_method(spider, method_name, args):
78+
def call_spider_method(spider, method_name, env, args):
7279
"""调用 Spider 实例的指定方法"""
7380
invoke_method_name = method_dict.get(method_name) or method_name
7481
try:
@@ -87,18 +94,25 @@ def call_spider_method(spider, method_name, args):
8794
except json.JSONDecodeError:
8895
# 保持原始字符串
8996
parsed_args.append(arg)
97+
try:
98+
env = json.loads(env)
99+
except json.JSONDecodeError:
100+
# 保持原始字符串
101+
pass
90102

91103
print(f'parsed_args:{parsed_args}')
92104

93105
if method_name == 'init':
94-
extend = parsed_args[0] if parsed_args else ''
106+
extend = parsed_args[0] if parsed_args and isinstance(parsed_args, list) else ''
95107
spider, result = t4_spider_init(spider, extend)
96108
# result = spider.init(modules)
97109
return result
98110
else:
99111
if not hasattr(spider, '_init_ok_'):
100112
# spider,_ = t4_spider_init(spider,*parsed_args) # 需要传extend参数,暂时没有好办法
101-
extend = parsed_args[0] if parsed_args else ''
113+
# extend = parsed_args[0] if parsed_args and isinstance(parsed_args,list) else ''
114+
# extend = env.get('ext','')
115+
extend = env.get('ext') if isinstance(env, dict) else ''
102116
spider, _ = t4_spider_init(spider, extend)
103117
method = getattr(spider, invoke_method_name)
104118
result = method(*parsed_args)
@@ -125,10 +139,11 @@ def main():
125139
return
126140
script_path = sys.argv[1]
127141
method_name = sys.argv[2]
128-
args = sys.argv[3:]
142+
env = sys.argv[3]
143+
args = sys.argv[4:]
129144
print(f'script_path:{script_path},method_name:{method_name}')
130-
spider = load_spider(script_path)
131-
result = call_spider_method(spider, method_name, args)
145+
spider = load_spider(script_path, env)
146+
result = call_spider_method(spider, method_name, env, args)
132147
print(result)
133148

134149

spider/py/base/spider.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,13 @@ def getName(self):
112112
def init_api_ext_file(self):
113113
pass
114114

115+
def initEnv(self, env=None):
116+
if env is None:
117+
env = {}
118+
self._ENV = env
119+
self.t4_api = env.get('proxyUrl')
120+
121+
115122
def getProxyUrl(self):
116123
"""
117124
获取本地代理地址

spider/py/动作代理测试.py

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
"""
2+
@header({
3+
searchable: 0,
4+
filterable: 0,
5+
quickSearch: 0,
6+
title: '动作代理测试',
7+
lang: 'hipy'
8+
})
9+
"""
10+
11+
# coding=utf-8
12+
# !/usr/bin/python
13+
import json
14+
import sys
15+
import time
16+
17+
sys.path.append('..')
18+
try:
19+
from base.spider import BaseSpider
20+
except ImportError:
21+
from t4.base.spider import BaseSpider
22+
23+
24+
class Spider(BaseSpider):
25+
def getName(self):
26+
return "动作代理测试"
27+
28+
filterate = False
29+
30+
def init(self, extend=""):
31+
print("============{0}============".format(extend))
32+
33+
def getDependence(self):
34+
return []
35+
36+
def isVideoFormat(self, url):
37+
pass
38+
39+
def manualVideoCheck(self):
40+
pass
41+
42+
def homeContent(self, filter):
43+
return {
44+
'class': [],
45+
'type_flag': '3-00-S'
46+
}
47+
48+
def homeVideoContent(self):
49+
print('homeVod...')
50+
videos = [{
51+
"vod_id": json.dumps({
52+
'actionId': '代理地址',
53+
'id': 'proxy_url',
54+
'type': 'input',
55+
'title': '直接用的代理m3u链接',
56+
'tip': '..',
57+
'value': self.getProxyUrl() + '&flag=live'
58+
}, ensure_ascii=False),
59+
'vod_pic': 'clan://assets/tab.png?bgcolor=0',
60+
'vod_name': '复制代理地址',
61+
'vod_tag': 'action'
62+
}]
63+
64+
return {
65+
'list': videos
66+
}
67+
68+
def categoryContent(self, tid, pg, filter, extend):
69+
print('categoryContent:', tid, pg, filter, extend)
70+
return {}
71+
72+
def detailContent(self, array):
73+
vod = {}
74+
result = {
75+
'list': [
76+
vod
77+
]
78+
}
79+
return result
80+
81+
def searchContent(self, key, quick, pg=1):
82+
videos = []
83+
result = {
84+
'list': videos
85+
}
86+
return result
87+
88+
def playerContent(self, flag, id, vipFlags):
89+
result = {}
90+
parse = 1
91+
url = id
92+
result["parse"] = parse # 0=直接播放、1=嗅探
93+
result["playUrl"] = ''
94+
result["url"] = url
95+
result['jx'] = 0 # VIP解析,0=不解析、1=解析
96+
result["header"] = ''
97+
return result
98+
99+
config = {
100+
"player": {},
101+
"filter": {}
102+
}
103+
header = {}
104+
105+
def localProxy(self, params):
106+
return [404, 'text/plain', 'localProxy response with 404 not found']
107+
108+
def action(self, action, value):
109+
if action == '代理地址':
110+
json_dict = json.loads(value)
111+
return {
112+
'action': {
113+
'actionId': '__copy__',
114+
'content': json_dict.get('proxy_url', '')
115+
},
116+
117+
'toast': '直播源已复制到剪贴板',
118+
}

0 commit comments

Comments
 (0)