feat(init): Phase 1 — 基础架构搭建
- 完成 T-001: Next.js 14+ App Router 项目初始化,配置图片域名白名单 - 完成 T-002: TypeScript 类型定义(ContentItem, Platform, PlatformAdapter) - 完成 T-003: API 代理层路由(热榜 + 详情) - 完成 T-004: TikHub API 客户端与滑动窗口限流器 - 完成 T-005: 抖音平台适配器 - 完成 T-006: TikTok 平台适配器 - 完成 T-007: 小红书平台适配器 - 完成 T-008: 适配器注册表与平台配置 - 完成 T-009: Zustand Store(settings + favorites) - 完成 T-010: 全局布局组件(Header + PlatformTabs) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
import type { ContentItem, PlatformAdapter } from "@/types/content";
|
||||
import { tikhubFetch } from "@/lib/tikhub";
|
||||
|
||||
export class DouyinAdapter implements PlatformAdapter {
|
||||
async fetchTrending(count: number): Promise<ContentItem[]> {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const data = await tikhubFetch<any>(
|
||||
"/api/v1/douyin/web/fetch_hot_search_result"
|
||||
);
|
||||
|
||||
const list =
|
||||
data?.data?.word_list ||
|
||||
data?.data?.trending_list ||
|
||||
data?.data ||
|
||||
[];
|
||||
|
||||
const items = Array.isArray(list) ? list : [];
|
||||
|
||||
return items.slice(0, count).map((item: Record<string, unknown>, index: number) =>
|
||||
this.mapToContentItem(item, index)
|
||||
);
|
||||
}
|
||||
|
||||
async fetchDetail(id: string): Promise<ContentItem> {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const data = await tikhubFetch<any>(
|
||||
"/api/v1/douyin/web/fetch_one_video",
|
||||
{ aweme_id: id }
|
||||
);
|
||||
|
||||
const videoData = data?.data?.aweme_detail || data?.data || {};
|
||||
return this.mapToContentItem(videoData, 0);
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
private mapToContentItem(raw: any, index: number): ContentItem {
|
||||
const stats = raw?.statistics || raw?.stats || {};
|
||||
const author = raw?.author || {};
|
||||
|
||||
return {
|
||||
id: String(raw?.aweme_id || raw?.id || raw?.word || `douyin-${index}`),
|
||||
title: raw?.desc || raw?.word || raw?.sentence || raw?.title || "无标题",
|
||||
cover_url:
|
||||
raw?.video?.cover?.url_list?.[0] ||
|
||||
raw?.video?.dynamic_cover?.url_list?.[0] ||
|
||||
raw?.cover ||
|
||||
undefined,
|
||||
video_url: raw?.video?.play_addr?.url_list?.[0] || undefined,
|
||||
author_name: author?.nickname || raw?.author_name || "未知作者",
|
||||
author_avatar: author?.avatar_thumb?.url_list?.[0] || undefined,
|
||||
play_count: stats?.play_count ?? raw?.hot_value ?? undefined,
|
||||
like_count: stats?.digg_count ?? undefined,
|
||||
comment_count: stats?.comment_count ?? undefined,
|
||||
share_count: stats?.share_count ?? undefined,
|
||||
publish_time: raw?.create_time
|
||||
? new Date(raw.create_time * 1000).toISOString()
|
||||
: new Date().toISOString(),
|
||||
platform: "douyin",
|
||||
original_url:
|
||||
raw?.share_url ||
|
||||
`https://www.douyin.com/video/${raw?.aweme_id || raw?.id || ""}`,
|
||||
tags:
|
||||
raw?.text_extra?.map(
|
||||
(t: { hashtag_name?: string }) => t.hashtag_name
|
||||
).filter(Boolean) || undefined,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import type { Platform, PlatformAdapter } from "@/types/content";
|
||||
import { DouyinAdapter } from "./douyin";
|
||||
import { TikTokAdapter } from "./tiktok";
|
||||
import { XiaohongshuAdapter } from "./xiaohongshu";
|
||||
|
||||
const adapters: Partial<Record<Platform, PlatformAdapter>> = {
|
||||
douyin: new DouyinAdapter(),
|
||||
tiktok: new TikTokAdapter(),
|
||||
xiaohongshu: new XiaohongshuAdapter(),
|
||||
};
|
||||
|
||||
export function getAdapter(platform: Platform): PlatformAdapter | null {
|
||||
const adapter = adapters[platform];
|
||||
if (!adapter) {
|
||||
console.warn(`[adapters] 未找到平台适配器: ${platform}`);
|
||||
return null;
|
||||
}
|
||||
return adapter;
|
||||
}
|
||||
|
||||
export function getSupportedPlatforms(): Platform[] {
|
||||
return Object.keys(adapters) as Platform[];
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
import type { ContentItem, PlatformAdapter } from "@/types/content";
|
||||
import { tikhubFetch } from "@/lib/tikhub";
|
||||
|
||||
export class TikTokAdapter implements PlatformAdapter {
|
||||
async fetchTrending(count: number): Promise<ContentItem[]> {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const data = await tikhubFetch<any>(
|
||||
"/api/v1/tiktok/web/fetch_trending_post"
|
||||
);
|
||||
|
||||
const list =
|
||||
data?.data?.aweme_list ||
|
||||
data?.data?.items ||
|
||||
data?.data ||
|
||||
[];
|
||||
|
||||
const items = Array.isArray(list) ? list : [];
|
||||
|
||||
return items.slice(0, count).map((item: Record<string, unknown>, index: number) =>
|
||||
this.mapToContentItem(item, index)
|
||||
);
|
||||
}
|
||||
|
||||
async fetchDetail(id: string): Promise<ContentItem> {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const data = await tikhubFetch<any>(
|
||||
"/api/v1/tiktok/web/fetch_post_detail",
|
||||
{ aweme_id: id }
|
||||
);
|
||||
|
||||
const videoData =
|
||||
data?.data?.aweme_detail || data?.data?.itemInfo?.itemStruct || data?.data || {};
|
||||
return this.mapToContentItem(videoData, 0);
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
private mapToContentItem(raw: any, index: number): ContentItem {
|
||||
const stats = raw?.statistics || raw?.stats || {};
|
||||
const author = raw?.author || {};
|
||||
|
||||
return {
|
||||
id: String(raw?.aweme_id || raw?.id || `tiktok-${index}`),
|
||||
title: raw?.desc || raw?.title || "Untitled",
|
||||
cover_url:
|
||||
raw?.video?.cover?.url_list?.[0] ||
|
||||
raw?.video?.origin_cover?.url_list?.[0] ||
|
||||
raw?.cover ||
|
||||
undefined,
|
||||
video_url: raw?.video?.play_addr?.url_list?.[0] || undefined,
|
||||
author_name:
|
||||
author?.nickname || author?.unique_id || raw?.author_name || "Unknown",
|
||||
author_avatar:
|
||||
author?.avatar_thumb?.url_list?.[0] ||
|
||||
author?.avatar_medium?.url_list?.[0] ||
|
||||
undefined,
|
||||
play_count: stats?.play_count ?? undefined,
|
||||
like_count: stats?.digg_count ?? undefined,
|
||||
comment_count: stats?.comment_count ?? undefined,
|
||||
share_count: stats?.share_count ?? undefined,
|
||||
publish_time: raw?.create_time
|
||||
? new Date(raw.create_time * 1000).toISOString()
|
||||
: new Date().toISOString(),
|
||||
platform: "tiktok",
|
||||
original_url:
|
||||
raw?.share_url ||
|
||||
`https://www.tiktok.com/@${author?.unique_id || "user"}/video/${raw?.aweme_id || raw?.id || ""}`,
|
||||
tags:
|
||||
raw?.text_extra
|
||||
?.map((t: { hashtag_name?: string }) => t.hashtag_name)
|
||||
.filter(Boolean) || undefined,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
import type { ContentItem, PlatformAdapter } from "@/types/content";
|
||||
import { tikhubFetch } from "@/lib/tikhub";
|
||||
|
||||
export class XiaohongshuAdapter implements PlatformAdapter {
|
||||
async fetchTrending(count: number): Promise<ContentItem[]> {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const data = await tikhubFetch<any>(
|
||||
"/api/v1/xiaohongshu/app/v2/fetch_feed"
|
||||
);
|
||||
|
||||
const list =
|
||||
data?.data?.items ||
|
||||
data?.data?.notes ||
|
||||
data?.data ||
|
||||
[];
|
||||
|
||||
const items = Array.isArray(list) ? list : [];
|
||||
|
||||
return items.slice(0, count).map((item: Record<string, unknown>, index: number) =>
|
||||
this.mapToContentItem(item, index)
|
||||
);
|
||||
}
|
||||
|
||||
async fetchDetail(id: string): Promise<ContentItem> {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const data = await tikhubFetch<any>(
|
||||
"/api/v1/xiaohongshu/app/v2/fetch_note_detail",
|
||||
{ note_id: id }
|
||||
);
|
||||
|
||||
const noteData =
|
||||
data?.data?.note_list?.[0] ||
|
||||
data?.data?.items?.[0] ||
|
||||
data?.data || {};
|
||||
return this.mapToContentItem(noteData, 0);
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
private mapToContentItem(raw: any, index: number): ContentItem {
|
||||
const note = raw?.note_card || raw?.note || raw;
|
||||
const user = note?.user || raw?.user || {};
|
||||
const interactInfo = note?.interact_info || {};
|
||||
|
||||
return {
|
||||
id: String(note?.note_id || raw?.id || `xhs-${index}`),
|
||||
title:
|
||||
note?.display_title || note?.title || note?.desc || raw?.title || "无标题",
|
||||
cover_url:
|
||||
note?.cover?.url ||
|
||||
note?.cover?.url_default ||
|
||||
note?.image_list?.[0]?.url ||
|
||||
raw?.cover?.url ||
|
||||
undefined,
|
||||
video_url: note?.video?.url || undefined,
|
||||
author_name: user?.nickname || user?.name || "未知作者",
|
||||
author_avatar: user?.avatar || user?.image || undefined,
|
||||
play_count: undefined, // 小红书通常不展示播放量
|
||||
like_count:
|
||||
interactInfo?.liked_count ??
|
||||
note?.liked_count ??
|
||||
raw?.likes ??
|
||||
undefined,
|
||||
comment_count:
|
||||
interactInfo?.comment_count ??
|
||||
note?.comment_count ??
|
||||
undefined,
|
||||
share_count:
|
||||
interactInfo?.share_count ??
|
||||
note?.share_count ??
|
||||
undefined,
|
||||
publish_time: note?.time
|
||||
? new Date(note.time * 1000).toISOString()
|
||||
: raw?.timestamp
|
||||
? new Date(raw.timestamp * 1000).toISOString()
|
||||
: new Date().toISOString(),
|
||||
platform: "xiaohongshu",
|
||||
original_url: `https://www.xiaohongshu.com/explore/${note?.note_id || raw?.id || ""}`,
|
||||
tags:
|
||||
note?.tag_list?.map(
|
||||
(t: { name?: string }) => t.name
|
||||
).filter(Boolean) || undefined,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
import type { PlatformConfig } from "@/types/content";
|
||||
|
||||
export const MVP_PLATFORMS: PlatformConfig[] = [
|
||||
{
|
||||
id: "douyin",
|
||||
name: "抖音",
|
||||
icon: "📱",
|
||||
color: "#000000",
|
||||
enabled: true,
|
||||
endpoints: {
|
||||
trending: "/api/v1/douyin/web/fetch_hot_search_result",
|
||||
detail: "/api/v1/douyin/web/fetch_one_video",
|
||||
},
|
||||
},
|
||||
{
|
||||
id: "tiktok",
|
||||
name: "TikTok",
|
||||
icon: "🎵",
|
||||
color: "#00F2EA",
|
||||
enabled: true,
|
||||
endpoints: {
|
||||
trending: "/api/v1/tiktok/web/fetch_trending_post",
|
||||
detail: "/api/v1/tiktok/web/fetch_post_detail",
|
||||
},
|
||||
},
|
||||
{
|
||||
id: "xiaohongshu",
|
||||
name: "小红书",
|
||||
icon: "📕",
|
||||
color: "#FF2442",
|
||||
enabled: true,
|
||||
endpoints: {
|
||||
trending: "/api/v1/xiaohongshu/app/v2/fetch_feed",
|
||||
detail: "/api/v1/xiaohongshu/app/v2/fetch_note_detail",
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
export function getPlatformConfig(platformId: string): PlatformConfig | undefined {
|
||||
return MVP_PLATFORMS.find((p) => p.id === platformId);
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
const WINDOW_MS = 1000;
|
||||
const MAX_REQUESTS = 10;
|
||||
|
||||
const timestamps: number[] = [];
|
||||
|
||||
export function canMakeRequest(): boolean {
|
||||
const now = Date.now();
|
||||
// Remove timestamps outside the window
|
||||
while (timestamps.length > 0 && timestamps[0] <= now - WINDOW_MS) {
|
||||
timestamps.shift();
|
||||
}
|
||||
return timestamps.length < MAX_REQUESTS;
|
||||
}
|
||||
|
||||
export function recordRequest(): void {
|
||||
timestamps.push(Date.now());
|
||||
}
|
||||
|
||||
export async function waitForSlot(): Promise<void> {
|
||||
while (!canMakeRequest()) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 100));
|
||||
}
|
||||
recordRequest();
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
import { waitForSlot } from "./rate-limiter";
|
||||
|
||||
const TIKHUB_BASE_URL = "https://api.tikhub.io";
|
||||
|
||||
// Runtime API Key (set via POST /api/settings)
|
||||
let runtimeApiKey: string | null = null;
|
||||
|
||||
export function setRuntimeApiKey(key: string) {
|
||||
runtimeApiKey = key;
|
||||
}
|
||||
|
||||
export function getApiKey(): string | null {
|
||||
return runtimeApiKey || process.env.TIKHUB_API_KEY || null;
|
||||
}
|
||||
|
||||
export async function tikhubFetch<T>(
|
||||
endpoint: string,
|
||||
params?: Record<string, string>
|
||||
): Promise<T> {
|
||||
const apiKey = getApiKey();
|
||||
if (!apiKey) {
|
||||
throw new TikHubError(401, "API Key 未配置,请在设置页面配置 TikHub API Key");
|
||||
}
|
||||
|
||||
await waitForSlot();
|
||||
|
||||
const url = new URL(endpoint, TIKHUB_BASE_URL);
|
||||
if (params) {
|
||||
Object.entries(params).forEach(([k, v]) => url.searchParams.set(k, v));
|
||||
}
|
||||
|
||||
const res = await fetch(url.toString(), {
|
||||
headers: {
|
||||
Authorization: `Bearer ${apiKey}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
cache: "no-store",
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
if (res.status === 401) {
|
||||
throw new TikHubError(401, "API Key 无效,请检查配置");
|
||||
}
|
||||
if (res.status === 429) {
|
||||
throw new TikHubError(429, "请求过于频繁,请稍后重试");
|
||||
}
|
||||
throw new TikHubError(res.status, `TikHub API 错误: ${res.status}`);
|
||||
}
|
||||
|
||||
return res.json();
|
||||
}
|
||||
|
||||
export class TikHubError extends Error {
|
||||
constructor(
|
||||
public statusCode: number,
|
||||
message: string
|
||||
) {
|
||||
super(message);
|
||||
this.name = "TikHubError";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
import { clsx, type ClassValue } from "clsx"
|
||||
import { twMerge } from "tailwind-merge"
|
||||
|
||||
export function cn(...inputs: ClassValue[]) {
|
||||
return twMerge(clsx(inputs))
|
||||
}
|
||||
Reference in New Issue
Block a user