-
Notifications
You must be signed in to change notification settings - Fork 1.2k
修复百度接口 #129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
修复百度接口 #129
Conversation
Summary of ChangesHello @wuaishare, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! 这个拉取请求旨在修复百度接口的数据抓取问题,通过更新用户代理、增强数据解析逻辑以及扩展类型定义,确保了各种百度数据(如热搜、小说、电影等)能够被稳定且正确地获取和处理。这些改动提升了数据源的可靠性,并为未来可能的数据结构变化提供了更好的兼容性。 Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
本次 PR 修复了百度接口的集成问题,通过更新 User-Agent 并使数据解析逻辑更具鲁棒性以处理不同的响应结构,做得很好。代码更改方向正确,但我在错误处理和代码清晰度方面发现了一些可以改进的地方,并已在代码中添加了具体的审查意见。
src/routes/baidu.ts
Outdated
| const sData = JSON.parse(matchResult[1]); | ||
| const cardContent = sData.data?.cards?.[0]?.content ?? sData.cards?.[0]?.content; | ||
| const jsonObject = Array.isArray(cardContent) | ||
| ? Array.isArray(cardContent[0]?.content) | ||
| ? cardContent[0].content | ||
| : cardContent | ||
| : []; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这段代码存在两个问题:
- 可靠性:
JSON.parse(matchResult[1])可能会因为matchResult[1]不是一个有效的 JSON 字符串而抛出异常,这将导致整个请求失败。建议增加try...catch来处理潜在的解析错误。 - 可读性: 用于确定
jsonObject值的嵌套三元运算符虽然紧凑,但可读性较差,不易于维护。
建议将此代码块重构为使用 try...catch 和更清晰的 if/else 结构,以提高代码的健壮性和可读性。
let jsonObject: any[] = [];
try {
const sData = JSON.parse(matchResult[1]);
const cardContent = sData.data?.cards?.[0]?.content ?? sData.cards?.[0]?.content;
if (Array.isArray(cardContent)) {
if (cardContent.length > 0 && Array.isArray(cardContent[0]?.content)) {
jsonObject = cardContent[0].content;
} else {
jsonObject = cardContent;
}
}
} catch (error) {
// 建议记录错误用于调试
// logger.error("Failed to parse Baidu data", error);
}
src/routes/baidu.ts
Outdated
| cover: v.img ?? v.imgInfo?.src ?? "", | ||
| author: v.show?.length ? v.show : "", | ||
| timestamp: 0, | ||
| hot: Number(v.hotScore ?? v.hotTag ?? 0), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
已经测试过了,没啥问题,cover、desc、hot都成功抓取到了。
"realtime": "热搜",
"novel": "小说",
"movie": "电影",
"teleplay": "电视剧",
"car": "汽车",
"game": "游戏"
这几个也做了测试,也都抓取正常。