mirror of
https://github.com/imsyy/DailyHotApi.git
synced 2026-01-12 13:14:55 +08:00
修复快手接口报错
This commit is contained in:
43
src/routes/kuaishou.ts
Normal file → Executable file
43
src/routes/kuaishou.ts
Normal file → Executable file
@@ -32,21 +32,52 @@ const getList = async (noCache: boolean) => {
|
|||||||
});
|
});
|
||||||
const listData: ListItem[] = [];
|
const listData: ListItem[] = [];
|
||||||
// 获取主要内容
|
// 获取主要内容
|
||||||
const pattern = /window.__APOLLO_STATE__=(.*);\(function\(\)/s;
|
const html = result.data || "";
|
||||||
const matchResult = result.data?.match(pattern);
|
const start = html.indexOf("window.__APOLLO_STATE__=");
|
||||||
const jsonObject = JSON.parse(matchResult[1])["defaultClient"];
|
if (start === -1) {
|
||||||
|
throw new Error("快手页面结构变更,未找到 APOLLO_STATE");
|
||||||
|
}
|
||||||
|
const scriptSlice = html.slice(start + "window.__APOLLO_STATE__=".length);
|
||||||
|
const sentinelA = scriptSlice.indexOf(";(function(");
|
||||||
|
const sentinelB = scriptSlice.indexOf("</script>");
|
||||||
|
const cutIndex =
|
||||||
|
sentinelA !== -1 && sentinelB !== -1 ? Math.min(sentinelA, sentinelB) : Math.max(sentinelA, sentinelB);
|
||||||
|
if (cutIndex === -1) {
|
||||||
|
throw new Error("快手页面结构变更,未找到 APOLLO_STATE 结束标记");
|
||||||
|
}
|
||||||
|
const raw = scriptSlice.slice(0, cutIndex).trim().replace(/;$/, "");
|
||||||
|
let jsonObject;
|
||||||
|
try {
|
||||||
|
// 快手返回的 JSON 末尾常带 undefined/null,需要截断到最后一个 '}' 出现
|
||||||
|
const lastBrace = raw.lastIndexOf("}");
|
||||||
|
const cleanRaw = lastBrace !== -1 ? raw.slice(0, lastBrace + 1) : raw;
|
||||||
|
jsonObject = JSON.parse(cleanRaw)["defaultClient"];
|
||||||
|
} catch (err) {
|
||||||
|
const msg =
|
||||||
|
err instanceof Error
|
||||||
|
? `${err.message} | snippet=${raw.slice(0, 200)}...`
|
||||||
|
: "未知错误";
|
||||||
|
throw new Error(`快手数据解析失败: ${msg}`);
|
||||||
|
}
|
||||||
// 获取所有分类
|
// 获取所有分类
|
||||||
const allItems = jsonObject['$ROOT_QUERY.visionHotRank({"page":"home"})']["items"];
|
const allItems =
|
||||||
|
jsonObject['$ROOT_QUERY.visionHotRank({"page":"home"})']?.items ||
|
||||||
|
jsonObject['$ROOT_QUERY.visionHotRank({"page":"home","platform":"web"})']
|
||||||
|
?.items ||
|
||||||
|
[];
|
||||||
// 获取全部热榜
|
// 获取全部热榜
|
||||||
allItems?.forEach((item: { id: string }) => {
|
allItems?.forEach((item: { id: string }) => {
|
||||||
// 基础数据
|
// 基础数据
|
||||||
const hotItem: RouterType["kuaishou"] = jsonObject[item.id];
|
const hotItem: RouterType["kuaishou"] = jsonObject[item.id];
|
||||||
|
if (!hotItem) return;
|
||||||
const id = hotItem.photoIds?.json?.[0];
|
const id = hotItem.photoIds?.json?.[0];
|
||||||
|
const hotValue = hotItem.hotValue ?? "";
|
||||||
|
const poster = hotItem.poster ? decodeURIComponent(hotItem.poster) : undefined;
|
||||||
listData.push({
|
listData.push({
|
||||||
id: hotItem.id,
|
id: hotItem.id,
|
||||||
title: hotItem.name,
|
title: hotItem.name,
|
||||||
cover: decodeURIComponent(hotItem.poster),
|
cover: poster,
|
||||||
hot: parseChineseNumber(hotItem.hotValue),
|
hot: parseChineseNumber(String(hotValue)),
|
||||||
timestamp: undefined,
|
timestamp: undefined,
|
||||||
url: `https://www.kuaishou.com/short-video/${id}`,
|
url: `https://www.kuaishou.com/short-video/${id}`,
|
||||||
mobileUrl: `https://www.kuaishou.com/short-video/${id}`,
|
mobileUrl: `https://www.kuaishou.com/short-video/${id}`,
|
||||||
|
|||||||
Reference in New Issue
Block a user