Skip to content

Commit c2d3c5b

Browse files
committed
set city manually with envvar AGS_WEATHER_CITY (#220)
1 parent bfd7990 commit c2d3c5b

File tree

1 file changed

+38
-25
lines changed

1 file changed

+38
-25
lines changed

.config/ags/widgets/bar/system.js

+38-25
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ import { WWO_CODE, WEATHER_SYMBOL, NIGHT_WEATHER_SYMBOL } from '../../data/weath
1212
const BATTERY_LOW = 20;
1313
const WEATHER_CACHE_FOLDER = `${GLib.get_user_cache_dir()}/ags/weather`;
1414
Utils.exec(`mkdir -p ${WEATHER_CACHE_FOLDER}`);
15+
let WEATHER_CITY = '';
16+
// try to read envvar AGS_WEATHER_CITY
17+
try {
18+
WEATHER_CITY = GLib.getenv('AGS_WEATHER_CITY');
19+
} catch (e) {
20+
print(e);
21+
}
1522

1623
const BatBatteryProgress = () => {
1724
const _updateProgress = (circprog) => { // Set circular progress value
@@ -155,15 +162,24 @@ const BatteryModule = () => Stack({
155162
],
156163
setup: (self) => self.poll(900000, async (self) => {
157164
const WEATHER_CACHE_PATH = WEATHER_CACHE_FOLDER + '/wttr.in.txt';
158-
Utils.execAsync('curl ipinfo.io')
165+
const updateWeatherForCity = (city) => execAsync(`curl https://wttr.in/${city}?format=j1`)
159166
.then(output => {
160-
return JSON.parse(output)['city'].toLowerCase();
161-
})
162-
.then((city) => execAsync(`curl https://wttr.in/${city}?format=j1`)
163-
.then(output => {
164-
const weather = JSON.parse(output);
165-
Utils.writeFile(JSON.stringify(weather), WEATHER_CACHE_PATH)
166-
.catch(print);
167+
const weather = JSON.parse(output);
168+
Utils.writeFile(JSON.stringify(weather), WEATHER_CACHE_PATH)
169+
.catch(print);
170+
const weatherCode = weather.current_condition[0].weatherCode;
171+
const weatherDesc = weather.current_condition[0].weatherDesc[0].value;
172+
const temperature = weather.current_condition[0].temp_C;
173+
const feelsLike = weather.current_condition[0].FeelsLikeC;
174+
const weatherSymbol = WEATHER_SYMBOL[WWO_CODE[weatherCode]];
175+
self.children[0].label = weatherSymbol;
176+
self.children[1].label = `${temperature}℃ • Feels like ${feelsLike}℃`;
177+
self.tooltipText = weatherDesc;
178+
}).catch((err) => {
179+
try { // Read from cache
180+
const weather = JSON.parse(
181+
Utils.readFile(WEATHER_CACHE_PATH)
182+
);
167183
const weatherCode = weather.current_condition[0].weatherCode;
168184
const weatherDesc = weather.current_condition[0].weatherDesc[0].value;
169185
const temperature = weather.current_condition[0].temp_C;
@@ -172,23 +188,20 @@ const BatteryModule = () => Stack({
172188
self.children[0].label = weatherSymbol;
173189
self.children[1].label = `${temperature}℃ • Feels like ${feelsLike}℃`;
174190
self.tooltipText = weatherDesc;
175-
}).catch((err) => {
176-
try { // Read from cache
177-
const weather = JSON.parse(
178-
Utils.readFile(WEATHER_CACHE_PATH)
179-
);
180-
const weatherCode = weather.current_condition[0].weatherCode;
181-
const weatherDesc = weather.current_condition[0].weatherDesc[0].value;
182-
const temperature = weather.current_condition[0].temp_C;
183-
const feelsLike = weather.current_condition[0].FeelsLikeC;
184-
const weatherSymbol = WEATHER_SYMBOL[WWO_CODE[weatherCode]];
185-
self.children[0].label = weatherSymbol;
186-
self.children[1].label = `${temperature}℃ • Feels like ${feelsLike}℃`;
187-
self.tooltipText = weatherDesc;
188-
} catch (err) {
189-
print(err);
190-
}
191-
}));
191+
} catch (err) {
192+
print(err);
193+
}
194+
});
195+
if (WEATHER_CITY != '' && WEATHER_CITY != null) {
196+
updateWeatherForCity(WEATHER_CITY);
197+
}
198+
else {
199+
Utils.execAsync('curl ipinfo.io')
200+
.then(output => {
201+
return JSON.parse(output)['city'].toLowerCase();
202+
})
203+
.then(updateWeatherForCity);
204+
}
192205
}),
193206
})
194207
}),

0 commit comments

Comments
 (0)