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

Use new modopts settings instead of args.

parent fbe4d390
No related branches found
No related tags found
No related merge requests found
...@@ -2,17 +2,35 @@ require('http.js', 'HTTPRequest'); ...@@ -2,17 +2,35 @@ require('http.js', 'HTTPRequest');
const locator = load({}, js.exec_dir + 'locator.js'); const locator = load({}, js.exec_dir + 'locator.js');
const xterm = load({}, js.exec_dir + 'xterm-colors.js'); const xterm = load({}, js.exec_dir + 'xterm-colors.js');
function loadSettings() {
const settings = load({}, 'modopts.js', 'wttr.in') || {};
if (settings.base_url === undefined) {
settings.base_url = 'https://wttr.in/';
} else if (settings.base_url.search(/\/$/) < 0) {
settings.base_url += '/';
}
if (settings.units === undefined) settings.units = '';
if (settings.view === undefined) settings.view = 'AFn';
if (settings.cache_ttl === undefined) settings.cache_ttl = 3600;
if (settings.fallback_location === undefined) {
settings.fallback_location = '';
} else {
settings.fallback_location = settings.fallback_location.replace(/\s/g, '+');
}
return settings;
}
function uReplace(str) { function uReplace(str) {
return str.replace(/\xE2\x9A\xA1/g, '\x01+\x01h\x01yZ \x01-'); // U+26A1 Lightning bolt return str.replace(/\xE2\x9A\xA1/g, '\x01+\x01h\x01yZ \x01-'); // U+26A1 Lightning bolt
} }
function getCacheName(qs, addr) { function getCacheName(url, addr) {
const cfn = format('wttr.in_%s_%s.ans', qs, addr || '').replace(/[^0-9a-z\.]+/ig, '_'); const cfn = format('wttr.in_%s%s.ans', url, addr || '').replace(/[^0-9a-z\.]+/ig, '_');
return system.temp_path + cfn; return system.temp_path + cfn;
} }
function readCache(qs, addr, ttl) { function readCache(url, addr, ttl) {
const cfn = getCacheName(qs, addr); const cfn = getCacheName(url, addr);
const f = new File(cfn); const f = new File(cfn);
if (!f.exists) return; if (!f.exists) return;
if (time() - file_date(cfn) > ttl) return; if (time() - file_date(cfn) > ttl) return;
...@@ -22,30 +40,39 @@ function readCache(qs, addr, ttl) { ...@@ -22,30 +40,39 @@ function readCache(qs, addr, ttl) {
return cache; return cache;
} }
function writeCache(qs, addr, ans) { function writeCache(url, addr, ans) {
const cfn = getCacheName(qs, addr); const cfn = getCacheName(url, addr);
const f = new File(cfn); const f = new File(cfn);
if (!f.open('w')) return; if (!f.open('w')) return;
f.write(ans); f.write(ans);
f.close(); f.close();
} }
function fetchWeather(qs, addr) { function fetchWeather(url, addr) {
const http = new HTTPRequest(); const http = new HTTPRequest();
if (addr !== undefined) http.extra_headers = { 'X-Forwarded-For': addr }; if (addr !== undefined) http.extra_headers = { 'X-Forwarded-For': addr };
const body = http.Get(qs); const body = http.Get(url);
if (http.response_code !== 200) throw new Error('wttr.in response had status ' + http.response_code); if (http.response_code !== 200) throw new Error('wttr.in response had status ' + http.response_code);
return body; return body;
} }
function getWeather(qs, ttl) { function getURL(settings, addr) {
const addr = locator.getAddress(); var url = settings.base_url;
const cachedWeather = readCache(qs, addr, ttl); if (addr === undefined) url += settings.fallback_location;
url += '?' + settings.units + settings.view;
return url;
}
function getWeather() {
const settings = loadSettings();
const addr = locator.getAddress() || settings.fallback_ip;
const url = getURL(settings, addr);
const cachedWeather = readCache(url, addr, settings.cache_ttl);
if (cachedWeather !== undefined) return cachedWeather; if (cachedWeather !== undefined) return cachedWeather;
const weather = fetchWeather(qs, addr); const weather = fetchWeather(url, addr);
const text = uReplace(weather); const text = uReplace(weather);
const ansi = xterm.convertColors(text); const ansi = xterm.convertColors(text);
writeCache(qs, addr, ansi); writeCache(url, addr, ansi);
return ansi; return ansi;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment