mirror of
https://github.com/cjdenio/trax-accidents.git
synced 2026-08-05 02:46:34 +00:00
Initial commit
This commit is contained in:
Binary file not shown.
@@ -0,0 +1,40 @@
|
||||
import { simpleParser } from "mailparser";
|
||||
import { readdir } from "node:fs/promises";
|
||||
import { Database } from "bun:sqlite";
|
||||
|
||||
const db = new Database("database.db");
|
||||
|
||||
const emails = await readdir("uta-emails/");
|
||||
|
||||
for (const filename of emails) {
|
||||
const data = await simpleParser(
|
||||
await Bun.file(`uta-emails/${filename}`).text(),
|
||||
);
|
||||
|
||||
const result = data.text.match(
|
||||
/train[- ]v(?:ersus|s?\.?)?[- ](?:auto|vehicle)/i,
|
||||
);
|
||||
|
||||
let content = data.text;
|
||||
|
||||
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,
|
||||
date,
|
||||
content,
|
||||
is_collision
|
||||
) VALUES ($id, $date, $content, $isCollision) ON CONFLICT DO NOTHING`,
|
||||
).run({
|
||||
$id: Bun.hash(data.date.toISOString() + data.text).toString(),
|
||||
$date: data.date.toISOString(),
|
||||
$content: content,
|
||||
$isCollision: result !== null,
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
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();
|
||||
|
||||
for (const url of URLS) {
|
||||
const feed = await parser.parseURL(url);
|
||||
|
||||
for (const item of feed.items) {
|
||||
const result = item.contentSnippet.match(
|
||||
/train[- ]v(?:ersus|s?\.?)?[- ](?:auto|vehicle)/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,
|
||||
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,
|
||||
});
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user