summaryrefslogtreecommitdiff
path: root/services/Weather.qml
blob: 4f53d0bf1bc9e142f7e53dfcd77824afd568a3c1 (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
41
42
43
44
pragma Singleton

import "root:/utils"
import Quickshell
import Quickshell.Io

Singleton {
    id: root

    property string loc
    property string icon
    property string description
    property real temperature

    function reload(): void {
        wttrProc.running = true;
    }

    onLocChanged: wttrProc.running = true

    Process {
        id: ipProc

        running: true
        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);
            }
        }
    }
}