-
Notifications
You must be signed in to change notification settings - Fork 10
/
aw.js
78 lines (75 loc) · 2.64 KB
/
aw.js
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const { pipe, map, fromPromise, flatten, filter } = require('callbag-basics')
const tap = require('callbag-tap') // eslint-disable-line no-unused-vars
const fetch = require('cross-fetch')
const { DateTime } = require('luxon')
const API = location => `https://dataservice.accuweather.com/forecasts/v1/hourly/12hour/${location.id}?apikey=${location.key}&details=true&metric=true`
const query = location$ => pipe(
location$,
// tap(location => console.log(`AccuWeather at ${location.name}`, API(location))),
map(location => fromPromise(
fetch(API(location))
.then(res => res.json())
.then(res => {
if (res.Code === 'ServiceUnavailable') throw Error(`The allowed number of requests has been exceeded for ${location.name}.`)
else return res
})
.then(forecast => ({ location, forecast }))
.catch((e) => {
const message = `${e.name}: ${e.message}`
console.error(message)
return message
})
)),
flatten,
filter(payload => Array.isArray(payload.forecast)),
// tap(console.debug), // DEBUG:
map(payload => {
const { location, forecast } = payload
console.log(location)
const now = DateTime.local().setZone(location.timezone)
return forecast.map(({
EpochDateTime: epoch,
DateTime: datetime,
IconPhrase: label,
Temperature: { Value: temp },
Visibility: { Value: visib },
Wind: { Speed: { Value: wind } },
WindGust: { Speed: { Value: gust } },
CloudCover: cloud,
PrecipitationProbability: precip,
RainProbability: rain,
SnowProbability: snow,
RelativeHumidity: humid,
DewPoint: { Value: dew },
Ceiling: { Value: ceiling },
IsDaylight: light,
UVIndex: uv,
}) => ({
epoch,
querydate: now.toISODate(), // date of the api call
queryhour: now.toISOTime().slice(0, 2), // hour of the api call
date: datetime.slice(0, 10), // date of the forecast
hour: datetime.slice(11, 13), // hour of the forecast
label: label
.replace(/[^\w!?]/g, '')
.toLowerCase(), // aw label of the weather
temp, // temperature in Celcius
visib, // visibility in Km
wind, // sustained wind speed in Km/h
gust, // gust speed in Km/h
cloud, // % of cloud cover
precip, // % chance of any kind of precipitation
rain, // % chance of rain
snow, // % chance of snow
humid, // relative humidity in %
dew, // dew point in Celcius
ceiling: ceiling / 1000, // loud ceiling height in Km
light, // daylight in boolean
uv, // uv index
}))
}),
// tap(console.debug), // DEBUG:
)
module.exports = {
query,
}