-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.ts
52 lines (44 loc) · 1.32 KB
/
engine.ts
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
import { debug } from '@actions/core'
import moment from 'moment'
import fetch from 'node-fetch'
export async function getForecast(lat: string | number, lng: string | number): Promise<string | undefined> {
interface Rsp1 {
properties: {
gridId: string
gridX: number
gridY: number
forecast: string
},
status?: number
}
interface Rsp2 {
properties: {
periods: Period[]
},
status: number
}
interface Period {
number: number
startTime: string
endTime: string
detailedForecast: string
}
const rsp1 = await fetch(`https://api.weather.gov/points/${lat},${lng}`).then(x => x.json()) as Rsp1
if (!(rsp1.status === undefined || rsp1.status == 200)) {
throw new Error(JSON.stringify(rsp1, null, 2))
}
const rsp2 = await fetch(rsp1.properties.forecast).then(x => x.json()) as Rsp2
if (!(rsp2.status === undefined || rsp2.status == 200)) {
throw new Error(JSON.stringify(rsp1, null, 2))
}
debug(`out: ${JSON.stringify(rsp2, null, 2)}`)
const noon = moment().startOf('day').hour(12)
const day = (period: Period) => {
const start = moment(period.startTime)
const end = moment(period.endTime)
if (start.isBefore(noon) && end.isAfter(noon)) {
return period
}
}
return rsp2.properties.periods.find(day)?.detailedForecast
}