()
| 74 | return new Date(getTimestamp(snowflake)); |
| 75 | } |
| 76 | export async function generateSitemap() { |
| 77 | let hasMore; |
| 78 | let offset = 0; |
| 79 | const safetyLimit = 200; |
| 80 | const limit = 10000; |
| 81 | const allThreads: Channel[] = []; |
| 82 | do { |
| 83 | // eslint-disable-next-line no-await-in-loop |
| 84 | const { threads, hasMore: hm } = await listPublicThreads({ |
| 85 | offset, |
| 86 | limit, |
| 87 | }); |
| 88 | allThreads.push(...threads); |
| 89 | hasMore = hm; |
| 90 | offset++; |
| 91 | console.log(`Gathered ${allThreads.length} threads`); |
| 92 | } while (hasMore && offset < safetyLimit); |
| 93 | console.log(`Finished collecting ${allThreads.length} threads`); |
| 94 | |
| 95 | // Go from newest to oldest |
| 96 | const chunks = chunk(allThreads, limit).reverse(); |
| 97 | if (chunks.length == 0 || allThreads.length == 0) { |
| 98 | return; |
| 99 | } |
| 100 | let i = 0; |
| 101 | for await (const chunk of chunks) { |
| 102 | const sitemap = new Sitemap('https://www.answeroverflow.com', 'url'); |
| 103 | sitemap.addMany( |
| 104 | chunk.map((thread) => ({ |
| 105 | loc: `/m/${thread.id}`, |
| 106 | // We really don't expect archived threads to be updated |
| 107 | priority: 0.9, |
| 108 | lastmod: thread.archivedTimestamp |
| 109 | ? new Date(Number(thread.archivedTimestamp)) |
| 110 | : getDate(thread.id), |
| 111 | })), |
| 112 | ); |
| 113 | await uploadFile({ |
| 114 | contentType: 'text/xml', |
| 115 | filename: `sitemaps/sitemap-${i}.xml`, |
| 116 | stream: sitemap.toXml(), |
| 117 | }); |
| 118 | console.log( |
| 119 | `Uploaded sitemaps/sitemap-${i}.xml with ${chunk.length} entries`, |
| 120 | ); |
| 121 | i++; |
| 122 | } |
| 123 | const sitemapIndex = new Sitemap('https://www.answeroverflow.com', 'sitemap'); |
| 124 | for (let j = 0; j < i; j++) { |
| 125 | sitemapIndex.add({ |
| 126 | loc: `/sitemap${j}.xml`, |
| 127 | }); |
| 128 | } |
| 129 | await uploadFile({ |
| 130 | contentType: 'text/xml', |
| 131 | filename: 'sitemaps/sitemap.xml', |
| 132 | stream: sitemapIndex.toXml(), |
| 133 | }); |
no test coverage detected