diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..16ff240 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,3 @@ +[*.js] +indent_style = space +indent_size = 4 diff --git a/scripts/sync-rss.js b/scripts/sync-rss.js index 285efcb..38311fb 100644 --- a/scripts/sync-rss.js +++ b/scripts/sync-rss.js @@ -1,53 +1,49 @@ 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( - /train[- ]v(?:ers[eu]s|s?\.?)?[- ](?:auto|vehicle|car)/i, - ); + 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 ?? + ""; - let content = item.contentSnippet; + const content = title + "\n\n" + description; - 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(); - } + const result = content.match( + /train[- ]v(?:ers[eu]s|s?\.?)?[- ](?:auto|vehicle|car)/i, + ); - db.query( - `INSERT INTO service_alerts ( - id, - date, - content, - is_collision - ) VALUES ($id, $date, $content, $isCollision) ON CONFLICT DO NOTHING`, - ).run({ - $id: Bun.hash( - item.isoDate + item.contentSnippet, - ).toString(), - $date: item.isoDate, - $content: content, - $isCollision: result !== null, - }); - } + db.query( + `INSERT INTO service_alerts ( + id, + date, + content, + is_collision + ) VALUES ($id, $date, $content, $isCollision) ON CONFLICT DO NOTHING`, + ).run({ + $id: alert.uuid, + $date: new Date( + alert.active_period[0].start * 1000, + ).toISOString(), + $content: content, + $isCollision: result !== null, + }); } }, };