• Synchronet Javascript and reading XML

    From KenDB3@VERT/KD3NET to All on Wed Jul 27 16:36:02 2016
    Hello folks,ããI'm rather stuck on something. Is there a way to read an XML file using theãSynchronet Javascript model? ããJSON has the lovely JSON.parse(), but I'm not sure how to read the data, orãmaybe convert it into JSON to make things easier. I even noticed thatãXMLHttpRequest is not defined, while HTTPRequest is defined. ããA lot of the code snippets I find tend to use things in the DOM and has thingsãlike hasChildNodes which is not defined in Core JS or Sync. ããSo, I was wondering if anyone else has tried to do this and has a solution, orãat least some advice to give. ããFor reference, I am trying to grab the data found here:ãhttp://www.hamqsl.com/solarxml.phpããIf I can turn into something usable by Sync, that would be awesome. ãã~KenDB3ãã---ã þ Synchronet þ KD3net-Rhode Island's only BBS about nothing. http://bbs.kd3.usã
  • From echicken@VERT/ECBBS to KenDB3 on Wed Jul 27 23:10:01 2016
    I'm rather stuck on something. Is there a way to read an XML file using theã> Synchronet Javascript model? ããE4X (ECMAScript for XML, IIRC) is still available in Synchronet's JSãinterpreter as far as I know. It's kinda shitty but can get the job done.ãDocumentation can be found on the web.ããThere are probably a few Synchronet-specific examples of how to use it;ã'exec/load/rss-atom.js', which I made, is not the best but does what it doesãand should be easy to follow. It uses E4X to turn an RSS or Atom feed (bothãare XML) into an easier-to-work-with JS object.ã ã> JSON has the lovely JSON.parse(), but I'm not sure how to read the data, orã> maybe convert it into JSON to make things easier. I even noticed thatã> XMLHttpRequest is not defined, while HTTPRequest is defined. ããXMLHttpRequest is something that exists in browsers, and doesn't necessarilyãhave to have anything to do with XML. It's a way for a script to make theãbrowser load something asynchronously / in the background via HTTP; good forãupdating content on a page without the user having to reload.ããHTTPRequest is defined if you load 'exec/load/http.js' into your script.ãThat's an HTTP(S) client for Synchronet's JS environment which you can use inãyour scripts (and I assume you already do in that weather script).ãã> A lot of the code snippets I find tend to use things in the DOM and hasã> things like hasChildNodes which is not defined in Core JS or Sync. ããYes, if you look for XML parsing via JS, the vast majority of examples will beãDOM-based and/or browser specific. We don't have the DOM around here. It'sãpossible that you'd be able to find a pure-JS parser if you looked, which mayãor may not be portable for Synchronet with some effort.ãã> For reference, I am trying to grab the data found here:ã> http://www.hamqsl.com/solarxml.phpããThere's already some code for dealing with this exact feed, but it's embeddedãin an IRC bot. You might be able to find something useful in:ã'/exec/ircbots/ham/ham.js', however this should work:ããload('http.js');ãtry {ã var solardata = new XML(ã (new HTTPRequest()).Get(ã 'http://www.hamqsl.com/solarxml.php'ã ).replace(ã /<\?[^?]*\?>/g, ''ã )ã ).solardata;ã} catch (err) {ã log('Shit done borked! ' + err);ã}ãã(See https://bbs.electronicchicken.com/temp/solar.txt if that didn't comeãthrough okay.)ããYou should then be able to get at the values from the feed like so:ããprint(solardata.sunspots);ãprint(solardata.solarwind);ããAnd so on.ããThe IRC bot code mentioned above has examples of how to deal with the nestedã'calculated(vhf)conditions' values, which gets deeper into E4X than I care toãdo right now.ããHope this helps.ãã---ãechicken ãelectronic chicken bbs - bbs.electronicchicken.com - 416-273-7230ã þ Synchronet þ electronic chicken bbs - bbs.electronicchicken.comã
  • From KenDB3@VERT/KD3NET to echicken on Thu Jul 28 09:11:44 2016
    Re: Synchronet Javascript and reading XMLã By: echicken to KenDB3 on Wed Jul 27 2016 11:10 pmããThank you EC, this helps tremendously. It's hard to know what you don't knowãsometimes.ãã >> I'm rather stuck on something. Is there a way to read an XML fileã >> using the Synchronet Javascript model? ãã ec> E4X (ECMAScript for XML, IIRC) is still available in Synchronet's JSã ec> interpreter as far as I know. It's kinda shitty but can get the job done.ã ec> Documentation can be found on the web.ãã ec> There are probably a few Synchronet-specific examples of how to use it;ã ec> 'exec/load/rss-atom.js', which I made, is not the best but does what itã ec> does and should be easy to follow. It uses E4X to turn an RSS or Atom feedã ec> (both are XML) into an easier-to-work-with JS object.ããI will definitely give that a look. I was trying to think of what might dealãwith XML and for the life of me couldn't remember this one.ãã >> JSON has the lovely JSON.parse(), but I'm not sure how to read theã >> data, or maybe convert it into JSON to make things easier. I evenã >> noticed that XMLHttpRequest is not defined, while HTTPRequest isã >> defined. ãã ec> XMLHttpRequest is something that exists in browsers, and doesn'tã ec> necessarily have to have anything to do with XML. It's a way for a scriptã ec> to make the browser load something asynchronously / in the background viaã ec> HTTP; good for updating content on a page without the user having toã ec> reload. ããAhhhh! Gotcha.ãã ec> HTTPRequest is defined if you load 'exec/load/http.js' into your script.ã ec> That's an HTTP(S) client for Synchronet's JS environment which you can useã ec> in your scripts (and I assume you already do in that weather script).ããI do use it there, and now, thanks to the explanation, I get the concept betterãthan I did before. Much appreciated :-)ãã >> A lot of the code snippets I find tend to use things in the DOM andã >> has things like hasChildNodes which is not defined in Core JS or Sync.ãã ec> Yes, if you look for XML parsing via JS, the vast majority of examplesã ec> will be DOM-based and/or browser specific. We don't have the DOM aroundã ec> here. It's possible that you'd be able to find a pure-JS parser if youã ec> looked, which may or may not be portable for Synchronet with some effort.ããI did a bunch of looking and at first I wasn't turning up much, but eventuallyãI found a pure-JS that would do XML to JSON, but the output wasn't the best. Itãcan be found here for anyone else interested though:ãhttp://www.thomasfrank.se/xml_to_json.html ãã >> For reference, I am trying to grab the data found here:ã >> http://www.hamqsl.com/solarxml.phpãã ec> There's already some code for dealing with this exact feed, but it'sã ec> embedded in an IRC bot. You might be able to find something useful in:ã ec> '/exec/ircbots/ham/ham.js', however this should work:ãã ec> load('http.js');ã ec> try {ã ec> var solardata = new XML(ã ec> (new HTTPRequest()).Get(ã ec> 'http://www.hamqsl.com/solarxml.php'ã ec> ).replace(ã ec> /<\?[^?]*\?>/g, ''ã ec> )ã ec> ).solardata;ã ec> } catch (err) {ã ec> log('Shit done borked! ' + err);ã ec> }ãã ec> (See https://bbs.electronicchicken.com/temp/solar.txt if that didn't comeã ec> through okay.)ãã ec> You should then be able to get at the values from the feed like so:ãã ec> print(solardata.sunspots);ã ec> print(solardata.solarwind);ãã ec> And so on.ããThat's awesome. I really appreciate the help!ãã ec> The IRC bot code mentioned above has examples of how to deal with theã ec> nested 'calculated(vhf)conditions' values, which gets deeper into E4X thanã ec> I care to do right now.ããUnderstood!ãã ec> Hope this helps.ããImmensely. Thanks again for your help EC. Not sure if I can do what I amãsetting out to do, but it's always fun playing around with stuff.ãã~KenDB3ãã---ã þ Synchronet þ KD3net-Rhode Island's only BBS about nothing. http://bbs.kd3.usã
  • From KenDB3@VERT/KD3NET to echicken on Fri Jul 29 16:49:55 2016
    There's already some code for dealing with this exact feed, but it'sã > embedded in an IRC bot. You might be able to find something useful in:ã > '/exec/ircbots/ham/ham.js', however this should work:ãã > load('http.js');ã > try {ã > var solardata = new XML(ã > (new HTTPRequest()).Get(ã > 'http://www.hamqsl.com/solarxml.php'ã > ).replace(ã > /<\?[^?]*\?>/g, ''ã > )ã > ).solardata;ã > } catch (err) {ã > log('Shit done borked! ' + err);ã > }ãã > (See https://bbs.electronicchicken.com/temp/solar.txt if that didn't comeã > through okay.)ãã > You should then be able to get at the values from the feed like so:ãã > print(solardata.sunspots);ã > print(solardata.solarwind);ãã > And so on.ãã > The IRC bot code mentioned above has examples of how to deal with the nestedã > 'calculated(vhf)conditions' values, which gets deeper into E4X than I careã > to do right now.ãããEC, I have another question (or anyone that feels they can answer).ããI notice in the IRC bot code that you can get banned for hitting the web pageãtoo often. ããWould there be a way to bring in the XML data without an HTTPRequest from aãlocal file? Say something I saved from the last successful pull from the liveãfile and defined as:ãvar localfile = js.exec_dir + "solar.php";ãwhere solar.php was saved from a previous lookup less than an hour ago?ããMy awful attempt to do this currently looks like this:ããif((time() - last_solar_update) > 60*60) {ã try {ã var solardata = new XML(ã (new HTTPRequest()).Get(ã 'http://bbs.kd3.us/sol/solarxml.php.xml'ã ).replace(ã /<\?[^?]*\?>/g, ''ã )ã ).solardata;ã } catch (err) {ã log('Shit done borked! ' + err);ã }ã} else {ã try {ã var localfile = js.exec_dir + "solar.php";ã var localPHPfile = new File(localfile);ã localPHPfile.open("r"); // open file with read accessã var localXMLfile = localPHPfile.readAll();ã var fixedXMLfile = localXMLfile.replace(ã /<\?[^?]*\?>/g, ''ã );ã var solardata = new XML(ã fixedXMLfile.solardata);ã } catch (err) {ã log('Shit done borked! ' + err);ã }ã localPHPfile.close();ã}ããAlso, if this is simply a crazy thing to try and do, feel free to let me know.ãlocalXMLfile keeps
    coming back as null and I'm not sure where I've gone wrongãanymore. ãã~KenDB3ãã---ã þ Synchronet þ KD3net-Rhode Island's only BBS about nothing. http://bbs.kd3.usã
  • From echicken@VERT/ECBBS to KenDB3 on Fri Jul 29 17:36:03 2016
    I notice in the IRC bot code that you can get banned for hitting the web pageã> too often. ããYes, this is something that I recall hearing about in the past. I guess youãget x number of hits per day.ã ã> Would there be a way to bring in the XML data without an HTTPRequest from aã> local file? Say ããSure. I would do something like this:ããload('http.js');ããvar url = 'http://www.hamqsl.com/solarxml.php';ãvar file = system.data_dir + 'solardata.xml';ãvar age = 43200; // Secondsãã// Fetch data via HTTP and write to file specified aboveãfunction getSolarData() {ã var sd = (new HTTPRequest()).Get(url);ã var f = new File(file);ã f.open('w');ã f.write(sd);ã f.close();ã}ãã// Read data from local file and return parsed XML objectãfunction readSolarData() {ã var f = new File(file);ã f.open('r');ã var sd = new XML(f.read().replace(/<\?[^?]*\?>/g, ''));ã f.close();ã return sd;ã}ãã// Fetch new data if local file timestamp less than file age specified aboveãif (!file_exists(file) || time() - file_utime(file) > 43200) getSolarData();ãã// Read the current data on handãvar sd = readSolarData();ãã// Now start printing out that fascinating solar data and your happy/sad sunãfaceããIt would be worth throwing in some try ... catch blocks somewhere in there,ãbecause the HTTP load or XML parsing portions may fail for a variety ofãreasons.ããYou could also move the data-fetching part into a separate script and run it onãa schedule once, twice, or however many times per day that you want, then theãuser facing script just loads whatever data is in the file that the otherãscript writes to.ãã---ãechicken ãelectronic chicken bbs - bbs.electronicchicken.com - 416-273-7230ã þ Synchronet þ electronic chicken bbs - bbs.electronicchicken.comã
  • From KenDB3@VERT/KD3NET to echicken on Sat Jul 30 01:01:44 2016
    I notice in the IRC bot code that you can get banned for hitting the webã > > page too often. ãã > Yes, this is something that I recall hearing about in the past. I guess youã > get x number of hits per day.ã > ã > > Would there be a way to bring in the XML data without an HTTPRequest fromã > > a local file? Say ãã > Sure. I would do something like this:ãã > load('http.js');ãã > var url = 'http://www.hamqsl.com/solarxml.php';ã > var file = system.data_dir + 'solardata.xml';ã > var age = 43200; // Secondsãã > // Fetch data via HTTP and write to file specified aboveã > function getSolarData() {ã > var sd = (new HTTPRequest()).Get(url);ã > var f = new File(file);ã > f.open('w');ã > f.write(sd);ã > f.close();ã > }ãã > // Read data from local file and return parsed XML objectã > function readSolarData() {ã > var f = new File(file);ã > f.open('r');ã > var sd = new XML(f.read().replace(/<\?[^?]*\?>/g, ''));ã > f.close();ã > return sd;ã > }ãã > // Fetch new data if local file timestamp less than file age specified aboveã > if (!file_exists(file) || time() - file_utime(file) > 43200) getSolarData();ãã > // Read the current data on handã > var sd = readSolarData();ãã > // Now start printing out that fascinating solar data and your happy/sad sunã > faceãã > It would be worth throwing in some try ... catch blocks somewhere in there,ã > because the HTTP load or XML parsing portions may fail for a variety ofã > reasons.ãã > You could also move the data-fetching part into a separate script and run itã > on a schedule once, twice, or however many times per day that you want, thenã > the user facing script just loads whatever data is in the file that theã > other script writes to.ããããThanks again EC, it is much appreciated. :-)ãã~KenDB3ãã---ã þ Synchronet þ KD3net-Rhode Island's only BBS about nothing. http://bbs.kd3.usã
  • From Kirkman@VERT/GUARDIAN to KenDB3 on Thu Aug 4 10:10:51 2016
    I faced the same quandary when I was putting together Sports Stats. UltimatelyãI decided to use Python to write my data scraper. It pulls in the XML andãconverts it to JSON. Once the data is in JSON, then I can parse it using Sync'sãJS. ãã////--------------------------------------------------ãBiC -=- http://breakintochat.com -=- bbs wiki and blogãã---ã þ Synchronetã
  • From KenDB3@VERT/KD3NET to Kirkman on Fri Aug 5 12:09:14 2016
    I faced the same quandary when I was putting together Sports Stats.ã > Ultimately I decided to use Python to write my data scraper. It pulls in theã > XML and converts it to JSON. Once the data is in JSON, then I can parse itã > using Sync's JS. ãããI was very close to doing that if I couldn't get the data a different way. Iãwas hoping to make the script more portable for Sync sysops though. So, thanksãto echicken, it turned out pretty well. ãã~KenDB3ãã---ã þ Synchronet þ KD3net-Rhode Island's only BBS about nothing. http://bbs.kd3.usã