aboutsummaryrefslogtreecommitdiff
path: root/.config/ags/service/weather.ts
blob: 14f2df266be7080cf137417f66bf88633330ebc6 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import options from "options"

const { interval, key, cities, unit } = options.datemenu.weather

class Weather extends Service {
    static {
        Service.register(this, {}, {
            "forecasts": ["jsobject"],
        })
    }

    #forecasts: Forecast[] = []
    get forecasts() { return this.#forecasts }

    async #fetch(placeid: number) {
        const url = "https://api.openweathermap.org/data/2.5/forecast"
        const res = await Utils.fetch(url, {
            params: {
                id: placeid,
                appid: key.value,
                untis: unit.value,
            },
        })
        return await res.json()
    }

    constructor() {
        super()
        if (!key.value)
            return this

        Utils.interval(interval.value, () => {
            Promise.all(cities.value.map(this.#fetch)).then(forecasts => {
                this.#forecasts = forecasts as Forecast[]
                this.changed("forecasts")
            })
        })
    }
}

export default new Weather

type Forecast = {
    city: {
        name: string,
    }
    list: Array<{
        dt: number
        main: {
            temp: number
            feels_like: number
        },
        weather: Array<{
            main: string,
            description: string,
            icon: string,
        }>
    }>
}