Subj : Re: Synchronet Javascript and reading XML
To   : KenDB3
From : echicken
Date : Fri Jul 29 2016 05:36 pm

> 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