Skip to content

Commit

Permalink
set city manually with envvar AGS_WEATHER_CITY (#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
end-4 committed Feb 7, 2024
1 parent d328d93 commit 7c0c61f
Showing 1 changed file with 38 additions and 25 deletions.
63 changes: 38 additions & 25 deletions .config/ags/widgets/bar/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ import { WWO_CODE, WEATHER_SYMBOL, NIGHT_WEATHER_SYMBOL } from '../../data/weath
const BATTERY_LOW = 20;
const WEATHER_CACHE_FOLDER = `${GLib.get_user_cache_dir()}/ags/weather`;
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 _updateProgress = (circprog) => { // Set circular progress value
Expand Down Expand Up @@ -155,15 +162,24 @@ const BatteryModule = () => Stack({
],
setup: (self) => self.poll(900000, async (self) => {
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 => {
const weather = JSON.parse(output);
Utils.writeFile(JSON.stringify(weather), WEATHER_CACHE_PATH)
.catch(print);
const weather = JSON.parse(output);
Utils.writeFile(JSON.stringify(weather), WEATHER_CACHE_PATH)
.catch(print);
const weatherCode = weather.current_condition[0].weatherCode;
const weatherDesc = weather.current_condition[0].weatherDesc[0].value;
const temperature = weather.current_condition[0].temp_C;
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 weatherDesc = weather.current_condition[0].weatherDesc[0].value;
const temperature = weather.current_condition[0].temp_C;
Expand All @@ -172,23 +188,20 @@ const BatteryModule = () => Stack({
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 weatherDesc = weather.current_condition[0].weatherDesc[0].value;
const temperature = weather.current_condition[0].temp_C;
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) {
print(err);
}
}));
} catch (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);
}
}),
})
}),
Expand Down

0 comments on commit 7c0c61f

Please sign in to comment.