feat: switch to static tweets

This commit is contained in:
Travis Fischer
2021-02-14 23:31:06 -05:00
parent 07c5421248
commit 0add28665a
9 changed files with 906 additions and 17 deletions

View File

@@ -2,6 +2,8 @@ import { NotionAPI } from 'notion-client'
import { ExtendedRecordMap, SearchParams, SearchResults } from 'notion-types'
import { getPreviewImages } from './get-preview-images'
import { mapNotionImageUrl } from './map-image-url'
import { fetchTweetAst, getTweetAst } from 'static-tweets'
import pMap from 'p-map'
const notion = new NotionAPI({
apiBaseUrl: process.env.NOTION_API_BASE_URL
@@ -47,6 +49,50 @@ export async function getPage(pageId: string): Promise<ExtendedRecordMap> {
const previewImageMap = await getPreviewImages(urls)
;(recordMap as any).preview_images = previewImageMap
const tweetIds: string[] = blockIds
.map((blockId) => {
const block = recordMap.block[blockId]?.value
if (block) {
if (block.type === 'tweet') {
const src = block.properties?.source?.[0]?.[0]
if (src) {
const id = src.split('?')[0].split('/').pop()
if (id) return id
}
}
}
})
.filter(Boolean)
const tweetAsts = await pMap(
tweetIds,
async (tweetId) => {
try {
return {
tweetId,
tweetAst: await fetchTweetAst(tweetId)
}
} catch (err) {
console.error('error fetching tweet info', tweetId, err)
}
},
{
concurrency: 4
}
)
const tweetAstMap = tweetAsts.filter(Boolean).reduce(
(acc, t) => ({
...acc,
[t.tweetId]: t.tweetAst
}),
{}
)
;(recordMap as any).tweetAstMap = tweetAstMap
return recordMap
}