• Synchronet Javascript and

    From KenDB3@VERT/KD3NET to All on Wed Jul 27 12:36:00 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 19:10:00 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 05:11:00 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?

    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.

    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.

    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.

    Ahhhh! Gotcha.

    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).

    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.

    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.

    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

    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.

    That's awesome. I really appreciate the help!

    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.

    Understood!

    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 Kirkman@VERT/GUARDIAN to KenDB3 on Thu Aug 4 06:10:00 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