-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambdaAtproto.ts
More file actions
248 lines (225 loc) · 5.61 KB
/
lambdaAtproto.ts
File metadata and controls
248 lines (225 loc) · 5.61 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import type { AtpAgent } from "@atproto/api";
import type { Context, ScheduledEvent } from "aws-lambda";
import { DateTime, Duration } from "luxon";
import pino from "pino";
import {
getAtprotoAgent,
type Link,
postToAtproto,
type StrongPostRef,
} from "./lib/atproto";
import {
buildDailySummaryPostContent,
type SummaryPostTextSubstitutions,
} from "./lib/birdObservations/buildBirdObservationSummaryPost";
import { fetchDailyCount as fetchDailyCountFromBirdWeatherApi } from "./lib/birdWeather";
import { assertedEnvVar } from "./lib/configTools";
import {
type BirdRecord,
fetchDailyCount as fetchDailyCountFromHaikuboxApi,
} from "./lib/haiku";
import {
type AmbientWeatherApiConfig,
buildWeatherSummaryForDay,
} from "./lib/weather";
type LambdaConfig = {
blueskyUsername: string;
blueskyPassword: string;
blueskyServerBaseUrl: string;
haikuSerialNumber: string;
haikuBaseUrl: string;
birdWeatherStationId: string;
birdWeatherBaseUrl: string;
AWNBaseUrl: string;
AWNApiKey: string;
AWNApplicationKey: string;
AWNDeviceMac: string;
latitude: number;
longitude: number;
};
function getConfigFromEnv(): LambdaConfig {
const blueskyUsername = assertedEnvVar("BLUESKY_USERNAME");
const blueskyPassword = assertedEnvVar("BLUESKY_PASSWORD");
const blueskyServerBaseUrl = assertedEnvVar("BLUESKY_BASE_URL");
const haikuSerialNumber = assertedEnvVar("HAIKU_SERIAL_NUMBER");
const haikuBaseUrl = assertedEnvVar("HAIKU_BASE_URL");
const birdWeatherStationId = assertedEnvVar("BIRDWEATHER_STATION_ID");
const birdWeatherBaseUrl = assertedEnvVar("BIRDWEATHER_BASE_URL");
const AWNBaseUrl = assertedEnvVar("AWN_BASE_URL");
const AWNApiKey = assertedEnvVar("AWN_API_KEY");
const AWNApplicationKey = assertedEnvVar("AWN_APPLICATION_KEY");
const AWNDeviceMac = assertedEnvVar("AWN_DEVICE_MAC");
const latitude = Number(assertedEnvVar("SITE_LATITUDE"));
const longitude = Number(assertedEnvVar("SITE_LONGITUDE"));
return {
blueskyUsername,
blueskyPassword,
blueskyServerBaseUrl,
haikuSerialNumber,
haikuBaseUrl,
birdWeatherStationId,
birdWeatherBaseUrl,
AWNBaseUrl,
AWNApiKey,
AWNApplicationKey,
AWNDeviceMac,
latitude,
longitude,
};
}
async function executeWithConfig(config: LambdaConfig): Promise<void> {
const {
blueskyUsername,
blueskyPassword,
blueskyServerBaseUrl,
haikuSerialNumber,
haikuBaseUrl,
birdWeatherStationId,
birdWeatherBaseUrl,
AWNBaseUrl,
AWNApiKey,
AWNApplicationKey,
AWNDeviceMac,
latitude,
longitude,
} = config;
// Post setup
const when = DateTime.now().minus(Duration.fromObject({ days: 1 }));
const dateString = when.toFormat("yyyy-MM-dd");
const logger = pino({});
const client = await getAtprotoAgent(
blueskyServerBaseUrl,
blueskyUsername,
blueskyPassword,
logger,
);
// Birdweather Post Generation
const bwBirds = await fetchDailyCountFromBirdWeatherApi(
birdWeatherBaseUrl,
birdWeatherStationId,
dateString,
);
const bwStatus = await postStatusFromBirdList(
bwBirds ?? [],
logger,
dateString,
"BirdWeather PUC",
"#BirdWeather",
client,
);
// Haikubox Post Generation
const haikuBirds = await fetchDailyCountFromHaikuboxApi(
haikuBaseUrl,
haikuSerialNumber,
dateString,
);
const haikuboxStatus = await postStatusFromBirdList(
haikuBirds ?? [],
logger,
dateString,
"Haikubox",
"#Haikubox",
client,
bwStatus,
);
void haikuboxStatus;
const ambientWeatherConfig: AmbientWeatherApiConfig = {
apiBaseUrl: AWNBaseUrl,
apiKey: AWNApiKey,
applicationKey: AWNApplicationKey,
deviceMac: AWNDeviceMac,
};
const weatherSummary = await buildWeatherSummaryForDay(
ambientWeatherConfig,
{ latitude, longitude },
when.toJSDate(),
);
const weatherStatus = await postToAtproto(
client,
weatherSummary,
bwStatus,
undefined,
undefined,
logger,
);
logger.info(
`Posted weather status to ${weatherStatus.uri} / ${weatherStatus.cid}`,
);
}
/**
* Build & post text & image from provided data, ie one song monitor's daily output
*
* @param birds
* @param logger
* @param dateString
* @param sourceName
* @param sourceTag Including #
* @param client
* @param replyRef
*/
async function postStatusFromBirdList(
birds: BirdRecord[],
logger: pino.Logger,
dateString: string,
sourceName: string,
sourceTag: string,
client: AtpAgent,
replyRef?: StrongPostRef,
): Promise<StrongPostRef> {
const links: Link[] = [
{
uri: "https://bsky.app/profile/did:plc:jsjgrbio76yz7zzch5fsasox/post/3mb356jnlrs2c", // FIXME parameterize
text: "caveat",
},
];
const shortLocation = `(NE MA)`; // FIXME move this to config
const caveatText = `\n\n ^ See caveat`;
const maxPostLength = 300;
const maxBirds = 10;
const minObservationCount = 10;
const textSubstitutions: SummaryPostTextSubstitutions = {
shortLocation,
dateString,
sourceName,
sourceTag,
caveatText,
};
const { text, imageData } = buildDailySummaryPostContent(
birds,
minObservationCount,
maxBirds,
maxPostLength,
textSubstitutions,
logger,
);
const birdsStatus = await postToAtproto(
client,
text,
replyRef,
links,
imageData,
logger,
);
logger.info(
`Posted ${sourceName} bird list to ${birdsStatus.uri} / ${birdsStatus.cid}`,
);
return birdsStatus;
}
/**
* Handler used by AWS to execute the lambda
*
* Should just build config then execute the main function, to make CLI usage simpler
* @param _event
* @param _context
*/
export const handler = async (
_event: ScheduledEvent,
_context: Context,
): Promise<void> => {
void _event;
void _context;
// Environmental setup
const configFromEnv = getConfigFromEnv();
await executeWithConfig(configFromEnv);
};
export { executeWithConfig, type LambdaConfig };