From 4c0b0bb1732efbda74374112ea37d2f3710a367d Mon Sep 17 00:00:00 2001 From: Nulo Date: Sun, 16 Apr 2023 20:40:10 -0300 Subject: [PATCH] centralizar manejo de feeds --- Mi webring.gen.js | 13 ++----------- feeds.js | 27 +++++++++++++++++++++++++++ tool | 9 +-------- 3 files changed, 30 insertions(+), 19 deletions(-) create mode 100644 feeds.js diff --git a/Mi webring.gen.js b/Mi webring.gen.js index b4a2274..7e5b3f2 100644 --- a/Mi webring.gen.js +++ b/Mi webring.gen.js @@ -4,13 +4,7 @@ import { parseFeed as _parseFeed } from "htmlparser2"; import { a, li, raw, render, ul } from "@nulo/html.js"; import { parseDocument } from "htmlparser2"; import { getElementsByTagName } from "domutils"; - -const feeds = { - fauno: "https://fauno.endefensadelsl.org/feed.xml", - copiona: "https://copiona.com/feed.xml", - j3s: "https://j3s.sh/feed.atom", - icyphox: "https://icyphox.sh/blog/feed.xml", -}; +import { feeds, readFeed } from "./feeds"; export default async () => { const articles = []; @@ -22,10 +16,7 @@ export default async () => { */ const relativeLink = (link) => new URL(link, baseUrl).toString(); - const rawFeed = await readFile( - path.join("cached-feeds/", name + ".xml"), - "utf-8" - ); + const rawFeed = await readFeed(name); const { title, item, link } = parseFeed(rawFeed); articles.push( diff --git a/feeds.js b/feeds.js new file mode 100644 index 0000000..d14afb1 --- /dev/null +++ b/feeds.js @@ -0,0 +1,27 @@ +import { readFile, writeFile } from "fs/promises"; +import { join } from "path"; + +export const feeds = { + fauno: "https://fauno.endefensadelsl.org/feed.xml", + copiona: "https://copiona.com/feed.xml", + j3s: "https://j3s.sh/feed.atom", + icyphox: "https://icyphox.sh/blog/feed.xml", +}; + +if (process.argv[2] === "refresh") { + for (const [name, url] of Object.entries(feeds)) { + console.log(`Refreshing ${name}`); + const res = await fetch(url); + const txt = await res.text(); + await writeFile(join("cached-feeds/", `${name}.xml`), txt); + } +} + +/** + * Lee un feed ya cacheado sin parsear. + * @param {string} name + * @returns string + */ +export async function readFeed(name) { + return await readFile(join("cached-feeds/", name + ".xml"), "utf-8"); +} diff --git a/tool b/tool index 876919a..d0b724f 100755 --- a/tool +++ b/tool @@ -11,15 +11,8 @@ build() { check() { ./node_modules/.bin/tsc --noEmit || exit $? } -refresh_feed() { - echo "Refreshing $1" - wget -qO "cached-feeds/$1.xml" "$2" || exit $? -} refresh_feeds() { - refresh_feed fauno https://fauno.endefensadelsl.org/feed.xml - refresh_feed copiona https://copiona.com/feed.xml - refresh_feed j3s https://j3s.sh/feed.atom - refresh_feed icyphox https://icyphox.sh/blog/feed.xml + node feeds.js refresh } fatal() {