From 68b6e1c9feb8693c6c8ca4e06b8ec27b9c1a019d Mon Sep 17 00:00:00 2001 From: octospacc Date: Tue, 22 Aug 2023 00:56:52 +0200 Subject: [PATCH] Test new features for variables --- BeeWarehouse.js | 55 +++++++++++++++++++++++-------- Examples/ExampleCom/ExampleCom.js | 2 +- Examples/FbLikebox/FbLikebox.js | 6 ++-- Examples/FbLikebox/FbLikebox.xml | 4 +-- 4 files changed, 48 insertions(+), 19 deletions(-) diff --git a/BeeWarehouse.js b/BeeWarehouse.js index 0b6bc70..878309e 100644 --- a/BeeWarehouse.js +++ b/BeeWarehouse.js @@ -3,10 +3,29 @@ const request = require('sync-request'); const dom = require('@xmldom/xmldom').DOMParser; const xpath = require('xpath'); -const FunctToEvalString = (Funct) => Funct.toString().split('{').slice(1).join('{').split('}').slice(0, -1).join('}'); +const FormatMacrosInText = (Text) => { + //console.log('`' + Text.replaceAll('{', '${Macros.').replaceAll('}', '({})}') + '`'); + return ('`' + Text.replaceAll('{', '${Macros.').replaceAll('}', '({})}') + '`'); +}; + +const GetStepValues = (Step, Parent, Reqs) => { + let StepFinal = {}; + let Macros = Reqs.Macros; + let Values = xpath.select('./*', Step); + for (let Value of Values) { + // if '${Step.getAttribute('source').toLowerCase()}' === 'body' + if (Value.tagName.toLowerCase() === 'value') { + StepFinal[Value.getAttribute('name')] = xpath.select(eval(FormatMacrosInText(Value.getAttribute('path'))), Reqs.Html/*HtmlDom*/).toString(); + } else + if (Value.tagName.toLowerCase() === 'items') { + StepFinal = {...StepFinal, ...GetStepValues(Value, Step, Reqs)}; + }; + }; + return StepFinal; +}; const LoadBeesText = (Xml) => { - let Final = {}; + let [Final, Macros] = [{}, {}]; let Parsed = new dom().parseFromString(Xml, 'text/xml'); let Bees = xpath.select('/bees/bee', Parsed); for (let Bee of Bees) { @@ -15,32 +34,42 @@ const LoadBeesText = (Xml) => { ApiUrl: Bee.getAttribute('apiurl'), Functions: {}, }; + let Sets = xpath.select('//set', Bee); + for (let Set_ of Sets) { + //let ArgsList = Set_.getAttribute('').split(' '); + //if (ArgsList[0]) { + Macros[Set_.getAttribute('name')] = (Args) => { + //console.log(eval('`' + Set_.getAttribute('value').replaceAll('{', '${Args.') + '`')); + eval('`' + Set_.getAttribute('value').replaceAll('{', '${Args.') + '`'); + }; + //}; + }; let Functs = xpath.select('./function', Bee); for (let Funct of Functs) { let FuncName = Funct.getAttribute('name'); let SubFuncts = []; let Steps = xpath.select('./*', Funct); for (let Step of Steps) { - SubFuncts = [...SubFuncts, () => { + SubFuncts = [...SubFuncts, (Args) => { let StepFinal = {}; - if (Step.tagName === 'request') { - let Res = request(Step.getAttribute('method'), `${Final[Name].ApiUrl}${Step.getAttribute('path')}`); - // if '${Step.getAttribute('format')}' == 'HTML' + for (let Arg in Args) { + Macros[Arg] = () => Args[Arg]; + }; + if (Step.tagName.toLowerCase() === 'request') { + let Res = request(Step.getAttribute('method'), Final[Name].ApiUrl + eval(FormatMacrosInText(Step.getAttribute('path')))); + // if '${Step.getAttribute('format').toLowerCase()}' === 'html' let Body = Res.getBody('utf-8'); let HtmlDom = new dom().parseFromString(Body); - let Values = xpath.select('./value', Step); - for (let Value of Values) { - // if '${Step.getAttribute('source')}' == 'body' - StepFinal[Value.getAttribute('name')] = xpath.select(Value.getAttribute('path'), HtmlDom).toString(); - }; + //let Values = xpath.select('./value', Step); + StepFinal = GetStepValues(Step, null, {Html: HtmlDom, Macros: Macros}); }; return StepFinal; }]; }; - Final[Name].Functions[FuncName] = () => { + Final[Name].Functions[FuncName] = (Args) => { let FuncFinal = {}; for (SubFunct of SubFuncts) { - FuncFinal = {...FuncFinal, ...SubFunct()}; + FuncFinal = {...FuncFinal, ...SubFunct(Args)}; }; return FuncFinal; }; diff --git a/Examples/ExampleCom/ExampleCom.js b/Examples/ExampleCom/ExampleCom.js index 9b3edc9..d9e9e38 100644 --- a/Examples/ExampleCom/ExampleCom.js +++ b/Examples/ExampleCom/ExampleCom.js @@ -1,6 +1,6 @@ #!/usr/bin/env node const BeeWarehouse = require('../../BeeWarehouse.js'); -let ExampleCom = BeeWarehouse.LoadBeesFile('./ExampleCom.xml').ExampleCom; +let ExampleCom = BeeWarehouse.LoadBeesFile(`${__dirname}/ExampleCom.xml`).ExampleCom; let Info = ExampleCom.Functions.GetInfo(); console.log(Info); diff --git a/Examples/FbLikebox/FbLikebox.js b/Examples/FbLikebox/FbLikebox.js index 8d97851..b9acb2b 100644 --- a/Examples/FbLikebox/FbLikebox.js +++ b/Examples/FbLikebox/FbLikebox.js @@ -2,10 +2,10 @@ const BeeWarehouse = require('../../BeeWarehouse.js'); let Arguments = { "Source": "profile.php?id=100043901540898" }; -let FbLikebox = BeeWarehouse.LoadBeesFile('./FbLikebox.xml').FbLikebox; +let FbLikebox = BeeWarehouse.LoadBeesFile(`${__dirname}/FbLikebox.xml`).FbLikebox; -let Profile = ExampleCom.Functions.GetProfileInfo(Arguments); +let Profile = FbLikebox.Functions.GetProfileInfo(Arguments); console.log(Profile); -let Posts = ExampleCom.Functions.GetLatestPosts(Arguments); +let Posts = FbLikebox.Functions.GetLatestPosts(Arguments); console.log(Posts); diff --git a/Examples/FbLikebox/FbLikebox.xml b/Examples/FbLikebox/FbLikebox.xml index bf6fa93..2998611 100644 --- a/Examples/FbLikebox/FbLikebox.xml +++ b/Examples/FbLikebox/FbLikebox.xml @@ -1,7 +1,7 @@ - + @@ -12,7 +12,7 @@ - +