mirror of
https://github.com/cjdenio/trax-accidents.git
synced 2026-12-01 03:06:19 +00:00
update for new service alerts platform
This commit is contained in:
2 files changed
+27
-28
No files matched your search
@@ -0,0 +1,3 @@
|
||||
[*.js]
|
||||
indent_style = space
|
||||
indent_size = 4
|
||||
+24
-28
@@ -1,37 +1,34 @@
|
||||
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 {
|
||||
async scheduled(controller) {
|
||||
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 feed = await parser.parseURL(url);
|
||||
const railAlerts = activeAlerts.filter((alert) =>
|
||||
alert.informed_entity.some(
|
||||
(e) =>
|
||||
typeof e.route_short_name === "string" &&
|
||||
e.route_short_name.startsWith("7"),
|
||||
),
|
||||
);
|
||||
|
||||
for (const item of feed.items) {
|
||||
const result = item.contentSnippet.match(
|
||||
for (const alert of railAlerts) {
|
||||
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,
|
||||
);
|
||||
|
||||
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(
|
||||
`INSERT INTO service_alerts (
|
||||
id,
|
||||
@@ -40,14 +37,13 @@ export default {
|
||||
is_collision
|
||||
) VALUES ($id, $date, $content, $isCollision) ON CONFLICT DO NOTHING`,
|
||||
).run({
|
||||
$id: Bun.hash(
|
||||
item.isoDate + item.contentSnippet,
|
||||
).toString(),
|
||||
$date: item.isoDate,
|
||||
$id: alert.uuid,
|
||||
$date: new Date(
|
||||
alert.active_period[0].start * 1000,
|
||||
).toISOString(),
|
||||
$content: content,
|
||||
$isCollision: result !== null,
|
||||
});
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
Reference in new issue
Block a user