Skip to content
Snippets Groups Projects
Commit ee95c38b authored by echicken's avatar echicken :chicken:
Browse files

Cache stuff. Rearranged some things. Stuff like that.

parent e7c208ae
No related branches found
No related tags found
1 merge request!463MRC mods by Codefenix (2024-10-20)
...@@ -33,10 +33,19 @@ Contents ...@@ -33,10 +33,19 @@ Contents
3) Customization 3) Customization
Any arguments that you pass on the command line will be treated as an The default query URL is https://wttr.in/?AFn for ANSI, no 'Follow' line,
alternate URL to query. For example: and narrow output. You can override this by supplying an alternate URL on
the command line, such as:
Command Line ?wttr.js https://wttr.in/?m0AFn Command Line ?wttr.js https://wttr.in/?m0AFn
The default is URL is 'https://wttr.in/?AFn' for ANSI, no 'Follow' line, Responses are cached in your OS temp directory and remain valid for 3600
and narrow output. seconds (one hour). You can override this default cache TTL by supplying
a numeric value (in seconds) on the command line, such as:
Command Line ?wttr.js 1800
You can combine the parameters in whichever order you prefer:
Command Line ?wttr.js https://wttr.in/?m0AFn 1800
Command Line ?wttr.js 1800 https://wttr.in/?m0AFn
require('http.js', 'HTTPRequest');
const locator = load({}, js.exec_dir + 'locator.js');
const xterm = load({}, js.exec_dir + 'xterm-colors.js');
function uReplace(str) {
return str.replace(/\xE2\x9A\xA1/g, '\x01+\x01h\x01yZ \x01-'); // U+26A1 Lightning bolt
}
function getCacheName(qs, addr) {
const cfn = format('wttr.in_%s_%s.ans', qs, addr || '').replace(/[^0-9a-z\.]+/ig, '_');
return system.temp_path + cfn;
}
function readCache(qs, addr, ttl) {
const cfn = getCacheName(qs, addr);
const f = new File(cfn);
if (!f.exists) return;
if (time() - file_date(cfn) > ttl) return;
if (!f.open('r')) return;
const cache = f.read();
f.close();
return cache;
}
function writeCache(qs, addr, ans) {
const cfn = getCacheName(qs, addr);
const f = new File(cfn);
if (!f.open('w')) return;
f.write(ans);
f.close();
}
function fetchWeather(qs, addr) {
const http = new HTTPRequest();
if (addr !== undefined) http.extra_headers = { 'X-Forwarded-For': addr };
const body = http.Get(qs);
if (http.response_code !== 200) throw new Error('wttr.in response had status ' + http.response_code);
return body;
}
function getWeather(qs, ttl) {
const addr = locator.getAddress();
const cachedWeather = readCache(qs, addr, ttl);
if (cachedWeather !== undefined) return cachedWeather;
const weather = fetchWeather(qs, addr);
const text = uReplace(weather);
const ansi = xterm.convertColors(text);
writeCache(qs, addr, ansi);
return ansi;
}
this;
\ No newline at end of file
require('sbbsdefs.js', 'P_UTF8'); require('sbbsdefs.js', 'P_UTF8');
require('http.js', 'HTTPRequest'); const wttr = load({}, js.exec_dir + 'wttr-lib.js');
const xterm = load({}, js.exec_dir + 'xterm-colors.js');
const locator = load({}, js.exec_dir + 'locator.js');
function uReplace(str) { function parseArgs() {
return str.replace(/\xE2\x9A\xA1/g, '/ '); // U+26A1 Lightning bolt const ret = {
} qs: 'https://wttr.in/?AFn',
ttl: 3600, // Seconds
function fetchWeather(addr) { };
const qs = argc > 0 ? argv.join('') : 'https://wttr.in/?AFn'; for (var n = 0; n < argc; n++) {
const http = new HTTPRequest(); const arg = parseInt(argv[n], 10);
if (addr !== undefined) http.extra_headers = { 'X-Forwarded-For': addr }; if (isNaN(arg)) {
const body = http.Get(qs); ret.qs = arg;
if (http.response_code !== 200) throw new Error('wttr.in response had status ' + http.response_code); } else {
return body; ret.ttl = arg;
}
}
return ret;
} }
function main() { function main() {
const addr = locator.getAddress(); const args = parseArgs();
const weather = fetchWeather(addr); const ansi = wttr.getWeather(args.qs, args.ttl);
const text = uReplace(weather);
const ansi = xterm.convertColors(text);
const attr = console.attributes; const attr = console.attributes;
console.clear(BG_BLACK|LIGHTGRAY); console.clear(BG_BLACK|LIGHTGRAY);
console.putmsg(ansi, P_UTF8); console.putmsg(ansi, P_UTF8);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment