|
| 1 | +import path from "path"; |
| 2 | +import {readFile} from "fs/promises"; |
| 3 | +import {fileURLToPath} from 'url'; |
| 4 | +import {execFile} from 'child_process'; |
| 5 | +import {promisify} from 'util'; |
| 6 | +import {getSitesMap} from "../utils/sites-map.js"; |
| 7 | +import {computeHash, deepCopy, getNowTime, urljoin} from "../utils/utils.js"; |
| 8 | +import {md5} from "../libs_drpy/crypto-util.js"; |
| 9 | +import {fastify} from "../controllers/fastlogger.js"; |
| 10 | +// import dotenv from 'dotenv'; |
| 11 | +// |
| 12 | +// dotenv.config({ path: path.join(process.cwd(), '.env.development') }); |
| 13 | + |
| 14 | +const execFileAsync = promisify(execFile); |
| 15 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 16 | +const _config_path = path.join(__dirname, '../config'); |
| 17 | +const _bridge_path = path.join(__dirname, '../spider/php/_bridge.php'); |
| 18 | + |
| 19 | +// Cache for module objects |
| 20 | +const moduleCache = new Map(); |
| 21 | + |
| 22 | +// Mapping from JS method names to PHP Spider method names |
| 23 | +const methodMapping = { |
| 24 | + 'init': 'init', |
| 25 | + 'home': 'homeContent', |
| 26 | + 'homeVod': 'homeVideoContent', |
| 27 | + 'category': 'categoryContent', |
| 28 | + 'detail': 'detailContent', |
| 29 | + 'search': 'searchContent', |
| 30 | + 'play': 'playerContent', |
| 31 | + 'proxy': 'proxy', // Not standard in BaseSpider, but might exist |
| 32 | + 'action': 'action' // Not standard |
| 33 | +}; |
| 34 | + |
| 35 | +// Helper to stringify args for CLI |
| 36 | +function stringify(arg) { |
| 37 | + if (arg === undefined) return 'null'; |
| 38 | + return JSON.stringify(arg); |
| 39 | +} |
| 40 | + |
| 41 | +// Helper to parse JSON output |
| 42 | +function json2Object(json) { |
| 43 | + if (!json) return {}; |
| 44 | + if (typeof json === 'object') return json; |
| 45 | + try { |
| 46 | + return JSON.parse(json); |
| 47 | + } catch (e) { |
| 48 | + return json; |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +// Execute PHP bridge |
| 53 | +const callPhpMethod = async (filePath, methodName, env, ...args) => { |
| 54 | + const phpPath = process.env.PHP_PATH || 'php'; |
| 55 | + const phpMethodName = methodMapping[methodName] || methodName; |
| 56 | + |
| 57 | + const cliArgs = [ |
| 58 | + _bridge_path, |
| 59 | + filePath, |
| 60 | + phpMethodName, |
| 61 | + JSON.stringify(env), |
| 62 | + ...args.map(stringify) |
| 63 | + ]; |
| 64 | + |
| 65 | + try { |
| 66 | + // fastify.log.info(`Calling PHP: ${phpPath} ${cliArgs.join(' ')}`); |
| 67 | + const {stdout, stderr} = await execFileAsync(phpPath, cliArgs, { |
| 68 | + encoding: 'utf8', |
| 69 | + maxBuffer: 10 * 1024 * 1024, // 10MB buffer |
| 70 | + env: { |
| 71 | + ...process.env, |
| 72 | + PYTHONIOENCODING: 'utf-8', // Just in case |
| 73 | + // Add any PHP specific env vars if needed |
| 74 | + } |
| 75 | + }); |
| 76 | + |
| 77 | + if (stderr) { |
| 78 | + // Log stderr but don't fail immediately unless stdout is empty or error |
| 79 | + // fastify.log.warn(`PHP Stderr: ${stderr}`); |
| 80 | + console.error(`PHP Stderr: ${stderr}`); |
| 81 | + } |
| 82 | + |
| 83 | + const result = json2Object(stdout.trim()); |
| 84 | + |
| 85 | + if (result && result.error) { |
| 86 | + throw new Error(`PHP Error: ${result.error}\nTrace: ${result.traceback}`); |
| 87 | + } |
| 88 | + |
| 89 | + return result; |
| 90 | + |
| 91 | + } catch (error) { |
| 92 | + console.error(`Error calling PHP method ${methodName}:`, error); |
| 93 | + throw error; |
| 94 | + } |
| 95 | +}; |
| 96 | + |
| 97 | +const loadEsmWithHash = async function (filePath, fileHash, env) { |
| 98 | + const spiderProxy = {}; |
| 99 | + const spiderMethods = Object.keys(methodMapping); |
| 100 | + |
| 101 | + spiderMethods.forEach(method => { |
| 102 | + spiderProxy[method] = async (...args) => { |
| 103 | + return callPhpMethod(filePath, method, env, ...args); |
| 104 | + }; |
| 105 | + }); |
| 106 | + |
| 107 | + return spiderProxy; |
| 108 | +}; |
| 109 | + |
| 110 | +const init = async function (filePath, env = {}, refresh) { |
| 111 | + try { |
| 112 | + const fileContent = await readFile(filePath, 'utf-8'); |
| 113 | + const fileHash = computeHash(fileContent); |
| 114 | + const moduleName = path.basename(filePath, '.php'); // .php extension |
| 115 | + let moduleExt = env.ext || ''; |
| 116 | + |
| 117 | + // Logic for SitesMap and moduleExt (similar to hipy.js) |
| 118 | + let SitesMap = getSitesMap(_config_path); |
| 119 | + if (moduleExt && SitesMap[moduleName]) { |
| 120 | + // ... logic for compressed ext ... |
| 121 | + // Simplified for now, assuming plain string or handled by caller |
| 122 | + } |
| 123 | + |
| 124 | + let hashMd5 = md5(filePath + '#php#' + moduleExt); |
| 125 | + |
| 126 | + if (moduleCache.has(hashMd5) && !refresh) { |
| 127 | + const cached = moduleCache.get(hashMd5); |
| 128 | + if (cached.hash === fileHash) { |
| 129 | + return cached.moduleObject; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + fastify.log.info(`Loading PHP module: ${filePath}`); |
| 134 | + let t1 = getNowTime(); |
| 135 | + |
| 136 | + const module = await loadEsmWithHash(filePath, fileHash, env); |
| 137 | + const rule = module; |
| 138 | + |
| 139 | + // Initialize the spider |
| 140 | + const initValue = await rule.init(moduleExt) || {}; |
| 141 | + |
| 142 | + let t2 = getNowTime(); |
| 143 | + const moduleObject = deepCopy(rule); |
| 144 | + moduleObject.cost = t2 - t1; |
| 145 | + |
| 146 | + moduleCache.set(hashMd5, {moduleObject, hash: fileHash}); |
| 147 | + return {...moduleObject, ...initValue}; |
| 148 | + |
| 149 | + } catch (error) { |
| 150 | + fastify.log.error(`Error in php.init :${filePath}`, error); |
| 151 | + throw new Error(`Failed to initialize PHP module:${error.message}`); |
| 152 | + } |
| 153 | +}; |
| 154 | + |
| 155 | +const getRule = async function (filePath, env) { |
| 156 | + const moduleObject = await init(filePath, env); |
| 157 | + return JSON.stringify(moduleObject); |
| 158 | +}; |
| 159 | + |
| 160 | +const home = async function (filePath, env, filter = 1) { |
| 161 | + const moduleObject = await init(filePath, env); |
| 162 | + return json2Object(await moduleObject.home(filter)); |
| 163 | +}; |
| 164 | + |
| 165 | +const homeVod = async function (filePath, env) { |
| 166 | + const moduleObject = await init(filePath, env); |
| 167 | + const homeVodResult = json2Object(await moduleObject.homeVod()); |
| 168 | + return homeVodResult && homeVodResult.list ? homeVodResult.list : homeVodResult; |
| 169 | +}; |
| 170 | + |
| 171 | +const category = async function (filePath, env, tid, pg = 1, filter = 1, extend = {}) { |
| 172 | + const moduleObject = await init(filePath, env); |
| 173 | + return json2Object(await moduleObject.category(tid, pg, filter, extend)); |
| 174 | +}; |
| 175 | + |
| 176 | +const detail = async function (filePath, env, ids) { |
| 177 | + const moduleObject = await init(filePath, env); |
| 178 | + return json2Object(await moduleObject.detail(ids)); |
| 179 | +}; |
| 180 | + |
| 181 | +const search = async function (filePath, env, wd, quick = 0, pg = 1) { |
| 182 | + const moduleObject = await init(filePath, env); |
| 183 | + return json2Object(await moduleObject.search(wd, quick, pg)); |
| 184 | +}; |
| 185 | + |
| 186 | +const play = async function (filePath, env, flag, id, flags) { |
| 187 | + const moduleObject = await init(filePath, env); |
| 188 | + return json2Object(await moduleObject.play(flag, id, flags)); |
| 189 | +}; |
| 190 | + |
| 191 | +const proxy = async function (filePath, env, params) { |
| 192 | + const moduleObject = await init(filePath, env); |
| 193 | + return json2Object(await moduleObject.proxy(params)); |
| 194 | +}; |
| 195 | + |
| 196 | +const action = async function (filePath, env, action, value) { |
| 197 | + const moduleObject = await init(filePath, env); |
| 198 | + return json2Object(await moduleObject.action(action, value)); |
| 199 | +}; |
| 200 | + |
| 201 | +export default { |
| 202 | + getRule, |
| 203 | + init, |
| 204 | + home, |
| 205 | + homeVod, |
| 206 | + category, |
| 207 | + detail, |
| 208 | + search, |
| 209 | + play, |
| 210 | + proxy, |
| 211 | + action |
| 212 | +}; |
0 commit comments