update for new service alerts platform

This commit is contained in:
cjdenio committed 2026-10-28 20:41:51 -06:00
1 parent efa5e67486
commit 6ff5ba2d3f
2 files changed
+27 -28

No files matched your search

+3
View File
@@ -0,0 +1,3 @@
[*.js]
indent_style = space
indent_size = 4
+24 -28
View File
@@ -1,37 +1,34 @@
import { Database } from "bun:sqlite"; import { Database } from "bun:sqlite";
import Parser from "rss-parser";
const URLS = [
"https://content.govdelivery.com/accounts/UTTA/widgets/UTTA_WIDGET_3.rss", // blue line
"https://content.govdelivery.com/accounts/UTTA/widgets/UTTA_WIDGET_4.rss", // red line
"https://content.govdelivery.com/accounts/UTTA/widgets/UTTA_WIDGET_5.rss", // green line
"https://content.govdelivery.com/accounts/UTTA/widgets/UTTA_WIDGET_11.rss", // s-line
"https://content.govdelivery.com/accounts/UTTA/widgets/UTTA_WIDGET_7.rss", // frontrunner
];
export default { export default {
async scheduled(controller) { async scheduled(controller) {
const db = new Database(import.meta.dir + "/../database.db"); const db = new Database(import.meta.dir + "/../database.db");
const parser = new Parser(); const activeAlerts = await fetch(
"https://api.prod.simplifytransit.com/public/alerts/uta",
).then((r) => r.json());
for (const url of URLS) { const railAlerts = activeAlerts.filter((alert) =>
const feed = await parser.parseURL(url); alert.informed_entity.some(
(e) =>
typeof e.route_short_name === "string" &&
e.route_short_name.startsWith("7"),
),
);
for (const item of feed.items) { for (const alert of railAlerts) {
const result = item.contentSnippet.match( const title =
alert.header_text.find((t) => t.language === "en")?.text ?? "";
const description =
alert.description_text.find((t) => t.language === "en")?.text ??
"";
const content = title + "\n\n" + description;
const result = content.match(
/train[- ]v(?:ers[eu]s|s?\.?)?[- ](?:auto|vehicle|car)/i, /train[- ]v(?:ers[eu]s|s?\.?)?[- ](?:auto|vehicle|car)/i,
); );
let content = item.contentSnippet;
const footerIndex = content.indexOf(
"You have received this message because you are subscribed to UTA Service Alerts",
);
if (footerIndex != -1) {
content = content.slice(0, footerIndex).trim();
}
db.query( db.query(
`INSERT INTO service_alerts ( `INSERT INTO service_alerts (
id, id,
@@ -40,14 +37,13 @@ export default {
is_collision is_collision
) VALUES ($id, $date, $content, $isCollision) ON CONFLICT DO NOTHING`, ) VALUES ($id, $date, $content, $isCollision) ON CONFLICT DO NOTHING`,
).run({ ).run({
$id: Bun.hash( $id: alert.uuid,
item.isoDate + item.contentSnippet, $date: new Date(
).toString(), alert.active_period[0].start * 1000,
$date: item.isoDate, ).toISOString(),
$content: content, $content: content,
$isCollision: result !== null, $isCollision: result !== null,
}); });
} }
}
}, },
}; };