@@ -12,6 +12,13 @@ import { WWO_CODE, WEATHER_SYMBOL, NIGHT_WEATHER_SYMBOL } from '../../data/weath
12
12
const BATTERY_LOW = 20 ;
13
13
const WEATHER_CACHE_FOLDER = `${ GLib . get_user_cache_dir ( ) } /ags/weather` ;
14
14
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
+ }
15
22
16
23
const BatBatteryProgress = ( ) => {
17
24
const _updateProgress = ( circprog ) => { // Set circular progress value
@@ -155,15 +162,24 @@ const BatteryModule = () => Stack({
155
162
] ,
156
163
setup : ( self ) => self . poll ( 900000 , async ( self ) => {
157
164
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` )
159
166
. 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
+ ) ;
167
183
const weatherCode = weather . current_condition [ 0 ] . weatherCode ;
168
184
const weatherDesc = weather . current_condition [ 0 ] . weatherDesc [ 0 ] . value ;
169
185
const temperature = weather . current_condition [ 0 ] . temp_C ;
@@ -172,23 +188,20 @@ const BatteryModule = () => Stack({
172
188
self . children [ 0 ] . label = weatherSymbol ;
173
189
self . children [ 1 ] . label = `${ temperature } ℃ • Feels like ${ feelsLike } ℃` ;
174
190
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
+ }
192
205
} ) ,
193
206
} )
194
207
} ) ,
0 commit comments