Skip to content

Commit

Permalink
fix(fetch): parse comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Chilfish committed Feb 29, 2024
1 parent d4ce54c commit bd62273
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
16 changes: 8 additions & 8 deletions packages/core/src/services/postService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export async function fetchLongText(
*/
export async function fetchComments(post: Post): Promise<Comment[]> {
const { commentCount, comment, picLarge } = await getOptions()
const imgSize = picLarge ? 'largest' : 'large'
const imgSize = picLarge ? 'woriginal' : 'large'

if (!post.user || post.comments_count === 0 || !comment)
return []
Expand All @@ -81,20 +81,20 @@ export async function fetchComments(post: Post): Promise<Comment[]> {
id: post.id,
is_show_bulletin: post.is_show_bulletin, // 必填字段,区分旧微博和新微博
flow: 0, // 热评
is_reload: 1, // 获取详情页的评论
is_mix: 0,
count: 10,
fetch_level: 0,
locale: 'zh_CN',
uid: post.user.id,
},
})

if (!data)
return []

const userComments = data.filter(comment => comment.user?.id === post.user?.id)
const othersComments = data.filter(comment => comment.user?.id !== post.user?.id)

return filterComments(
Array.from(new Set([
...userComments,
...othersComments.slice(0, commentCount),
])),
data.slice(0, commentCount),
imgSize,
)
}
Expand Down
14 changes: 10 additions & 4 deletions packages/core/src/utils/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,20 @@ export function parseText(text?: string) {
* 提取原图链接
*/
export function parseImg(
size: 'large' | 'largest',
size: string,
pic_ids?: string[],
img_infos?: Record<string, PicInfo>,
) {
if (!pic_ids || !img_infos)
return []

return pic_ids.map(id => img_infos[id][size].url)
try {
return pic_ids.map(id => img_infos[id][size].url)
}
catch (e) {
console.log(`提取图片链接失败 ${e}`)
return []
}
}

/**
Expand Down Expand Up @@ -92,7 +98,7 @@ function parseCard(url_struct?: any[], card?: any): CardInfo | undefined {
*/
export function filterComments(
comments?: any[],
imgSize: 'large' | 'largest' = 'large',
imgSize = 'large',
): Comment[] {
if (!comments || !comments.length || !comments[0]?.id)
return []
Expand Down Expand Up @@ -124,7 +130,7 @@ export function filterComments(
return res
}
catch (e) {
console.log(e, `数据解析失败, id: ${comment.id}, ${comment.text}`)
console.log(`数据解析失败, id: ${comment.id}, ${e}`)
return null
}
}).filter((e): e is Comment => !!e)
Expand Down

0 comments on commit bd62273

Please sign in to comment.