完善获取文章列表

This commit is contained in:
tangly1024
2022-05-09 13:52:44 +08:00
parent d200420c31
commit adac140892
4 changed files with 41 additions and 25 deletions

View File

@@ -1,24 +1,28 @@
import { idToUuid } from 'notion-utils'
export default function getAllPageIds (collectionQuery, viewId) {
if (!collectionQuery) {
export default function getAllPageIds (collectionQuery, collectionId, collectionView, viewIds) {
if (!collectionQuery && !collectionView) {
return []
}
const views = Object.values(collectionQuery)[0]
let views = collectionQuery[collectionId]
if (!views) {
return []
views = collectionView
}
let pageIds = []
if (viewId) {
const vId = idToUuid(viewId)
pageIds = views[vId]?.blockIds
} else {
if (collectionQuery && Object.values(collectionQuery).length > 0) {
const pageSet = new Set()
Object.values(views).forEach(view => {
view?.blockIds?.forEach(id => pageSet.add(id)) // group视图
view?.collection_group_results?.blockIds?.forEach(id => pageSet.add(id)) // table视图
})
pageIds = [...pageSet]
console.log('从collectionQuery获取博客', collectionQuery)
} else if (viewIds && viewIds.length > 0) {
const ids = collectionView[viewIds[0]].value.page_sort
console.log('从viewId获取博客 ', viewIds)
for (const id of ids) {
pageIds.push(id)
}
}
console.log('文章列表', pageIds)
return pageIds
}

View File

@@ -21,22 +21,22 @@ export async function getAllPosts({ notionPageData, from, pageType }) {
return []
}
const pageBlock = notionPageData.block
const schema = notionPageData.schema
const tagOptions = notionPageData.tags
const collectionQuery = notionPageData.collectionQuery
const { block, schema, tagOptions, collectionQuery, collectionId, collectionView, viewIds } = notionPageData
const data = []
const pageIds = getAllPageIds(collectionQuery)
const pageIds = getAllPageIds(collectionQuery, collectionId, collectionView, viewIds)
for (let i = 0; i < pageIds.length; i++) {
const id = pageIds[i]
const properties = (await getPageProperties(id, pageBlock, schema)) || null
const properties = (await getPageProperties(id, block, schema)) || null
const value = block[id]?.value
if (!value) {
continue
}
properties.slug = properties.slug ?? properties.id
properties.createdTime = formatDate(new Date(pageBlock[id].value?.created_time).toString(), BLOG.LANG)
properties.lastEditedTime = formatDate(new Date(pageBlock[id].value?.last_edited_time).toString(), BLOG.LANG)
properties.fullWidth = pageBlock[id].value?.format?.page_full_width ?? false
properties.page_cover = getPostCover(id, pageBlock) ?? null
properties.content = pageBlock[id].value?.content ?? []
properties.createdTime = formatDate(new Date(value.created_time).toString(), BLOG.LANG)
properties.lastEditedTime = formatDate(new Date(value?.last_edited_time).toString(), BLOG.LANG)
properties.fullWidth = value.format?.page_full_width ?? false
properties.page_cover = getPostCover(id, block) ?? null
properties.content = value.content ?? []
properties.tagItems = properties?.tags?.map(tag => {
return { name: tag, color: tagOptions.find(t => t.value === tag)?.color || 'gray' }
}) || []

View File

@@ -211,13 +211,18 @@ async function getPageRecordMapByNotionAPI({ pageId, from }) {
}
const collection = Object.values(pageRecordMap.collection)[0]?.value
const collectionId = rawMetadata?.collection_id
const collectionQuery = pageRecordMap.collection_query
const collectionView = pageRecordMap.collection_view
const schema = collection?.schema
const tagOptions = getTagOptions(schema)
const categoryOptions = getCategoryOptions(schema)
const viewIds = rawMetadata?.view_ids
const data = []
const pageIds = getAllPageIds(collectionQuery)
const pageIds = getAllPageIds(collectionQuery, collectionId, collectionView, viewIds)
if (pageIds?.length === 0) {
console.error('获取到的文章列表为空请检查notion模板', collectionQuery, collection, pageRecordMap)
}
for (let i = 0; i < pageIds.length; i++) {
const id = pageIds[i]
const properties = (await getPageProperties(id, block, schema)) || null
@@ -243,6 +248,9 @@ async function getPageRecordMapByNotionAPI({ pageId, from }) {
return {
collection,
collectionQuery,
collectionId,
collectionView,
viewIds,
block,
schema,
tagOptions,

View File

@@ -13,7 +13,11 @@ export async function getPostBlocks(id, from, slice) {
console.warn('[请求API]:', `from:${from}`, `id:${id}`)
pageBlock = await getPageWithRetry(id, from)
console.warn('[请求完成]: 结果', `${pageBlock ? '成功' : '失败'}`, `from:${from}`, `id:${id}`)
if (pageBlock) {
console.info('[请求成功]:', `from:${from}`, `id:${id}`)
} else {
console.error('[请求失败]:', `from:${from}`, `id:${id}`)
}
if (pageBlock) {
await setDataToCache(cacheKey, pageBlock)
@@ -29,7 +33,7 @@ export async function getPostBlocks(id, from, slice) {
*/
async function getPageWithRetry(id, from, retryAttempts = 3) {
if (retryAttempts && retryAttempts > 0) {
console.log('[发起请求]', `from:${from}`, `id:${id}`, `剩余重试次数:${retryAttempts}`)
console.log('[发起请求]', `from:${from}`, `id:${id}`, retryAttempts < 3 ? `剩余重试次数:${retryAttempts}` : '')
try {
const authToken = BLOG.NOTION_ACCESS_TOKEN || null
const api = new NotionAPI({ authToken })