New options for source views & faster rendering
This commit is contained in:
		| @@ -1,14 +1,23 @@ | |||||||
| const pageSize = 20 | const courtesyWait = 1000 * 10 | ||||||
| const courtesyWait = 1000 * 1 |  | ||||||
| const tooLongAgo = 7 * 4 // Days |  | ||||||
| const linkToIndex = false | const linkToIndex = false | ||||||
|  | const pageSize = 20 | ||||||
|  |  | ||||||
| const printDate = date => | const printDate = date => | ||||||
|   (date.getMonth() + 1) + '.' + date.getDate() + '.' + date.getFullYear() |   (date.getMonth() + 1) + '.' + date.getDate() + '.' + date.getFullYear() | ||||||
|  |  | ||||||
|  | const view = { | ||||||
|  |   tooLongAgo: 7 * 4, // Days | ||||||
|  |   pageSize | ||||||
|  | } | ||||||
|  |  | ||||||
|  | const sourceView = { | ||||||
|  |   pageSize | ||||||
|  | } | ||||||
|  |  | ||||||
| const feeds = { | const feeds = { | ||||||
|   main: { |   main: { | ||||||
|     main: true, |     main: true, | ||||||
|  |     view, | ||||||
|     nitter: [], |     nitter: [], | ||||||
|     tumblr: [ |     tumblr: [ | ||||||
|       'dakedres' |       'dakedres' | ||||||
| @@ -99,9 +108,8 @@ const sources = { | |||||||
| module.exports = { | module.exports = { | ||||||
|   feeds, |   feeds, | ||||||
|   sources, |   sources, | ||||||
|   pageSize, |  | ||||||
|   courtesyWait, |   courtesyWait, | ||||||
|   tooLongAgo, |  | ||||||
|   linkToIndex, |   linkToIndex, | ||||||
|  |   sourceView, | ||||||
|   printDate |   printDate | ||||||
| } | } | ||||||
							
								
								
									
										63
									
								
								index.js
									
									
									
									
									
								
							
							
						
						
									
										63
									
								
								index.js
									
									
									
									
									
								
							| @@ -22,7 +22,7 @@ const handleNitterUser = async user => { | |||||||
|       data = processNitter(rss, user) |       data = processNitter(rss, user) | ||||||
|     } catch(err) { |     } catch(err) { | ||||||
|       if(err.constructor.name == NoMatchesError.name || err.constructor.name == DOMException.name) { |       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++ |         index++ | ||||||
|       } else { |       } else { | ||||||
|         console.error(err) |         console.error(err) | ||||||
| @@ -144,10 +144,12 @@ const processTumblr = (rss, user) => { | |||||||
|  |  | ||||||
| const oneDay = 1000 * 60 * 60 * 24 | const oneDay = 1000 * 60 * 60 * 24 | ||||||
|  |  | ||||||
| const printFeed = async (sources, directory, header) => { | const printFeed = async (sources, directory, header, viewOptions) => { | ||||||
|   // Coalate |   // Coalate | ||||||
|   let feed = [] |   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 |   let missingSources = 0 | ||||||
|  |  | ||||||
|   for(let source of sources) { |   for(let source of sources) { | ||||||
| @@ -157,7 +159,7 @@ const printFeed = async (sources, directory, header) => { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     for(let post of source) { |     for(let post of source) { | ||||||
|       if(tooLongAgo && post.date > tooLongAgo) |       if(post.date > tooLongAgo) | ||||||
|         feed.push(post) |         feed.push(post) | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
| @@ -172,12 +174,19 @@ const printFeed = async (sources, directory, header) => { | |||||||
|  |  | ||||||
|   let pages = [] |   let pages = [] | ||||||
|  |  | ||||||
|   for(let i = 0; i < Math.ceil(feed.length / config.pageSize); i++) { |   for(let i = 0; i < Math.ceil(feed.length / viewOptions.pageSize); i++) { | ||||||
|     pages.push(feed.slice(i * config.pageSize, (i + 1) * config.pageSize) ) |     pages.push(feed.slice(i * viewOptions.pageSize, (i + 1) * viewOptions.pageSize) ) | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   // Write |   // 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++) { |   for(let i = 0; i < pages.length; i++) { | ||||||
|     let nextPage = i + 1 |     let nextPage = i + 1 | ||||||
|  |  | ||||||
| @@ -185,11 +194,14 @@ const printFeed = async (sources, directory, header) => { | |||||||
|       `<a href="data:text/html,">end</a>` : |       `<a href="data:text/html,">end</a>` : | ||||||
|       `<a href="${nextPage}.html">next</a>` |       `<a href="${nextPage}.html">next</a>` | ||||||
|  |  | ||||||
|     Bun.write( |     writePage(i, renderPage(`Page ${i + 1}`, pages[i], header, link)) | ||||||
|       Path.join(directory, (i == 0 ? 'index' : i) + '.html'), |  | ||||||
|       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) => { | 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"> | <meta name="viewport" content="width=device-width, initial-scale=1"> | ||||||
| <style> | <style> | ||||||
|   body { |   body { | ||||||
|  |     width: 100vw; | ||||||
|     max-width: 640px; |     max-width: 640px; | ||||||
|     float: right; |     float: right; | ||||||
|     font-family: sans-serif; |     font-family: sans-serif; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |   ul { | ||||||
|  |     padding-inline-start: 30px; | ||||||
|  |     list-style-type: none; | ||||||
|  |   } | ||||||
|  |  | ||||||
|   p { |   p { | ||||||
|     padding: 30px; |     padding: 30px; | ||||||
|   } |   } | ||||||
| @@ -300,6 +318,7 @@ const main = async () => { | |||||||
|     feeds.push({ |     feeds.push({ | ||||||
|       name: feedName, |       name: feedName, | ||||||
|       main: feed.main, |       main: feed.main, | ||||||
|  |       view: feed.view, | ||||||
|       sources, |       sources, | ||||||
|       link |       link | ||||||
|     }) |     }) | ||||||
| @@ -312,7 +331,7 @@ const main = async () => { | |||||||
|       if(config.linkToIndex) |       if(config.linkToIndex) | ||||||
|         link += '/index.html' |         link += '/index.html' | ||||||
|  |  | ||||||
|       return `<div><a href="${link}">${name}</a></div>` |       return `<li><a href="${link}">${name}</a></li>` | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     return `\ |     return `\ | ||||||
| @@ -320,26 +339,40 @@ const main = async () => { | |||||||
|  |  | ||||||
| <summary>Feeds</summary> | <summary>Feeds</summary> | ||||||
| <section> | <section> | ||||||
|  | <ul> | ||||||
|  |  | ||||||
| ${buildLink({ link: '' }, 'main')} | ${buildLink({ link: '' }, 'main')} | ||||||
| ${feeds.filter(feed => !feed.main).map(feed => buildLink(feed)).join('\n')} | ${feeds.filter(feed => !feed.main).map(feed => buildLink(feed)).join('\n')} | ||||||
|  |  | ||||||
|  | </ul> | ||||||
| <hr> | <hr> | ||||||
|  | <ul> | ||||||
|  |  | ||||||
| ${allSources.map(source => buildLink(source)).join('\n')} | ${allSources.map(source => buildLink(source)).join('\n')} | ||||||
|  |  | ||||||
|  | </ul> | ||||||
| </section> | </section> | ||||||
|  |  | ||||||
| </details> | </details> | ||||||
| <hr>` | <hr>` | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |   let promises = [] | ||||||
|  |  | ||||||
|   console.log('Writing...') |   console.log('Writing...') | ||||||
|   for(let source of allSources) { |   for(let source of allSources) { | ||||||
|     console.log(source) |     promises.push( | ||||||
|  |       printFeed([ source.source ], Path.join('out', source.link), buildFeedNav(2), config.sourceView) | ||||||
|     await printFeed([ source.source ], Path.join('out', source.link), buildFeedNav(2)) |     ) | ||||||
|   } |   } | ||||||
|   for(let feed of feeds) { |   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!') |   console.log('Done!') | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user