set city manually with envvar AGS_WEATHER_CITY (#220)

This commit is contained in:
end-4
2024-02-08 01:29:25 +07:00
parent d328d935a5
commit 7c0c61fb3e
+19 -6
View File
@@ -12,6 +12,13 @@ import { WWO_CODE, WEATHER_SYMBOL, NIGHT_WEATHER_SYMBOL } from '../../data/weath
const BATTERY_LOW = 20; const BATTERY_LOW = 20;
const WEATHER_CACHE_FOLDER = `${GLib.get_user_cache_dir()}/ags/weather`; const WEATHER_CACHE_FOLDER = `${GLib.get_user_cache_dir()}/ags/weather`;
Utils.exec(`mkdir -p ${WEATHER_CACHE_FOLDER}`); Utils.exec(`mkdir -p ${WEATHER_CACHE_FOLDER}`);
let WEATHER_CITY = '';
// try to read envvar AGS_WEATHER_CITY
try {
WEATHER_CITY = GLib.getenv('AGS_WEATHER_CITY');
} catch (e) {
print(e);
}
const BatBatteryProgress = () => { const BatBatteryProgress = () => {
const _updateProgress = (circprog) => { // Set circular progress value const _updateProgress = (circprog) => { // Set circular progress value
@@ -155,11 +162,7 @@ const BatteryModule = () => Stack({
], ],
setup: (self) => self.poll(900000, async (self) => { setup: (self) => self.poll(900000, async (self) => {
const WEATHER_CACHE_PATH = WEATHER_CACHE_FOLDER + '/wttr.in.txt'; const WEATHER_CACHE_PATH = WEATHER_CACHE_FOLDER + '/wttr.in.txt';
Utils.execAsync('curl ipinfo.io') const updateWeatherForCity = (city) => execAsync(`curl https://wttr.in/${city}?format=j1`)
.then(output => {
return JSON.parse(output)['city'].toLowerCase();
})
.then((city) => execAsync(`curl https://wttr.in/${city}?format=j1`)
.then(output => { .then(output => {
const weather = JSON.parse(output); const weather = JSON.parse(output);
Utils.writeFile(JSON.stringify(weather), WEATHER_CACHE_PATH) Utils.writeFile(JSON.stringify(weather), WEATHER_CACHE_PATH)
@@ -188,7 +191,17 @@ const BatteryModule = () => Stack({
} catch (err) { } catch (err) {
print(err); print(err);
} }
})); });
if (WEATHER_CITY != '' && WEATHER_CITY != null) {
updateWeatherForCity(WEATHER_CITY);
}
else {
Utils.execAsync('curl ipinfo.io')
.then(output => {
return JSON.parse(output)['city'].toLowerCase();
})
.then(updateWeatherForCity);
}
}), }),
}) })
}), }),