Skip to content

Commit

Permalink
fix: fetch hot comments (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chilfish committed Feb 29, 2024
1 parent db15ea5 commit 28a6bc6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
10 changes: 8 additions & 2 deletions packages/core/src/services/postService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export async function fetchRangePosts(page = 1): FetchReturn {

export async function fetchLongText(
post: Post & { isLongText: boolean },
): Promise<string> {
) {
let text = post.text

if (post.isLongText) {
Expand All @@ -75,7 +75,13 @@ export async function fetchComments(post: Post): Promise<Comment[]> {
return []

await delay(3000)
const { data } = await weiFetch<{ data: Comment[] }>(`/statuses/buildComments?id=${post.id}&is_show_bulletin=0`)
const { data } = await weiFetch<{ data: Comment[] }>(`/statuses/buildComments`, {
params: {
id: post.id,
is_show_bulletin: post.is_show_bulletin, // 必填字段,区分旧微博和新微博
flow: 0, // 热评
},
})

if (!data)
return []
Expand Down
32 changes: 24 additions & 8 deletions packages/core/src/utils/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@ export const link = (text: string, url = weibo) => `<a href="${url}">${text}</a>
* 解析正文,例如 @user => link(user, userUrl)
*/
export function parseText(text?: string) {
if (!text)
return ''
if (!text) {
return {
text: '',
textImg: null,
}
}

let parsed = text
.replace(
/<a[^>]*>(@[^<]+)<\/a>/g, // @用户
Expand All @@ -29,14 +34,18 @@ export function parseText(text?: string) {
.replace(/\/\/weibo.cn\/sinaurl\?u=(.+)/, (_, href) => decodeURIComponent(href)) // 去掉微博的链接跳转

const retweetImg = /<a[^>]*href="([^"]*)"[^>]*>查看图片<\/a>/gm.exec(parsed)
let textImg = null

if (retweetImg && retweetImg[1]) {
const img = retweetImg[1]
textImg = retweetImg[1]

parsed = parsed.replace(retweetImg[0], `[img://${img}]`)
parsed = parsed.replace(retweetImg[0], `[img://${textImg}]`)
}

return parsed
return {
text: parsed,
textImg,
}
}

/**
Expand Down Expand Up @@ -86,9 +95,11 @@ export function filterComments(comments?: any[]): Comment[] {
return []
return comments.map((comment) => {
try {
const { text } = parseText(comment.text) // 评论区就没见过折叠长文本

const res: Comment = {
id: comment.idstr,
text: parseText(comment.text), // 评论区就没见过折叠长文本
text,
img: comment.url_struct?.[0]?.long_url,
created_at: comment.created_at,
user: {
Expand Down Expand Up @@ -124,10 +135,15 @@ export async function postFilter(
const includeComments = !isRepost && options.comment

try {
const { text, textImg } = await fetchLongText(post)
const imgs = includeImgs ? parseImg(imgSize, post.pic_ids, post.pic_infos) : []

textImg && imgs.push(textImg)

const res: Post = {
id: post.id,
text: await fetchLongText(post),
imgs: includeImgs ? parseImg(imgSize, post.pic_ids, post.pic_infos) : [],
text,
imgs,
reposts_count: post.reposts_count,
comments_count: post.comments_count,
like_count: post.attitudes_count,
Expand Down
2 changes: 2 additions & 0 deletions types/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ export interface Post extends Meta {
*/
retweeted_status?: Retweet

is_show_bulletin?: 0 | 2

comments: Comment[]
}

Expand Down

0 comments on commit 28a6bc6

Please sign in to comment.