diff options
| author | 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> | 2025-04-09 00:14:48 +1000 |
|---|---|---|
| committer | 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> | 2025-04-09 00:14:48 +1000 |
| commit | a6f26b3d379029660a651d5fff4822b0044cc49f (patch) | |
| tree | 39f7ff864aa937cafcc2cac6dd3ec46b83342153 /src/services/weather.ts | |
| parent | updates: fix cache file extension (diff) | |
| download | caelestia-shell-a6f26b3d379029660a651d5fff4822b0044cc49f.tar.gz caelestia-shell-a6f26b3d379029660a651d5fff4822b0044cc49f.tar.bz2 caelestia-shell-a6f26b3d379029660a651d5fff4822b0044cc49f.zip | |
weather: store api key directly in config
Also only notify once when no api key for both weather and news
Diffstat (limited to 'src/services/weather.ts')
| -rw-r--r-- | src/services/weather.ts | 54 |
1 files changed, 24 insertions, 30 deletions
diff --git a/src/services/weather.ts b/src/services/weather.ts index 3d3c5f0..d51e7fc 100644 --- a/src/services/weather.ts +++ b/src/services/weather.ts @@ -217,8 +217,8 @@ export default class Weather extends GObject.Object { } readonly #cache: string = `${CACHE}/weather.json`; + #notified = false; - #key: string = ""; #data: WeatherData = DEFAULT; #interval: Time | null = null; @@ -322,15 +322,28 @@ export default class Weather extends GObject.Object { async getWeather() { const location = config.location || JSON.parse(await execAsync("curl ipinfo.io")).city; - return JSON.parse( - await execAsync([ - "curl", - `https://api.weatherapi.com/v1/forecast.json?key=${this.#key}&q=${location}&days=1&aqi=no&alerts=no`, - ]) - ); + const opts = `key=${config.apiKey.get()}&q=${location}&days=1&aqi=no&alerts=no`; + const url = `https://api.weatherapi.com/v1/forecast.json?${opts}`; + return JSON.parse(await execAsync(["curl", url])); } async updateWeather() { + if (!config.apiKey.get()) { + if (!this.#notified) { + notify({ + summary: "Weather API key required", + body: `A weather API key is required to get weather data. Get one from https://www.weatherapi.com.`, + icon: "dialog-warning-symbolic", + urgency: "critical", + actions: { + "Get API key": () => execAsync(`app2unit -O 'https://www.weatherapi.com'`).catch(print), + }, + }); + this.#notified = true; + } + return; + } + if (GLib.file_test(this.#cache, GLib.FileTest.EXISTS)) { const cache = await readFileAsync(this.#cache); const cache_data: WeatherData = JSON.parse(cache); @@ -354,32 +367,13 @@ export default class Weather extends GObject.Object { this.#notify(); } - #init(first: boolean) { - if (GLib.file_test(config.key.get(), GLib.FileTest.EXISTS)) - readFileAsync(config.key.get()) - .then(k => { - this.#key = k.trim(); - this.updateWeather().catch(console.error); - this.#interval = interval(config.interval.get(), () => this.updateWeather().catch(console.error)); - }) - .catch(console.error); - else if (first) - notify({ - summary: "Weather API key required", - body: `A weather API key is required to get weather data. Get one from https://www.weatherapi.com and put it in ${config.key}.`, - icon: "dialog-warning-symbolic", - urgency: "critical", - actions: { - "Get API key": () => execAsync(`app2unit -O 'https://www.weatherapi.com'`).catch(print), - }, - }); - } - constructor() { super(); - this.#init(true); - config.key.subscribe(() => this.#init(false)); + this.updateWeather().catch(console.error); + this.#interval = interval(config.interval.get(), () => this.updateWeather().catch(console.error)); + + config.apiKey.subscribe(() => this.updateWeather()); config.interval.subscribe(i => { this.#interval?.cancel(); |