From 7affc50925077da66036868eb994a7b55a0c4534 Mon Sep 17 00:00:00 2001 From: dakedres Date: Wed, 10 Jan 2024 17:55:07 -0500 Subject: [PATCH] New options for source views & faster rendering --- default/config.js | 20 ++++++++++----- index.js | 63 ++++++++++++++++++++++++++++++++++++----------- 2 files changed, 62 insertions(+), 21 deletions(-) diff --git a/default/config.js b/default/config.js index b62aa19..14d4fc0 100644 --- a/default/config.js +++ b/default/config.js @@ -1,14 +1,23 @@ -const pageSize = 20 -const courtesyWait = 1000 * 1 -const tooLongAgo = 7 * 4 // Days +const courtesyWait = 1000 * 10 const linkToIndex = false +const pageSize = 20 const printDate = date => (date.getMonth() + 1) + '.' + date.getDate() + '.' + date.getFullYear() +const view = { + tooLongAgo: 7 * 4, // Days + pageSize +} + +const sourceView = { + pageSize +} + const feeds = { main: { main: true, + view, nitter: [], tumblr: [ 'dakedres' @@ -99,9 +108,8 @@ const sources = { module.exports = { feeds, sources, - pageSize, courtesyWait, - tooLongAgo, linkToIndex, + sourceView, printDate -} +} \ No newline at end of file diff --git a/index.js b/index.js index 55667eb..c49433d 100644 --- a/index.js +++ b/index.js @@ -22,7 +22,7 @@ const handleNitterUser = async user => { data = processNitter(rss, user) } catch(err) { if(err.constructor.name == NoMatchesError.name || err.constructor.name == DOMException.name) { - console.log(`Failed to fetch ${user} from ${source}`) + console.warn(`Failed to fetch ${user} from ${source}`) index++ } else { console.error(err) @@ -144,10 +144,12 @@ const processTumblr = (rss, user) => { const oneDay = 1000 * 60 * 60 * 24 -const printFeed = async (sources, directory, header) => { +const printFeed = async (sources, directory, header, viewOptions) => { // Coalate let feed = [] - let tooLongAgo = (Date.now() - (Date.now() % oneDay)) - oneDay * config.tooLongAgo + let tooLongAgo = viewOptions.tooLongAgo ? + (Date.now() - (Date.now() % oneDay)) - oneDay * viewOptions.tooLongAgo : + 0 let missingSources = 0 for(let source of sources) { @@ -157,7 +159,7 @@ const printFeed = async (sources, directory, header) => { } for(let post of source) { - if(tooLongAgo && post.date > tooLongAgo) + if(post.date > tooLongAgo) feed.push(post) } } @@ -172,12 +174,19 @@ const printFeed = async (sources, directory, header) => { let pages = [] - for(let i = 0; i < Math.ceil(feed.length / config.pageSize); i++) { - pages.push(feed.slice(i * config.pageSize, (i + 1) * config.pageSize) ) + for(let i = 0; i < Math.ceil(feed.length / viewOptions.pageSize); i++) { + pages.push(feed.slice(i * viewOptions.pageSize, (i + 1) * viewOptions.pageSize) ) } // Write + let promises = [] + + const writePage = (index, content) => + promises.push( + Bun.write(Path.join(directory, index == 0 ? 'index' : index) + '.html', content) + ) + for(let i = 0; i < pages.length; i++) { let nextPage = i + 1 @@ -185,11 +194,14 @@ const printFeed = async (sources, directory, header) => { `end` : `next` - Bun.write( - Path.join(directory, (i == 0 ? 'index' : i) + '.html'), - renderPage(`Page ${i + 1}`, pages[i], header, link) - ) + writePage(i, renderPage(`Page ${i + 1}`, pages[i], header, link)) } + + if(pages.length == 0) { + writePage(0, renderPage('No posts', [], header, 'No posts available') ) + } + + return Promise.all(promises) } const renderPage = (title, posts, header, footer) => { @@ -201,11 +213,17 @@ const renderPage = (title, posts, header, footer) => {