From 2c138b23c53b35fd917ea1eabfce15c4ee718f4f Mon Sep 17 00:00:00 2001 From: Lea Date: Sun, 15 Oct 2023 13:29:44 +0200 Subject: [PATCH] we do a little trolling --- .gitignore | 1 + index.mjs | 128 +++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 5 ++ pnpm-lock.yaml | 16 +++++++ run.sh | 5 ++ 5 files changed, 155 insertions(+) create mode 100644 .gitignore create mode 100644 index.mjs create mode 100644 package.json create mode 100644 pnpm-lock.yaml create mode 100644 run.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b512c09 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules \ No newline at end of file diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..bd3a3c3 --- /dev/null +++ b/index.mjs @@ -0,0 +1,128 @@ +import fs from "fs"; +import xml from "xml"; +import crypto from "crypto"; + +class Shooting { + date; + location; + state; + victims_dead; + victims_injured; + victims_total; + description; + source_urls; + + /** + * @param {string} input + */ + constructor(input) { + const lines = input.trim().split("\n"); + + this.date = lines[0].replace(/(^\|+)|(\{\{[\w\s]*\|)|(\}\})|((-|–)[0-9]+)/g, ""); + this.location = lines[1].replace(/(^\|+)|(\[\[(.*\|)?)|(\]\].*)/g, ""); + this.state = lines[2].replace(/(^\|+)|(\[\[(.*\|)?)|(\]\])/g, ""); + this.victims_dead = Number(lines[3].replace(/^\|+/, "")) || 0; + this.victims_injured = Number(lines[4].replace(/^\|+/, "")) || 0; + this.victims_total = Number(lines[5].replace(/(^\|+)|(''')/g, "")) || 0; + + this.description = lines[6] + .replace(/^\|+/, "") + .replace(/(\[\[(.*\|)?)|(\]\])/g, "") + .replace(/\.*\<\/ref\>/g, "") + .replace(/\{\{.*\}\}/g, "") + .replace(/\}\}.*/g, "") + .trim(); + + this.source_urls = /\|url=^[\s|]*/g + .exec(lines[6]) + ?.map((match) => match.replace("|url=", "")); + } +} + +const input = fs.readFileSync("/tmp/feed_input.txt").toString("utf8"); +const text = input + .substring( + input.indexOf("== List =="), + input.indexOf("== Monthly statistics ==") - 1 + ) + .trim(); +const segments = text + .split(/\n\|-.*\n/) + .slice(2) + .filter((segment) => segment.length); + +const shootings = []; + +for (const segment of segments) { + const shooting = new Shooting(segment); + shootings.push(shooting); +} + +// fs.writeFileSync("./shootings.json", JSON.stringify(shootings, null, 4)); + +const feed = { + rss: [ + { + _attr: { + version: "2.0", + "xmlns:atom": "http://www.w3.org/2005/Atom", + }, + }, + { + channel: [ + { + "atom:link": { + _attr: { + href: "https://futacockinside.me/files/mass_shootings.rss", + rel: "self", + type: "application/rss+xml", + }, + }, + }, + { + title: "Mass shootings in America (2023)", + }, + { + link: "https://en.m.wikipedia.org/w/index.php?title=List_of_mass_shootings_in_the_United_States_in_2023", + }, + { + description: + "Automatically updating feed of mass shootings in America, updated automatically from Wikipedia", + }, + { language: "en-US" }, + ...shootings.map((shooting) => { + const guid = crypto + .createHash("sha256") + .update( + `${shooting.date} ${shooting.location} ${shooting.state} ${shooting.victims_dead}/${shooting.victims_injured}/${shooting.victims_total}` + ) + .digest("hex"); + + return { + item: [ + { + title: `${shooting.date} - ${shooting.location}, ${shooting.state}`, + }, + { pubDate: new Date(`${shooting.date} 2023`).toUTCString() }, + { guid: guid }, + { link: "https://en.m.wikipedia.org/w/index.php?title=List_of_mass_shootings_in_the_United_States_in_2023" }, + { + description: `${shooting.victims_dead} dead, ${ + shooting.victims_injured + } injured.\n\n${shooting.description}\n${ + shooting.source_urls?.join(" ") || "" + }`.trim(), + }, + + ], + }; + }), + ], + }, + ], +}; + +fs.writeFileSync( + "/tmp/feed_output.rss", + xml(feed, { indent: " " }) +); diff --git a/package.json b/package.json new file mode 100644 index 0000000..74c6efc --- /dev/null +++ b/package.json @@ -0,0 +1,5 @@ +{ + "dependencies": { + "xml": "^1.0.1" + } +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml new file mode 100644 index 0000000..b001d3a --- /dev/null +++ b/pnpm-lock.yaml @@ -0,0 +1,16 @@ +lockfileVersion: '6.0' + +settings: + autoInstallPeers: true + excludeLinksFromLockfile: false + +dependencies: + xml: + specifier: ^1.0.1 + version: 1.0.1 + +packages: + + /xml@1.0.1: + resolution: {integrity: sha512-huCv9IH9Tcf95zuYCsQraZtWnJvBtLVE0QHMOs8bWyZAFZNDcYjsPq1nEx8jKA9y+Beo9v+7OBPRisQTjinQMw==} + dev: false diff --git a/run.sh b/run.sh new file mode 100644 index 0000000..50a9aed --- /dev/null +++ b/run.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +curl https://en.m.wikipedia.org/w/index.php\?title\=List_of_mass_shootings_in_the_United_States_in_2023\&action\=raw > /tmp/feed_input.txt +node ./index.mjs +cp /tmp/feed_output.rss /mnt/vault/public/game-archive/files/mass_shootings.rss