New options for source views & faster rendering
This commit is contained in:
		| @@ -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 | ||||
| } | ||||
| } | ||||
							
								
								
									
										63
									
								
								index.js
									
									
									
									
									
								
							
							
						
						
									
										63
									
								
								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) => { | ||||
|       `<a href="data:text/html,">end</a>` : | ||||
|       `<a href="${nextPage}.html">next</a>` | ||||
|  | ||||
|     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) => { | ||||
| <meta name="viewport" content="width=device-width, initial-scale=1"> | ||||
| <style> | ||||
|   body { | ||||
|     width: 100vw; | ||||
|     max-width: 640px; | ||||
|     float: right; | ||||
|     font-family: sans-serif; | ||||
|   } | ||||
|  | ||||
|   ul { | ||||
|     padding-inline-start: 30px; | ||||
|     list-style-type: none; | ||||
|   } | ||||
|  | ||||
|   p { | ||||
|     padding: 30px; | ||||
|   } | ||||
| @@ -300,6 +318,7 @@ const main = async () => { | ||||
|     feeds.push({ | ||||
|       name: feedName, | ||||
|       main: feed.main, | ||||
|       view: feed.view, | ||||
|       sources, | ||||
|       link | ||||
|     }) | ||||
| @@ -312,7 +331,7 @@ const main = async () => { | ||||
|       if(config.linkToIndex) | ||||
|         link += '/index.html' | ||||
|  | ||||
|       return `<div><a href="${link}">${name}</a></div>` | ||||
|       return `<li><a href="${link}">${name}</a></li>` | ||||
|     } | ||||
|  | ||||
|     return `\ | ||||
| @@ -320,26 +339,40 @@ const main = async () => { | ||||
|  | ||||
| <summary>Feeds</summary> | ||||
| <section> | ||||
| <ul> | ||||
|  | ||||
| ${buildLink({ link: '' }, 'main')} | ||||
| ${feeds.filter(feed => !feed.main).map(feed => buildLink(feed)).join('\n')} | ||||
|  | ||||
| </ul> | ||||
| <hr> | ||||
| <ul> | ||||
|  | ||||
| ${allSources.map(source => buildLink(source)).join('\n')} | ||||
|  | ||||
| </ul> | ||||
| </section> | ||||
|  | ||||
| </details> | ||||
| <hr>` | ||||
|   } | ||||
|  | ||||
|   let promises = [] | ||||
|  | ||||
|   console.log('Writing...') | ||||
|   for(let source of allSources) { | ||||
|     console.log(source) | ||||
|  | ||||
|     await printFeed([ source.source ], Path.join('out', source.link), buildFeedNav(2)) | ||||
|     promises.push( | ||||
|       printFeed([ source.source ], Path.join('out', source.link), buildFeedNav(2), config.sourceView) | ||||
|     ) | ||||
|   } | ||||
|   for(let feed of feeds) { | ||||
|     await printFeed(feed.sources, Path.join('out', feed.link), buildFeedNav(feed.main ? 0 : 1)) | ||||
|     promises.push( | ||||
|       printFeed(feed.sources, Path.join('out', feed.link), buildFeedNav(feed.main ? 0 : 1), feed.view) | ||||
|     ) | ||||
|   } | ||||
|  | ||||
|   await Promise.all(promises) | ||||
|  | ||||
|   console.log('Done!') | ||||
| } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user