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
+38
-39
No files matched your search
@@ -0,0 +1,3 @@
|
|||||||
|
[*.js]
|
||||||
|
indent_style = space
|
||||||
|
indent_size = 4
|
||||||
+35
-39
@@ -1,53 +1,49 @@
|
|||||||
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 =
|
||||||
/train[- ]v(?:ers[eu]s|s?\.?)?[- ](?:auto|vehicle|car)/i,
|
alert.header_text.find((t) => t.language === "en")?.text ?? "";
|
||||||
);
|
const description =
|
||||||
|
alert.description_text.find((t) => t.language === "en")?.text ??
|
||||||
|
"";
|
||||||
|
|
||||||
let content = item.contentSnippet;
|
const content = title + "\n\n" + description;
|
||||||
|
|
||||||
const footerIndex = content.indexOf(
|
const result = content.match(
|
||||||
"You have received this message because you are subscribed to UTA Service Alerts",
|
/train[- ]v(?:ers[eu]s|s?\.?)?[- ](?:auto|vehicle|car)/i,
|
||||||
);
|
);
|
||||||
if (footerIndex != -1) {
|
|
||||||
content = content.slice(0, footerIndex).trim();
|
|
||||||
}
|
|
||||||
|
|
||||||
db.query(
|
db.query(
|
||||||
`INSERT INTO service_alerts (
|
`INSERT INTO service_alerts (
|
||||||
id,
|
id,
|
||||||
date,
|
date,
|
||||||
content,
|
content,
|
||||||
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,
|
||||||
});
|
});
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
Reference in new issue
Block a user