@@ -6,6 +6,7 @@ import {fileURLToPath} from 'url';
66import { md5 } from "../libs_drpy/crypto-util.js" ;
77import { PythonShell , PythonShellError } from 'python-shell' ;
88import { fastify } from "../controllers/fastlogger.js" ;
9+ import { daemon } from "../utils/daemonManager.js" ;
910
1011// 缓存已初始化的模块和文件 hash 值
1112const moduleCache = new Map ( ) ;
@@ -32,7 +33,7 @@ const loadEsmWithHash = async function (filePath, fileHash, env) {
3233 const bridgePath = path . join ( _lib_path , '_bridge.py' ) ; // 桥接脚本路径
3334
3435 // 创建方法调用函数
35- const callPythonMethod = async ( methodName , env , ...args ) => {
36+ const callPythonMethodOld = async ( methodName , env , ...args ) => {
3637
3738 const options = {
3839 mode : 'text' , // 使用JSON模式自动解析
@@ -98,6 +99,69 @@ const loadEsmWithHash = async function (filePath, fileHash, env) {
9899 }
99100 } ;
100101
102+ const callPythonMethod = async ( methodName , env , ...args ) => {
103+ const config = daemon . getDaemonConfig ( ) ;
104+ const command = [
105+ `"${ daemon . getPythonPath ( ) } "` ,
106+ `"${ config . clientScript } "` ,
107+ `--script-path "${ filePath } "` ,
108+ `--method-name "${ methodName } "` ,
109+ `--env '${ JSON . stringify ( env ) } '` ,
110+ ...args . map ( arg => `--arg '${ JSON . stringify ( arg ) } '` )
111+ ] . join ( ' ' ) ;
112+ // console.log(command);
113+ const cmd_args = [ ] ;
114+ args . forEach ( arg => {
115+ cmd_args . push ( `--arg` ) ;
116+ cmd_args . push ( `${ JSON . stringify ( arg ) } ` ) ;
117+ } ) ;
118+ const options = {
119+ mode : 'text' ,
120+ pythonPath : daemon . getPythonPath ( ) ,
121+ pythonOptions : [ '-u' ] , // 无缓冲输出
122+ env : {
123+ "PYTHONIOENCODING" : 'utf-8' ,
124+ } ,
125+ args : [
126+ '--script-path' , filePath ,
127+ '--method-name' , methodName ,
128+ '--env' , JSON . stringify ( env ) ,
129+ ...cmd_args
130+ ]
131+ } ;
132+ const results = await PythonShell . run ( config . clientScript , {
133+ ...options ,
134+ } ) ;
135+ // 取最后一条返回
136+ const stdout = results . slice ( - 1 ) [ 0 ] ;
137+ fastify . log . info ( `hipy logs: ${ JSON . stringify ( results . slice ( 0 , - 1 ) ) } ` ) ;
138+ // console.log(`hipy logs: ${JSON.stringify(results.slice(0, -1))}`);
139+ let vodResult = { } ;
140+ if ( typeof stdout === 'string' && stdout ) {
141+ switch ( stdout ) {
142+ case 'None' :
143+ vodResult = null ;
144+ break ;
145+ case 'True' :
146+ vodResult = true ;
147+ break ;
148+ case 'False' :
149+ vodResult = false ;
150+ break ;
151+ default :
152+ vodResult = JSON5 . parse ( stdout ) ;
153+ break ;
154+ }
155+ }
156+ // console.log(typeof vodResult);
157+ // 检查是否有错误
158+ if ( vodResult && vodResult . error ) {
159+ throw new Error ( `Python错误: ${ vodResult . error } \n${ vodResult . traceback } ` ) ;
160+ }
161+ // console.log(vodResult);
162+ return vodResult . result ;
163+ }
164+
101165 // 定义Spider类的方法
102166 const spiderMethods = [
103167 'init' , 'home' , 'homeVod' , 'homeContent' , 'category' ,
@@ -162,12 +226,13 @@ const init = async function (filePath, env = {}, refresh) {
162226 module = await loadEsmWithHash ( filePath , fileHash , env ) ;
163227 // console.log('module:', module);
164228 const rule = module ;
165- await rule . init ( default_init_cfg ) ;
229+ const initValue = await rule . init ( default_init_cfg ) || { } ;
166230 let t2 = getNowTime ( ) ;
167231 const moduleObject = deepCopy ( rule ) ;
168232 moduleObject . cost = t2 - t1 ;
169233 moduleCache . set ( hashMd5 , { moduleObject, hash : fileHash , proxyUrl : env . proxyUrl } ) ;
170- return moduleObject ;
234+ // return moduleObject;
235+ return { ...moduleObject , ...initValue } ;
171236 } catch ( error ) {
172237 console . log ( `Error in hipy.init :${ filePath } ` , error ) ;
173238 throw new Error ( `Failed to initialize module:${ error . message } ` ) ;
0 commit comments