Merge pull request #128 from wuaishare/master

修复快手接口
This commit is contained in:
imsyy
2025-12-28 22:55:54 +08:00
committed by GitHub
3 changed files with 93 additions and 63 deletions

59
src/routes/huxiu.ts Normal file → Executable file
View File

@@ -1,7 +1,7 @@
import type { RouterData } from "../types.js"; import type { RouterData } from "../types.js";
import type { RouterType } from "../router.types.js"; import type { RouterType } from "../router.types.js";
import { get } from "../utils/getData.js";
import { getTime } from "../utils/getTime.js"; import { getTime } from "../utils/getTime.js";
import axios from "axios";
export const handleRoute = async (_: undefined, noCache: boolean) => { export const handleRoute = async (_: undefined, noCache: boolean) => {
const listData = await getList(noCache); const listData = await getList(noCache);
@@ -16,36 +16,39 @@ export const handleRoute = async (_: undefined, noCache: boolean) => {
return routeData; return routeData;
}; };
// 标题处理
const titleProcessing = (text: string) => {
const paragraphs = text.split("<br><br>");
const title = paragraphs.shift()?.replace(/。$/, "");
const intro = paragraphs.join("<br><br>");
return { title, intro };
};
const getList = async (noCache: boolean) => { const getList = async (noCache: boolean) => {
const url = `https://www.huxiu.com/moment/`; // PC 端接口
const result = await get({ const url = `https://moment-api.huxiu.com/web-v3/moment/feed?platform=www`;
url, const res = await axios.get(url, {
noCache, headers: {
"User-Agent": "Mozilla/5.0",
Referer: "https://www.huxiu.com/moment/",
},
timeout: 10000,
}); });
// 正则查找 const list: RouterType["huxiu"][] = res.data?.data?.moment_list?.datalist || [];
const pattern =
/<script>[\s\S]*?window\.__INITIAL_STATE__\s*=\s*(\{[\s\S]*?\});[\s\S]*?<\/script>/;
const matchResult = result.data.match(pattern);
const jsonObject = JSON.parse(matchResult[1]).moment.momentList.moment_list.datalist;
return { return {
...result, fromCache: false,
data: jsonObject.map((v: RouterType["huxiu"]) => ({ updateTime: new Date().toISOString(),
id: v.object_id, data: list.map((v: RouterType["huxiu"]) => {
title: titleProcessing(v.content).title, const content = (v.content || "").replace(/<br\s*\/?>/gi, "\n");
desc: titleProcessing(v.content).intro, const [titleLine, ...rest] = content
author: v.user_info.username, .split("\n")
.map((s) => s.trim())
.filter(Boolean);
const title = titleLine?.replace(/。$/, "") || "";
const intro = rest.join("\n");
const momentId = v.object_id;
return {
id: momentId,
title,
desc: intro,
author: v.user_info?.username || "",
timestamp: getTime(v.publish_time), timestamp: getTime(v.publish_time),
hot: undefined, hot: v.count_info?.agree_num,
url: v.url || `https://www.huxiu.com/moment/${v.object_id}.html`, url: `https://www.huxiu.com/moment/${momentId}.html`,
mobileUrl: v.url || `https://m.huxiu.com/moment/${v.object_id}.html`, mobileUrl: `https://m.huxiu.com/moment/${momentId}.html`,
})), };
}),
}; };
}; };

47
src/routes/kuaishou.ts Normal file → Executable file
View File

@@ -4,6 +4,8 @@ import { get } from "../utils/getData.js";
import { parseChineseNumber } from "../utils/getNum.js"; import { parseChineseNumber } from "../utils/getNum.js";
import UserAgent from "user-agents"; import UserAgent from "user-agents";
const APOLLO_STATE_PREFIX = "window.__APOLLO_STATE__=";
export const handleRoute = async (_: undefined, noCache: boolean) => { export const handleRoute = async (_: undefined, noCache: boolean) => {
const listData = await getList(noCache); const listData = await getList(noCache);
const routeData: RouterData = { const routeData: RouterData = {
@@ -32,21 +34,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(APOLLO_STATE_PREFIX);
const jsonObject = JSON.parse(matchResult[1])["defaultClient"]; if (start === -1) {
throw new Error("快手页面结构变更,未找到 APOLLO_STATE");
}
const scriptSlice = html.slice(start + APOLLO_STATE_PREFIX.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}`,

View File

@@ -1,16 +1,7 @@
import type { RouterData } from "../types.js"; import type { RouterData } from "../types.js";
import { get } from "../utils/getData.js"; import { get } from "../utils/getData.js";
import { getTime } from "../utils/getTime.js"; import { getTime } from "../utils/getTime.js";
import { parseRSS } from "../utils/parseRSS.js";
interface Topic {
id: number;
title: string;
excerpt: string;
last_poster_username: string;
created_at: string;
views: number;
like_count: number;
}
export const handleRoute = async (_: undefined, noCache: boolean) => { export const handleRoute = async (_: undefined, noCache: boolean) => {
const listData = await getList(noCache); const listData = await getList(noCache);
@@ -19,7 +10,7 @@ export const handleRoute = async (_: undefined, noCache: boolean) => {
title: "Linux.do", title: "Linux.do",
type: "热门文章", type: "热门文章",
description: "Linux 技术社区热搜", description: "Linux 技术社区热搜",
link: "https://linux.do/hot", link: "https://linux.do/top/weekly",
total: listData.data?.length || 0, total: listData.data?.length || 0,
...listData, ...listData,
}; };
@@ -27,31 +18,34 @@ export const handleRoute = async (_: undefined, noCache: boolean) => {
}; };
const getList = async (noCache: boolean) => { const getList = async (noCache: boolean) => {
const url = "https://linux.do/top/weekly.json"; const url = "https://linux.do/top.rss?period=weekly";
const result = await get({ const result = await get({
url, url,
noCache, noCache,
headers: { headers: {
"Accept": "application/json", "Accept": "application/rss+xml, application/xml;q=0.9, */*;q=0.8",
} "User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36",
},
}); });
const topics = result.data.topic_list.topics as Topic[]; const items = await parseRSS(result.data);
const list = topics.map((topic) => { const list = items.map((item, index) => {
const link = item.link || "";
return { return {
id: topic.id, id: item.guid || link || index,
title: topic.title, title: item.title || "",
desc: topic.excerpt, desc: item.contentSnippet?.trim() || item.content?.trim() || "",
author: topic.last_poster_username, author: item.author,
timestamp: getTime(topic.created_at), timestamp: getTime(item.pubDate || 0),
url: `https://linux.do/t/${topic.id}`, url: link,
mobileUrl: `https://linux.do/t/${topic.id}`, mobileUrl: link,
hot: topic.views || topic.like_count hot: undefined,
}; };
}); });
return { return {
...result, ...result,
data: list data: list,
}; };
}; };