summaryrefslogtreecommitdiff
path: root/services/Weather.qml
blob: 38750aee20814d1ba1ca38f59614f9f923cd1e5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
pragma Singleton

import qs.config
import qs.utils
import Quickshell
import QtQuick

Singleton {
    id: root

    property string loc
    property string icon
    property string description
    property string tempC: "0°C"
    property string tempF: "0°F"

    function reload(): void {
        if (Config.services.weatherLocation)
            loc = Config.services.weatherLocation;
        else if (!loc || timer.elapsed() > 900)
            Requests.get("https://ipinfo.io/json", text => {
                loc = JSON.parse(text).loc ?? "";
                timer.restart();
            });
    }

    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;
        tempC = `${parseFloat(json.temp_C)}°C`;
        tempF = `${parseFloat(json.temp_F)}°F`;
    })

    Component.onCompleted: reload()

    ElapsedTimer {
        id: timer
    }
}