|
| 1 | +import { cli, Strategy } from '../../registry.js'; |
| 2 | +import { getHupuThreadUrl, readHupuNextData, stripHtml } from './utils.js'; |
| 3 | + |
| 4 | +// JSON数据结构(对应Next.js的__NEXT_DATA__) |
| 5 | +interface NextData { |
| 6 | + props: Props; |
| 7 | +} |
| 8 | + |
| 9 | +interface Props { |
| 10 | + pageProps: PageProps; |
| 11 | +} |
| 12 | + |
| 13 | +interface PageProps { |
| 14 | + detail?: Detail; |
| 15 | + detail_error_info?: DetailError; |
| 16 | +} |
| 17 | + |
| 18 | +interface DetailError { |
| 19 | + code: number; |
| 20 | + message: string; |
| 21 | +} |
| 22 | + |
| 23 | +interface Detail { |
| 24 | + thread?: Thread; |
| 25 | + lights?: ReplyData[]; |
| 26 | +} |
| 27 | + |
| 28 | +interface Thread { |
| 29 | + tid: string; |
| 30 | + title: string; |
| 31 | + content: string; |
| 32 | + lights?: number; |
| 33 | + replies?: number; |
| 34 | + author?: Author; |
| 35 | +} |
| 36 | + |
| 37 | +interface Author { |
| 38 | + puname?: string; |
| 39 | +} |
| 40 | + |
| 41 | +interface ReplyData { |
| 42 | + pid: string; |
| 43 | + author?: Author; |
| 44 | + content: string; |
| 45 | + allLightCount?: number; // 修复:正确的字段名 |
| 46 | + created_at_format?: string; |
| 47 | +} |
| 48 | + |
| 49 | +cli({ |
| 50 | + site: 'hupu', |
| 51 | + name: 'detail', |
| 52 | + description: '获取虎扑帖子详情 (使用Next.js JSON数据)', |
| 53 | + domain: 'bbs.hupu.com', |
| 54 | + strategy: Strategy.PUBLIC, |
| 55 | + browser: true, |
| 56 | + navigateBefore: false, |
| 57 | + args: [ |
| 58 | + { |
| 59 | + name: 'tid', |
| 60 | + required: true, |
| 61 | + positional: true, |
| 62 | + help: '帖子ID(9位数字)' |
| 63 | + }, |
| 64 | + { |
| 65 | + name: 'replies', |
| 66 | + type: 'boolean', |
| 67 | + default: false, |
| 68 | + help: '是否包含热门回复' |
| 69 | + } |
| 70 | + ], |
| 71 | + columns: ['title', 'author', 'content', 'replies', 'lights', 'url'], |
| 72 | + func: async (page, kwargs) => { |
| 73 | + const { tid, replies: includeReplies = false } = kwargs; |
| 74 | + |
| 75 | + const url = getHupuThreadUrl(tid).replace(/-1\.html$/, '.html'); |
| 76 | + const data = await readHupuNextData<NextData>(page, url, 'Read Hupu thread detail', { |
| 77 | + expectedTid: String(tid), |
| 78 | + }); |
| 79 | + |
| 80 | + // 检查错误信息(只有当code不是200时才报错) |
| 81 | + const errorInfo = data.props.pageProps.detail_error_info; |
| 82 | + if (errorInfo && errorInfo.code !== 200) { |
| 83 | + throw new Error(`帖子访问失败: ${errorInfo.message} (code: ${errorInfo.code})`); |
| 84 | + } |
| 85 | + |
| 86 | + // 获取帖子信息 |
| 87 | + const thread = data.props.pageProps.detail?.thread; |
| 88 | + if (!thread) { |
| 89 | + throw new Error('帖子不存在或已被删除'); |
| 90 | + } |
| 91 | + |
| 92 | + const authorName = thread.author?.puname || '未知作者'; |
| 93 | + const content = stripHtml(thread.content); |
| 94 | + const contentPreview = content.length > 300 ? content.substring(0, 300) + '...' : content; |
| 95 | + |
| 96 | + // 构建结果 |
| 97 | + const result: any = { |
| 98 | + title: thread.title, |
| 99 | + author: authorName, |
| 100 | + content: contentPreview, |
| 101 | + replies: thread.replies || 0, |
| 102 | + lights: thread.lights || 0, |
| 103 | + url: `https://bbs.hupu.com/${tid}.html` |
| 104 | + }; |
| 105 | + |
| 106 | + // 如果需要包含回复,添加回复信息到内容中 |
| 107 | + if (includeReplies) { |
| 108 | + const replyList = data.props.pageProps.detail?.lights || []; |
| 109 | + const topReplies = replyList.slice(0, 3); |
| 110 | + |
| 111 | + if (topReplies.length > 0) { |
| 112 | + let replyText = '\n\n【热门回复】\n'; |
| 113 | + topReplies.forEach((reply, index) => { |
| 114 | + const userName = reply.author?.puname || '未知用户'; |
| 115 | + const replyContent = stripHtml(reply.content).substring(0, 100); |
| 116 | + const replyLights = reply.allLightCount || 0; // 修复:使用正确的字段名 |
| 117 | + const replyTime = reply.created_at_format || '未知时间'; |
| 118 | + replyText += `${index + 1}. ${userName} (亮${replyLights} ${replyTime}):\n ${replyContent}\n\n`; |
| 119 | + }); |
| 120 | + result.content = contentPreview + replyText; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + return [result]; |
| 125 | + }, |
| 126 | +}); |
0 commit comments