mirror of
https://github.com/imsyy/DailyHotApi.git
synced 2026-01-12 13:14:55 +08:00
Compare commits
13 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d18d97f72d | ||
|
|
61f89ed13d | ||
|
|
7dc310de43 | ||
|
|
7270d9cf16 | ||
|
|
17d1658818 | ||
|
|
82f983793c | ||
|
|
772f421157 | ||
|
|
c190efd005 | ||
|
|
9801c4c7ff | ||
|
|
3298e734a0 | ||
|
|
0a3869f1a2 | ||
|
|
10db165fc4 | ||
|
|
faa6f0225d |
@@ -15,6 +15,7 @@ DISALLOW_ROBOT=true
|
||||
REDIS_HOST="127.0.0.1"
|
||||
REDIS_PORT=6379
|
||||
REDIS_PASSWORD=""
|
||||
REDIS_DB=0
|
||||
|
||||
# 缓存时长( 秒 )
|
||||
CACHE_TTL=3600
|
||||
@@ -27,3 +28,6 @@ USE_LOG_FILE=true
|
||||
|
||||
# RSS Mode
|
||||
RSS_MODE=false
|
||||
|
||||
# Weibo
|
||||
FILTER_WEIBO_ADVERTISEMENT=false
|
||||
@@ -15,7 +15,9 @@ export type Config = {
|
||||
REDIS_HOST: string;
|
||||
REDIS_PORT: number;
|
||||
REDIS_PASSWORD: string;
|
||||
REDIS_DB: number;
|
||||
ZHIHU_COOKIE: string;
|
||||
FILTER_WEIBO_ADVERTISEMENT: boolean;
|
||||
};
|
||||
|
||||
// 验证并提取环境变量
|
||||
@@ -52,5 +54,7 @@ export const config: Config = {
|
||||
REDIS_HOST: getEnvVariable("REDIS_HOST") || "127.0.0.1",
|
||||
REDIS_PORT: getNumericEnvVariable("REDIS_PORT", 6379),
|
||||
REDIS_PASSWORD: getEnvVariable("REDIS_PASSWORD") || "",
|
||||
REDIS_DB: getNumericEnvVariable("REDIS_DB", 0),
|
||||
ZHIHU_COOKIE: getEnvVariable("ZHIHU_COOKIE") || "",
|
||||
FILTER_WEIBO_ADVERTISEMENT: getBooleanEnvVariable("FILTER_WEIBO_ADVERTISEMENT", false),
|
||||
};
|
||||
|
||||
4
src/router.types.d.ts
vendored
4
src/router.types.d.ts
vendored
@@ -89,8 +89,10 @@ export type RouterType = {
|
||||
word_scheme: string;
|
||||
note: string;
|
||||
flag_desc: string;
|
||||
num: number;
|
||||
// num: number;
|
||||
desc_extr: number;
|
||||
onboard_time: number;
|
||||
pic: string;
|
||||
};
|
||||
zhihu: {
|
||||
target: {
|
||||
|
||||
59
src/routes/huxiu.ts
Normal file → Executable file
59
src/routes/huxiu.ts
Normal file → Executable file
@@ -1,7 +1,7 @@
|
||||
import type { RouterData } from "../types.js";
|
||||
import type { RouterType } from "../router.types.js";
|
||||
import { get } from "../utils/getData.js";
|
||||
import { getTime } from "../utils/getTime.js";
|
||||
import axios from "axios";
|
||||
|
||||
export const handleRoute = async (_: undefined, noCache: boolean) => {
|
||||
const listData = await getList(noCache);
|
||||
@@ -16,36 +16,39 @@ export const handleRoute = async (_: undefined, noCache: boolean) => {
|
||||
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 url = `https://www.huxiu.com/moment/`;
|
||||
const result = await get({
|
||||
url,
|
||||
noCache,
|
||||
// PC 端接口
|
||||
const url = `https://moment-api.huxiu.com/web-v3/moment/feed?platform=www`;
|
||||
const res = await axios.get(url, {
|
||||
headers: {
|
||||
"User-Agent": "Mozilla/5.0",
|
||||
Referer: "https://www.huxiu.com/moment/",
|
||||
},
|
||||
timeout: 10000,
|
||||
});
|
||||
// 正则查找
|
||||
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;
|
||||
const list: RouterType["huxiu"][] = res.data?.data?.moment_list?.datalist || [];
|
||||
return {
|
||||
...result,
|
||||
data: jsonObject.map((v: RouterType["huxiu"]) => ({
|
||||
id: v.object_id,
|
||||
title: titleProcessing(v.content).title,
|
||||
desc: titleProcessing(v.content).intro,
|
||||
author: v.user_info.username,
|
||||
fromCache: false,
|
||||
updateTime: new Date().toISOString(),
|
||||
data: list.map((v: RouterType["huxiu"]) => {
|
||||
const content = (v.content || "").replace(/<br\s*\/?>/gi, "\n");
|
||||
const [titleLine, ...rest] = content
|
||||
.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),
|
||||
hot: undefined,
|
||||
url: v.url || `https://www.huxiu.com/moment/${v.object_id}.html`,
|
||||
mobileUrl: v.url || `https://m.huxiu.com/moment/${v.object_id}.html`,
|
||||
})),
|
||||
hot: v.count_info?.agree_num,
|
||||
url: `https://www.huxiu.com/moment/${momentId}.html`,
|
||||
mobileUrl: `https://m.huxiu.com/moment/${momentId}.html`,
|
||||
};
|
||||
}),
|
||||
};
|
||||
};
|
||||
|
||||
47
src/routes/kuaishou.ts
Normal file → Executable file
47
src/routes/kuaishou.ts
Normal file → Executable file
@@ -4,6 +4,8 @@ import { get } from "../utils/getData.js";
|
||||
import { parseChineseNumber } from "../utils/getNum.js";
|
||||
import UserAgent from "user-agents";
|
||||
|
||||
const APOLLO_STATE_PREFIX = "window.__APOLLO_STATE__=";
|
||||
|
||||
export const handleRoute = async (_: undefined, noCache: boolean) => {
|
||||
const listData = await getList(noCache);
|
||||
const routeData: RouterData = {
|
||||
@@ -32,21 +34,52 @@ const getList = async (noCache: boolean) => {
|
||||
});
|
||||
const listData: ListItem[] = [];
|
||||
// 获取主要内容
|
||||
const pattern = /window.__APOLLO_STATE__=(.*);\(function\(\)/s;
|
||||
const matchResult = result.data?.match(pattern);
|
||||
const jsonObject = JSON.parse(matchResult[1])["defaultClient"];
|
||||
const html = result.data || "";
|
||||
const start = html.indexOf(APOLLO_STATE_PREFIX);
|
||||
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];
|
||||
if (!hotItem) return;
|
||||
const id = hotItem.photoIds?.json?.[0];
|
||||
const hotValue = hotItem.hotValue ?? "";
|
||||
const poster = hotItem.poster ? decodeURIComponent(hotItem.poster) : undefined;
|
||||
listData.push({
|
||||
id: hotItem.id,
|
||||
title: hotItem.name,
|
||||
cover: decodeURIComponent(hotItem.poster),
|
||||
hot: parseChineseNumber(hotItem.hotValue),
|
||||
cover: poster,
|
||||
hot: parseChineseNumber(String(hotValue)),
|
||||
timestamp: undefined,
|
||||
url: `https://www.kuaishou.com/short-video/${id}`,
|
||||
mobileUrl: `https://www.kuaishou.com/short-video/${id}`,
|
||||
|
||||
@@ -1,16 +1,7 @@
|
||||
import type { RouterData } from "../types.js";
|
||||
import { get } from "../utils/getData.js";
|
||||
import { getTime } from "../utils/getTime.js";
|
||||
|
||||
interface Topic {
|
||||
id: number;
|
||||
title: string;
|
||||
excerpt: string;
|
||||
last_poster_username: string;
|
||||
created_at: string;
|
||||
views: number;
|
||||
like_count: number;
|
||||
}
|
||||
import { parseRSS } from "../utils/parseRSS.js";
|
||||
|
||||
export const handleRoute = async (_: undefined, noCache: boolean) => {
|
||||
const listData = await getList(noCache);
|
||||
@@ -19,7 +10,7 @@ export const handleRoute = async (_: undefined, noCache: boolean) => {
|
||||
title: "Linux.do",
|
||||
type: "热门文章",
|
||||
description: "Linux 技术社区热搜",
|
||||
link: "https://linux.do/hot",
|
||||
link: "https://linux.do/top/weekly",
|
||||
total: listData.data?.length || 0,
|
||||
...listData,
|
||||
};
|
||||
@@ -27,31 +18,34 @@ export const handleRoute = async (_: undefined, 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({
|
||||
url,
|
||||
noCache,
|
||||
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 list = topics.map((topic) => {
|
||||
const items = await parseRSS(result.data);
|
||||
const list = items.map((item, index) => {
|
||||
const link = item.link || "";
|
||||
return {
|
||||
id: topic.id,
|
||||
title: topic.title,
|
||||
desc: topic.excerpt,
|
||||
author: topic.last_poster_username,
|
||||
timestamp: getTime(topic.created_at),
|
||||
url: `https://linux.do/t/${topic.id}`,
|
||||
mobileUrl: `https://linux.do/t/${topic.id}`,
|
||||
hot: topic.views || topic.like_count
|
||||
id: item.guid || link || index,
|
||||
title: item.title || "",
|
||||
desc: item.contentSnippet?.trim() || item.content?.trim() || "",
|
||||
author: item.author,
|
||||
timestamp: getTime(item.pubDate || 0),
|
||||
url: link,
|
||||
mobileUrl: link,
|
||||
hot: undefined,
|
||||
};
|
||||
});
|
||||
|
||||
return {
|
||||
...result,
|
||||
data: list
|
||||
data: list,
|
||||
};
|
||||
};
|
||||
@@ -2,6 +2,7 @@ import type { RouterData } from "../types.js";
|
||||
import type { RouterType } from "../router.types.js";
|
||||
import { get } from "../utils/getData.js";
|
||||
import { getTime } from "../utils/getTime.js";
|
||||
import { config } from "../config";
|
||||
|
||||
export const handleRoute = async (_: undefined, noCache: boolean) => {
|
||||
const listData = await getList(noCache);
|
||||
@@ -18,35 +19,35 @@ export const handleRoute = async (_: undefined, noCache: boolean) => {
|
||||
};
|
||||
|
||||
const getList = async (noCache: boolean) => {
|
||||
const url =
|
||||
"https://m.weibo.cn/api/container/getIndex?containerid=106003type%3D25%26t%3D3%26disable_hot%3D1%26filter_type%3Drealtimehot&title=%E5%BE%AE%E5%8D%9A%E7%83%AD%E6%90%9C&extparam=filter_type%3Drealtimehot%26mi_cid%3D100103%26pos%3D0_0%26c_type%3D30%26display_time%3D1540538388&luicode=10000011&lfid=231583";
|
||||
const url = "https://weibo.com/ajax/side/hotSearch";
|
||||
|
||||
const result = await get({
|
||||
url,
|
||||
noCache,
|
||||
ttl: 60,
|
||||
headers: {
|
||||
Referer: "https://s.weibo.com/top/summary?cate=realtimehot",
|
||||
"MWeibo-Pwa": "1",
|
||||
"X-Requested-With": "XMLHttpRequest",
|
||||
Referer: "https://weibo.com/",
|
||||
"User-Agent":
|
||||
"Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1",
|
||||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
|
||||
},
|
||||
});
|
||||
const list = result.data.data.cards?.[0]?.card_group;
|
||||
|
||||
if (!result.data?.data?.realtime) {
|
||||
return { ...result, data: [] };
|
||||
}
|
||||
|
||||
const list = result.data.data.realtime;
|
||||
return {
|
||||
...result,
|
||||
data: list.map((v: RouterType["weibo"]) => {
|
||||
const key = v.word_scheme ? v.word_scheme : `#${v.desc}`;
|
||||
data: list.map((v: any, index: number) => {
|
||||
const title = v.word || v.word_scheme || `热搜${index + 1}`;
|
||||
return {
|
||||
id: v.itemid,
|
||||
title: v.desc,
|
||||
desc: key,
|
||||
// author: v.flag_desc,
|
||||
timestamp: getTime(v.onboard_time),
|
||||
// hot: v.num,
|
||||
url: `https://s.weibo.com/weibo?q=${encodeURIComponent(key)}&t=31&band_rank=1&Refer=top`,
|
||||
mobileUrl: v?.scheme,
|
||||
id: v.mid || v.word_scheme || `weibo-${index}`,
|
||||
title: title,
|
||||
desc: v.word_scheme || `#${title}#`,
|
||||
timestamp: getTime(v.onboard_time || Date.now()),
|
||||
url: `https://s.weibo.com/weibo?q=${encodeURIComponent(title)}`,
|
||||
mobileUrl: `https://s.weibo.com/weibo?q=${encodeURIComponent(title)}`,
|
||||
};
|
||||
}),
|
||||
};
|
||||
|
||||
@@ -26,6 +26,7 @@ const redis = new Redis({
|
||||
host: config.REDIS_HOST,
|
||||
port: config.REDIS_PORT,
|
||||
password: config.REDIS_PASSWORD,
|
||||
db: config.REDIS_DB,
|
||||
maxRetriesPerRequest: 5,
|
||||
// 重试策略:最小延迟 50ms,最大延迟 2s
|
||||
retryStrategy: (times) => Math.min(times * 50, 2000),
|
||||
|
||||
Reference in New Issue
Block a user