'小红书解析
This commit is contained in:
63
app/api/xiaohongshu/profile/route.ts
Normal file
63
app/api/xiaohongshu/profile/route.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { resolvePaymentApiUrl } from "@/config/domain.config";
|
||||
|
||||
export const maxDuration = 60;
|
||||
|
||||
/** 开发环境优先使用本地 salonapi,避免 PAYMENT_API_URL 指向远程时 404 */
|
||||
function getSalonApiBase(): string {
|
||||
if (process.env.NODE_ENV === "development") {
|
||||
const port = process.env.PAYJSAPI_PORT || "8007";
|
||||
const host = process.env.SALONAPI_HOST || "127.0.0.1";
|
||||
return `http://${host}:${port}`;
|
||||
}
|
||||
return resolvePaymentApiUrl();
|
||||
}
|
||||
|
||||
/** 代理到 salonapi 获取小红书用户资料 */
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const body = await request.json();
|
||||
const url = String(body?.url || "").trim();
|
||||
|
||||
if (!url) {
|
||||
return NextResponse.json({ ok: false, error: "请输入小红书链接" }, { status: 400 });
|
||||
}
|
||||
|
||||
const apiBase = getSalonApiBase();
|
||||
const res = await fetch(`${apiBase}/api/salon/xiaohongshu/profile`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ url }),
|
||||
signal: AbortSignal.timeout(50000),
|
||||
});
|
||||
|
||||
const data = await res.json().catch(() => ({}));
|
||||
|
||||
if (res.ok && data?.ok) {
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
nickname: data.nickname,
|
||||
gender: data.gender,
|
||||
avatarUrl: data.avatarUrl ?? undefined,
|
||||
followers: data.followersText ?? data.followers ?? 0,
|
||||
followersText: data.followersText ?? undefined,
|
||||
following: data.followingText ?? data.following ?? 0,
|
||||
followingText: data.followingText ?? undefined,
|
||||
likesAndCollects: data.likesAndCollectsText ?? data.likesAndCollects ?? 0,
|
||||
likesAndCollectsText: data.likesAndCollectsText ?? undefined,
|
||||
postCount: data.postCount ?? 0,
|
||||
postCountText: data.postCountText ?? undefined,
|
||||
redbookId: data.redbookId ?? undefined,
|
||||
ipLocation: data.ipLocation ?? undefined,
|
||||
personalLink: data.personalLink ?? undefined,
|
||||
resolvedUrl: data.resolvedUrl ?? undefined,
|
||||
});
|
||||
}
|
||||
|
||||
const error = data?.detail ?? data?.error ?? "无法解析用户资料,请确认链接有效";
|
||||
return NextResponse.json({ ok: false, error }, { status: res.status >= 400 ? res.status : 400 });
|
||||
} catch (e) {
|
||||
const msg = e instanceof Error ? e.message : "获取资料失败";
|
||||
return NextResponse.json({ ok: false, error: msg }, { status: 500 });
|
||||
}
|
||||
}
|
||||
29
app/api/xiaohongshu/validate/route.ts
Normal file
29
app/api/xiaohongshu/validate/route.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
|
||||
/** 支持:xiaohongshu.com/user/profile/xxx、xhslink.com/user/profile/xxx、xhslink.com/m/xxx 短链 */
|
||||
const XH_PROFILE_PATTERN = /^https?:\/\/(www\.)?(xiaohongshu\.com|xhslink\.com)\/user\/profile\/[a-zA-Z0-9_-]+/i;
|
||||
const XH_SHORT_PATTERN = /^https?:\/\/xhslink\.com\/m\/[a-zA-Z0-9_-]+/i;
|
||||
|
||||
function isValidUrl(url: string): boolean {
|
||||
return XH_PROFILE_PATTERN.test(url) || XH_SHORT_PATTERN.test(url);
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const body = await request.json();
|
||||
const url = String(body?.url || "").trim();
|
||||
|
||||
if (!url) {
|
||||
return NextResponse.json({ valid: false, error: "请输入小红书链接" }, { status: 400 });
|
||||
}
|
||||
|
||||
const valid = isValidUrl(url);
|
||||
if (!valid) {
|
||||
return NextResponse.json({ valid: false, error: "小红书link异常" }, { status: 200 });
|
||||
}
|
||||
|
||||
return NextResponse.json({ valid: true });
|
||||
} catch {
|
||||
return NextResponse.json({ valid: false, error: "校验失败" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user