weather: use xmlhttprequest
Remove curl dependency Also add buffer period so it doesnt spam api requests
This commit is contained in:
parent
3e6d4381f3
commit
0d7804f677
3 changed files with 50 additions and 26 deletions
|
|
@ -45,7 +45,6 @@ Dependencies:
|
||||||
- [`networkmanager`](https://networkmanager.dev)
|
- [`networkmanager`](https://networkmanager.dev)
|
||||||
- [`lm-sensors`](https://github.com/lm-sensors/lm-sensors)
|
- [`lm-sensors`](https://github.com/lm-sensors/lm-sensors)
|
||||||
- [`fish`](https://github.com/fish-shell/fish-shell)
|
- [`fish`](https://github.com/fish-shell/fish-shell)
|
||||||
- [`curl`](https://github.com/curl/curl)
|
|
||||||
- [`aubio`](https://github.com/aubio/aubio)
|
- [`aubio`](https://github.com/aubio/aubio)
|
||||||
- [`libpipewire`](https://pipewire.org)
|
- [`libpipewire`](https://pipewire.org)
|
||||||
- `glibc`
|
- `glibc`
|
||||||
|
|
|
||||||
36
services/Requests.qml
Normal file
36
services/Requests.qml
Normal file
|
|
@ -0,0 +1,36 @@
|
||||||
|
pragma Singleton
|
||||||
|
|
||||||
|
import "root:/config"
|
||||||
|
import "root:/utils"
|
||||||
|
import Quickshell
|
||||||
|
|
||||||
|
Singleton {
|
||||||
|
id: root
|
||||||
|
|
||||||
|
function get(url: string, callback: var): void {
|
||||||
|
const xhr = new XMLHttpRequest();
|
||||||
|
|
||||||
|
const cleanup = () => {
|
||||||
|
xhr.abort();
|
||||||
|
xhr.onreadystatechange = null;
|
||||||
|
xhr.onerror = null;
|
||||||
|
};
|
||||||
|
|
||||||
|
xhr.open("GET", url, true);
|
||||||
|
xhr.onreadystatechange = () => {
|
||||||
|
if (xhr.readyState === XMLHttpRequest.DONE) {
|
||||||
|
if (xhr.status === 200)
|
||||||
|
callback(xhr.responseText);
|
||||||
|
else
|
||||||
|
console.warn(`[REQUESTS] GET request to ${url} failed with status ${xhr.status}`);
|
||||||
|
cleanup();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
xhr.onerror = () => {
|
||||||
|
console.warn(`[REQUESTS] GET request to ${url} failed`);
|
||||||
|
cleanup();
|
||||||
|
};
|
||||||
|
|
||||||
|
xhr.send();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -3,7 +3,6 @@ pragma Singleton
|
||||||
import "root:/config"
|
import "root:/config"
|
||||||
import "root:/utils"
|
import "root:/utils"
|
||||||
import Quickshell
|
import Quickshell
|
||||||
import Quickshell.Io
|
|
||||||
import QtQuick
|
import QtQuick
|
||||||
|
|
||||||
Singleton {
|
Singleton {
|
||||||
|
|
@ -17,33 +16,23 @@ Singleton {
|
||||||
function reload(): void {
|
function reload(): void {
|
||||||
if (Config.dashboard.weatherLocation)
|
if (Config.dashboard.weatherLocation)
|
||||||
loc = Config.dashboard.weatherLocation;
|
loc = Config.dashboard.weatherLocation;
|
||||||
else
|
else if (!loc || timer.elapsed() > 900)
|
||||||
ipProc.running = true;
|
Requests.get("https://ipinfo.io/json", text => {
|
||||||
|
loc = JSON.parse(text).loc ?? "";
|
||||||
|
timer.restart();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
onLocChanged: wttrProc.running = true
|
onLocChanged: Requests.get(`https://wttr.in/${loc}?format=j1`, text => {
|
||||||
|
const json = JSON.parse(text).current_condition[0];
|
||||||
|
icon = Icons.getWeatherIcon(json.weatherCode);
|
||||||
|
description = json.weatherDesc[0].value;
|
||||||
|
temperature = parseFloat(json.temp_C);
|
||||||
|
})
|
||||||
|
|
||||||
Component.onCompleted: reload()
|
Component.onCompleted: reload()
|
||||||
|
|
||||||
Process {
|
ElapsedTimer {
|
||||||
id: ipProc
|
id: timer
|
||||||
|
|
||||||
command: ["curl", "ipinfo.io"]
|
|
||||||
stdout: StdioCollector {
|
|
||||||
onStreamFinished: root.loc = JSON.parse(text).loc ?? ""
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Process {
|
|
||||||
id: wttrProc
|
|
||||||
|
|
||||||
command: ["curl", `https://wttr.in/${root.loc}?format=j1`]
|
|
||||||
stdout: StdioCollector {
|
|
||||||
onStreamFinished: {
|
|
||||||
const json = JSON.parse(text).current_condition[0];
|
|
||||||
root.icon = Icons.getWeatherIcon(json.weatherCode);
|
|
||||||
root.description = json.weatherDesc[0].value;
|
|
||||||
root.temperature = parseFloat(json.temp_C);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue