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
+38 -25
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,15 +162,24 @@ 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 => { .then(output => {
return JSON.parse(output)['city'].toLowerCase(); const weather = JSON.parse(output);
}) Utils.writeFile(JSON.stringify(weather), WEATHER_CACHE_PATH)
.then((city) => execAsync(`curl https://wttr.in/${city}?format=j1`) .catch(print);
.then(output => { const weatherCode = weather.current_condition[0].weatherCode;
const weather = JSON.parse(output); const weatherDesc = weather.current_condition[0].weatherDesc[0].value;
Utils.writeFile(JSON.stringify(weather), WEATHER_CACHE_PATH) const temperature = weather.current_condition[0].temp_C;
.catch(print); const feelsLike = weather.current_condition[0].FeelsLikeC;
const weatherSymbol = WEATHER_SYMBOL[WWO_CODE[weatherCode]];
self.children[0].label = weatherSymbol;
self.children[1].label = `${temperature}℃ • Feels like ${feelsLike}`;
self.tooltipText = weatherDesc;
}).catch((err) => {
try { // Read from cache
const weather = JSON.parse(
Utils.readFile(WEATHER_CACHE_PATH)
);
const weatherCode = weather.current_condition[0].weatherCode; const weatherCode = weather.current_condition[0].weatherCode;
const weatherDesc = weather.current_condition[0].weatherDesc[0].value; const weatherDesc = weather.current_condition[0].weatherDesc[0].value;
const temperature = weather.current_condition[0].temp_C; const temperature = weather.current_condition[0].temp_C;
@@ -172,23 +188,20 @@ const BatteryModule = () => Stack({
self.children[0].label = weatherSymbol; self.children[0].label = weatherSymbol;
self.children[1].label = `${temperature}℃ • Feels like ${feelsLike}`; self.children[1].label = `${temperature}℃ • Feels like ${feelsLike}`;
self.tooltipText = weatherDesc; self.tooltipText = weatherDesc;
}).catch((err) => { } catch (err) {
try { // Read from cache print(err);
const weather = JSON.parse( }
Utils.readFile(WEATHER_CACHE_PATH) });
); if (WEATHER_CITY != '' && WEATHER_CITY != null) {
const weatherCode = weather.current_condition[0].weatherCode; updateWeatherForCity(WEATHER_CITY);
const weatherDesc = weather.current_condition[0].weatherDesc[0].value; }
const temperature = weather.current_condition[0].temp_C; else {
const feelsLike = weather.current_condition[0].FeelsLikeC; Utils.execAsync('curl ipinfo.io')
const weatherSymbol = WEATHER_SYMBOL[WWO_CODE[weatherCode]]; .then(output => {
self.children[0].label = weatherSymbol; return JSON.parse(output)['city'].toLowerCase();
self.children[1].label = `${temperature}℃ • Feels like ${feelsLike}`; })
self.tooltipText = weatherDesc; .then(updateWeatherForCity);
} catch (err) { }
print(err);
}
}));
}), }),
}) })
}), }),