From c110afb6edd1d770f5203013e2d8f369c8c9d7a2 Mon Sep 17 00:00:00 2001 From: Taylor Troesh Date: Sat, 31 Aug 2024 08:33:49 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.js | 29 ++++++++++++++++++++++++++++- feed.js | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ style.css | 6 ++++-- template.html | 10 +++------- 4 files changed, 85 insertions(+), 10 deletions(-) create mode 100644 feed.js diff --git a/build.js b/build.js index 925fc9b..ea4e3c8 100644 --- a/build.js +++ b/build.js @@ -17,7 +17,6 @@ for (const blog of blogs) { blog.title = (blog.title || "").trim(); blog.desc = (blog.desc || "").trim(); if (!blog.url) continue; - if (blog.title && blog.title === blog.desc) console.log(blog.title); if ( 3 > 0 + @@ -90,6 +89,7 @@ const about = `

blogs.hn is a directory of tech sites, primarily sourced from HackerNews.

To submit/update a blog, edit blogs.json in a pull-request.

If you like sites with RSS feeds, consider checking out ooh.directory. If you don't have an RSS reader, I highly recommend Reeder 5.

+

You can import all the RSS feeds via this OPML file.

And if you aren't already, you should write things on the internet. All you need is worstpress and some markdown files! You can email me if you need help getting started.

Every blog is a window into a skull. Don't be afraid to ask questions and kindle friendships! Remember to be kind, courteous, and succinct.

Thanks for stopping by.

@@ -102,3 +102,30 @@ fs.writeFileSync( "./dist/about.html", template.replace("{{body}}", `
${about}
`) ); + +const escxml = unsafe => { + return unsafe.replace(/[<>&'"]/g, (c) => { + switch (c) { + case '<': return '<'; + case '>': return '>'; + case '&': return '&'; + case '\'': return '''; + case '"': return '"'; + default: return c; + } + }); +}; +fs.writeFileSync( + "./dist/blogs.hn.opml", + ` + + + blogs.hn + + +${blogs.filter(blog => blog.title && blog.feed && blog.url).map(blog => ``.replace(/\s+/gi, " ")).join('\n')} + +`, + "utf-8" +); + diff --git a/feed.js b/feed.js new file mode 100644 index 0000000..9a507b7 --- /dev/null +++ b/feed.js @@ -0,0 +1,50 @@ +const fetch = require("node-fetch"); +const xml2js = require("xml2js"); + +process.stdin.setEncoding('utf8'); + +let input = ''; +process.stdin.on('data', chunk => input += chunk); + +process.stdin.on('end', async () => { + try { + const feeds = JSON.parse(input) + .map(x => x.feed) + .filter(x => x) + .sort(() => Math.random() - 0.5) + .slice(0, 1); + + const results = await Promise.all(feeds.map(async (url) => { + try { + console.log(url); + const response = await fetch(url); + const text = await response.text(); + const parser = new xml2js.Parser(); + const xml = await parser.parseStringPromise(text); + + const items = xml.rss ? xml.rss.channel[0].item : xml.feed.entry; + return items.map(item => { + let obj = {}; + for (let key in item) { + obj[key] = item[key][0]; + } + return obj; + }); + } catch (error) { + return []; + } + })); + + console.log(results?.[0]); + + const output = results + .flatMap(x => x) + .filter(x => x.id || x.guid) + .map(x => `${x.id || x.guid}\t${x.link}`) + .join('\n'); + + console.log(output); + } catch (error) { + console.error("Error parsing input or processing feeds:", error); + } +}); diff --git a/style.css b/style.css index 8321042..88cc4c2 100644 --- a/style.css +++ b/style.css @@ -60,11 +60,13 @@ body { } header { padding: 1rem 2rem; - display: grid; + display: flex; gap: 0.5rem; - grid-template-columns: auto 1fr auto auto auto; align-items: baseline; } +header > a:first-child { + margin-right: auto; +} main { margin: 2rem 2rem 12rem; } diff --git a/template.html b/template.html index bf94954..ba58878 100644 --- a/template.html +++ b/template.html @@ -24,14 +24,10 @@
blogs.hn -
-
- -
- github about + src + opml +
{{body}}