-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
172 lines (160 loc) · 5.15 KB
/
index.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
const {
fill,
filter,
flatten,
forEach,
keys,
map,
range,
reduce,
values
} = require('arare')
const {
pipe
} = require('callbag-basics')
const {
flattenObj,
run
} = require('./utils')
const CB = {
operate: require('callbag-operate'),
subscribe: require('callbag-subscribe'),
tap: require('callbag-tap'),
timer: require('callbag-date-timer'),
...require('callbag-basics')
}
const JSONDB = require('node-json-db').JsonDB
const {
DateTime
} = require('luxon')
const aw = require('./aw')
const defaultModel = require('./model-ajstewart')
const pogo = require('./pogo')
require('./server')
const Discord = require('discord.js')
run(async () => {
console.log('Castform is running')
// Load config to check
const configDB = new JSONDB('config').getData('/')
// Setup callbags
pipe(
configDB,
keys,
filter(key => !configDB[key].disabled),
forEach(key => {
const location = configDB[key]
const model = location.model ? require(`./model-${location.model}`) : defaultModel
const webhookClients = location.webhooks.map(({id, token}) => new Discord.WebhookClient(id, token))
// forecasts
const forecast = CB.operate(
CB.map(_ => location),
aw.query,
// CB.tap(console.debug), // DEBUG:
CB.tap(weathers => {
console.info({
timestamp: DateTime.local().setZone(location.timezone).toISO(),
info: 'predictions',
location: key,
payload: weathers.map(({
date,
hour,
label
}) => ({
[`${date}T${hour}`]: label
})).reduce(...flattenObj),
})
const awDB = new JSONDB(`data/aw/${key}/${weathers[0].querydate}`, true, true)
awDB.push(`/${weathers[0].queryhour}`, weathers, true)
if (location.model) {
const modelDB = new JSONDB(`data/model/${key}/${weathers[0].querydate}`, true, true)
modelDB.push(`/${weathers[0].queryhour}`, weathers.map(weather => ({
querydate: weather.querydate,
queryhour: weather.queryhour,
date: weather.date,
hour: weather.hour,
model: model(weather)
})), true)
}
}),
)
// reports
const report = CB.operate(
CB.map(_ => DateTime.local().setZone(location.timezone).startOf('hour')),
CB.map(now => pipe(
[-1, 0],
map(x => now.plus({
day: x
}).toISODate()),
map(dt => values(new JSONDB(`data/aw/${key}/${dt}`).getData('/'))),
flatten,
reduce(
(predictions, weather) => {
const queryhour = DateTime.fromISO(`${weather.querydate}T${weather.queryhour}`, {
zone: location.timezone
})
const hour = DateTime.fromISO(`${weather.date}T${weather.hour}`, {
zone: location.timezone
})
const toForecast = hour.diff(now).as('hour') - 1
const fromQuery = now.diff(queryhour).as('hour')
if (toForecast >= 0 && toForecast < 12) {
predictions[toForecast].forecasts[fromQuery] = {
queryhour,
weather: model(weather)
}
}
return predictions
},
range(0, 12, 1).map(x => ({
hour: now.plus({
hour: x + 1
}),
forecasts: fill(12 - x, undefined)
}))
)
)),
// CB.tap(console.debug), // DEBUG:
CB.map(predictions => {
const now = DateTime.local().setZone(location.timezone).startOf('hour')
const clocks = '🕛 🕐 🕑 🕒 🕓 🕔 🕕 🕖 🕗 🕘 🕙 🕚'.split(' ')
const report = [
`**${location.name}** \`${now.toISODate()}T${now.toISOTime().slice(0, 2)}\``,
` ${range(0, 12, 1).map(x => now.minus({ hours: x }).hour % 12).map(hour => clocks[hour]).join('')}`,
...predictions
.map(prediction => `\`${prediction.hour.toISOTime().slice(0, 2)}\` ${
prediction.forecasts
.map(forecast => pogo.labelEmoteMap[forecast ? forecast.weather.dominant : 'none'])
.join('')
}`),
'—'
]
return report
}),
CB.tap(report => {
console.info({
timestamp: DateTime.local().setZone(location.timezone).toISO(),
info: 'report',
location: key,
payload: report
})
webhookClients.forEach(webhookClient => webhookClient.send(report.join('\n')))
}),
)
// run
pipe(
CB.timer(DateTime.fromObject({
hour: 0,
minute: location.minute || 0,
zone: location.timezone
}).toJSDate(), 60 * 60 * 1000),
// CB.timer(DateTime.local().plus({ seconds: 5 }).toJSDate(), 60 * 60 * 1000), // DEBUG:
forecast,
report,
CB.subscribe({
complete: () => console.log('done'),
error: console.error
})
)
})
)
})