mirror of
https://github.com/zimplexing/OrionTV.git
synced 2026-02-24 04:04:40 +08:00
Update
This commit is contained in:
80
backend/src/config/index.ts
Normal file
80
backend/src/config/index.ts
Normal file
@@ -0,0 +1,80 @@
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
|
||||
export interface ApiSite {
|
||||
key: string;
|
||||
api: string;
|
||||
name: string;
|
||||
detail?: string;
|
||||
}
|
||||
|
||||
export interface StorageConfig {
|
||||
type: "localstorage" | "database";
|
||||
database?: {
|
||||
host?: string;
|
||||
port?: number;
|
||||
username?: string;
|
||||
password?: string;
|
||||
database?: string;
|
||||
};
|
||||
}
|
||||
|
||||
export interface Config {
|
||||
cache_time?: number;
|
||||
api_site: {
|
||||
[key: string]: ApiSite;
|
||||
};
|
||||
storage?: StorageConfig;
|
||||
}
|
||||
|
||||
export const API_CONFIG = {
|
||||
search: {
|
||||
path: "?ac=videolist&wd=",
|
||||
pagePath: "?ac=videolist&wd={query}&pg={page}",
|
||||
headers: {
|
||||
"User-Agent":
|
||||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",
|
||||
Accept: "application/json",
|
||||
},
|
||||
},
|
||||
detail: {
|
||||
path: "?ac=videolist&ids=",
|
||||
headers: {
|
||||
"User-Agent":
|
||||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",
|
||||
Accept: "application/json",
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
// Adjust path to read from project root, not from `backend/`
|
||||
const configPath = path.join(process.cwd(), "config.json");
|
||||
let cachedConfig: Config;
|
||||
|
||||
try {
|
||||
cachedConfig = JSON.parse(fs.readFileSync(configPath, "utf-8")) as Config;
|
||||
} catch (error) {
|
||||
console.error(`Error reading or parsing config.json at ${configPath}`, error);
|
||||
// Provide a default fallback config to prevent crashes
|
||||
cachedConfig = {
|
||||
api_site: {},
|
||||
cache_time: 300,
|
||||
};
|
||||
}
|
||||
|
||||
export function getConfig(): Config {
|
||||
return cachedConfig;
|
||||
}
|
||||
|
||||
export function getCacheTime(): number {
|
||||
const config = getConfig();
|
||||
return config.cache_time || 300; // 默认5分钟缓存
|
||||
}
|
||||
|
||||
export function getApiSites(): ApiSite[] {
|
||||
const config = getConfig();
|
||||
return Object.entries(config.api_site).map(([key, site]) => ({
|
||||
...site,
|
||||
key,
|
||||
}));
|
||||
}
|
||||
28
backend/src/index.ts
Normal file
28
backend/src/index.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import express, { Express, Request, Response } from "express";
|
||||
import cors from "cors";
|
||||
import dotenv from "dotenv";
|
||||
|
||||
dotenv.config();
|
||||
|
||||
const app: Express = express();
|
||||
const port = process.env.PORT || 3001;
|
||||
|
||||
// Middlewares
|
||||
app.use(cors());
|
||||
app.use(express.json());
|
||||
|
||||
// Health check route
|
||||
app.get("/", (req: Request, res: Response) => {
|
||||
res.send("MyTV Backend Service is running!");
|
||||
});
|
||||
|
||||
import apiRouter from "./routes";
|
||||
|
||||
// API routes
|
||||
app.use("/api", apiRouter);
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`[server]: Server is running at http://localhost:${port}`);
|
||||
});
|
||||
|
||||
export default app;
|
||||
186
backend/src/routes/detail.ts
Normal file
186
backend/src/routes/detail.ts
Normal file
@@ -0,0 +1,186 @@
|
||||
import { Router, Request, Response } from "express";
|
||||
import { API_CONFIG, ApiSite, getApiSites, getCacheTime } from "../config";
|
||||
import { VideoDetail } from "../types";
|
||||
import { cleanHtmlTags } from "../utils";
|
||||
|
||||
const router = Router();
|
||||
|
||||
// Match m3u8 links
|
||||
const M3U8_PATTERN = /(https?:\/\/[^"'\s]+?\.m3u8)/g;
|
||||
|
||||
async function handleSpecialSourceDetail(
|
||||
id: string,
|
||||
apiSite: ApiSite
|
||||
): Promise<VideoDetail> {
|
||||
const detailUrl = `${apiSite.detail}/index.php/vod/detail/id/${id}.html`;
|
||||
const controller = new AbortController();
|
||||
const timeoutId = setTimeout(() => controller.abort(), 10000);
|
||||
|
||||
const response = await fetch(detailUrl, {
|
||||
headers: API_CONFIG.detail.headers,
|
||||
signal: controller.signal,
|
||||
});
|
||||
clearTimeout(timeoutId);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`详情页请求失败: ${response.status}`);
|
||||
}
|
||||
|
||||
const html = await response.text();
|
||||
let matches: string[] = [];
|
||||
|
||||
if (apiSite.key === "ffzy") {
|
||||
const ffzyPattern =
|
||||
/\$(https?:\/\/[^"'\s]+?\/\d{8}\/\d+_[a-f0-9]+\/index\.m3u8)/g;
|
||||
matches = html.match(ffzyPattern) || [];
|
||||
}
|
||||
|
||||
if (matches.length === 0) {
|
||||
const generalPattern = /\$(https?:\/\/[^"'\s]+?\.m3u8)/g;
|
||||
matches = html.match(generalPattern) || [];
|
||||
}
|
||||
|
||||
matches = Array.from(new Set(matches)).map((link: string) => {
|
||||
link = link.substring(1);
|
||||
const parenIndex = link.indexOf("(");
|
||||
return parenIndex > 0 ? link.substring(0, parenIndex) : link;
|
||||
});
|
||||
|
||||
const titleMatch = html.match(/<h1[^>]*>([^<]+)<\/h1>/);
|
||||
const titleText = titleMatch ? titleMatch[1].trim() : "";
|
||||
const descMatch = html.match(
|
||||
/<div[^>]*class=["']sketch["'][^>]*>([\s\S]*?)<\/div>/
|
||||
);
|
||||
const descText = descMatch ? cleanHtmlTags(descMatch[1]) : "";
|
||||
const coverMatch = html.match(/(https?:\/\/[^"'\s]+?\.jpg)/g);
|
||||
const coverUrl = coverMatch ? coverMatch[0].trim() : "";
|
||||
|
||||
return {
|
||||
code: 200,
|
||||
episodes: matches,
|
||||
detailUrl,
|
||||
videoInfo: {
|
||||
title: titleText,
|
||||
cover: coverUrl,
|
||||
desc: descText,
|
||||
source_name: apiSite.name,
|
||||
source: apiSite.key,
|
||||
id,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async function getDetailFromApi(
|
||||
apiSite: ApiSite,
|
||||
id: string
|
||||
): Promise<VideoDetail> {
|
||||
const detailUrl = `${apiSite.api}${API_CONFIG.detail.path}${id}`;
|
||||
const controller = new AbortController();
|
||||
const timeoutId = setTimeout(() => controller.abort(), 10000);
|
||||
|
||||
const response = await fetch(detailUrl, {
|
||||
headers: API_CONFIG.detail.headers,
|
||||
signal: controller.signal,
|
||||
});
|
||||
clearTimeout(timeoutId);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`详情请求失败: ${response.status}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
if (
|
||||
!data ||
|
||||
!data.list ||
|
||||
!Array.isArray(data.list) ||
|
||||
data.list.length === 0
|
||||
) {
|
||||
throw new Error("获取到的详情内容无效");
|
||||
}
|
||||
|
||||
const videoDetail = data.list[0];
|
||||
let episodes: string[] = [];
|
||||
|
||||
if (videoDetail.vod_play_url) {
|
||||
const playSources = videoDetail.vod_play_url.split("$$$");
|
||||
if (playSources.length > 0) {
|
||||
const mainSource = playSources[0];
|
||||
const episodeList = mainSource.split("#");
|
||||
episodes = episodeList
|
||||
.map((ep: string) => {
|
||||
const parts = ep.split("$");
|
||||
return parts.length > 1 ? parts[1] : "";
|
||||
})
|
||||
.filter(
|
||||
(url: string) =>
|
||||
url && (url.startsWith("http://") || url.startsWith("https://"))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (episodes.length === 0 && videoDetail.vod_content) {
|
||||
const matches = videoDetail.vod_content.match(M3U8_PATTERN) || [];
|
||||
episodes = matches.map((link: string) => link.replace(/^\$/, ""));
|
||||
}
|
||||
|
||||
return {
|
||||
code: 200,
|
||||
episodes,
|
||||
detailUrl,
|
||||
videoInfo: {
|
||||
title: videoDetail.vod_name,
|
||||
cover: videoDetail.vod_pic,
|
||||
desc: cleanHtmlTags(videoDetail.vod_content),
|
||||
type: videoDetail.type_name,
|
||||
year: videoDetail.vod_year?.match(/\d{4}/)?.[0] || "",
|
||||
area: videoDetail.vod_area,
|
||||
director: videoDetail.vod_director,
|
||||
actor: videoDetail.vod_actor,
|
||||
remarks: videoDetail.vod_remarks,
|
||||
source_name: apiSite.name,
|
||||
source: apiSite.key,
|
||||
id,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async function getVideoDetail(
|
||||
id: string,
|
||||
sourceCode: string
|
||||
): Promise<VideoDetail> {
|
||||
if (!id) {
|
||||
throw new Error("缺少视频ID参数");
|
||||
}
|
||||
if (!/^[\w-]+$/.test(id)) {
|
||||
throw new Error("无效的视频ID格式");
|
||||
}
|
||||
const apiSites = getApiSites();
|
||||
const apiSite = apiSites.find((site) => site.key === sourceCode);
|
||||
if (!apiSite) {
|
||||
throw new Error("无效的API来源");
|
||||
}
|
||||
if (apiSite.detail) {
|
||||
return handleSpecialSourceDetail(id, apiSite);
|
||||
}
|
||||
return getDetailFromApi(apiSite, id);
|
||||
}
|
||||
|
||||
router.get("/", async (req: Request, res: Response) => {
|
||||
const id = req.query.id as string;
|
||||
const sourceCode = req.query.source as string;
|
||||
|
||||
if (!id || !sourceCode) {
|
||||
return res.status(400).json({ error: "缺少必要参数" });
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await getVideoDetail(id, sourceCode);
|
||||
const cacheTime = getCacheTime();
|
||||
res.setHeader("Cache-Control", `public, max-age=${cacheTime}`);
|
||||
res.json(result);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: (error as Error).message });
|
||||
}
|
||||
});
|
||||
|
||||
export default router;
|
||||
161
backend/src/routes/douban.ts
Normal file
161
backend/src/routes/douban.ts
Normal file
@@ -0,0 +1,161 @@
|
||||
import { Router, Request, Response } from "express";
|
||||
import { getCacheTime } from "../config";
|
||||
|
||||
const router = Router();
|
||||
|
||||
// --- Interfaces ---
|
||||
interface DoubanItem {
|
||||
title: string;
|
||||
poster: string;
|
||||
rate: string;
|
||||
}
|
||||
|
||||
interface DoubanResponse {
|
||||
code: number;
|
||||
message: string;
|
||||
list: DoubanItem[];
|
||||
}
|
||||
|
||||
interface DoubanApiResponse {
|
||||
subjects: Array<{
|
||||
title: string;
|
||||
cover: string;
|
||||
rate: string;
|
||||
}>;
|
||||
}
|
||||
|
||||
// --- Helper Functions ---
|
||||
|
||||
async function fetchDoubanData(url: string): Promise<DoubanApiResponse> {
|
||||
const controller = new AbortController();
|
||||
const timeoutId = setTimeout(() => controller.abort(), 10000);
|
||||
|
||||
const fetchOptions = {
|
||||
signal: controller.signal,
|
||||
headers: {
|
||||
"User-Agent":
|
||||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36",
|
||||
Referer: "https://movie.douban.com/",
|
||||
Accept: "application/json, text/plain, */*",
|
||||
},
|
||||
};
|
||||
|
||||
try {
|
||||
const response = await fetch(url, fetchOptions);
|
||||
clearTimeout(timeoutId);
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! Status: ${response.status}`);
|
||||
}
|
||||
return await response.json();
|
||||
} catch (error) {
|
||||
clearTimeout(timeoutId);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async function handleTop250(pageStart: number, res: Response) {
|
||||
const target = `https://movie.douban.com/top250?start=${pageStart}&filter=`;
|
||||
const controller = new AbortController();
|
||||
const timeoutId = setTimeout(() => controller.abort(), 10000);
|
||||
|
||||
const fetchOptions = {
|
||||
signal: controller.signal,
|
||||
headers: {
|
||||
"User-Agent":
|
||||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36",
|
||||
Referer: "https://movie.douban.com/",
|
||||
Accept:
|
||||
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
|
||||
},
|
||||
};
|
||||
|
||||
try {
|
||||
const fetchResponse = await fetch(target, fetchOptions);
|
||||
clearTimeout(timeoutId);
|
||||
|
||||
if (!fetchResponse.ok) {
|
||||
throw new Error(`HTTP error! Status: ${fetchResponse.status}`);
|
||||
}
|
||||
|
||||
const html = await fetchResponse.text();
|
||||
const moviePattern =
|
||||
/<div class="item">[\s\S]*?<img[^>]+alt="([^"]+)"[^>]*src="([^"]+)"[\s\S]*?<span class="rating_num"[^>]*>([^<]+)<\/span>[\s\S]*?<\/div>/g;
|
||||
const movies: DoubanItem[] = [];
|
||||
let match;
|
||||
|
||||
while ((match = moviePattern.exec(html)) !== null) {
|
||||
const title = match[1];
|
||||
const cover = match[2];
|
||||
const rate = match[3] || "";
|
||||
const processedCover = cover.replace(/^http:/, "https:");
|
||||
movies.push({ title, poster: processedCover, rate });
|
||||
}
|
||||
|
||||
const apiResponse: DoubanResponse = {
|
||||
code: 200,
|
||||
message: "获取成功",
|
||||
list: movies,
|
||||
};
|
||||
const cacheTime = getCacheTime();
|
||||
res.setHeader("Cache-Control", `public, max-age=${cacheTime}`);
|
||||
res.json(apiResponse);
|
||||
} catch (error) {
|
||||
clearTimeout(timeoutId);
|
||||
res.status(500).json({
|
||||
error: "获取豆瓣 Top250 数据失败",
|
||||
details: (error as Error).message,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// --- Main Route Handler ---
|
||||
|
||||
router.get("/", async (req: Request, res: Response) => {
|
||||
const { type, tag } = req.query;
|
||||
const pageSize = parseInt((req.query.pageSize as string) || "16");
|
||||
const pageStart = parseInt((req.query.pageStart as string) || "0");
|
||||
|
||||
if (!type || !tag) {
|
||||
return res.status(400).json({ error: "缺少必要参数: type 或 tag" });
|
||||
}
|
||||
if (typeof type !== "string" || !["tv", "movie"].includes(type)) {
|
||||
return res.status(400).json({ error: "type 参数必须是 tv 或 movie" });
|
||||
}
|
||||
if (pageSize < 1 || pageSize > 100) {
|
||||
return res.status(400).json({ error: "pageSize 必须在 1-100 之间" });
|
||||
}
|
||||
if (pageStart < 0) {
|
||||
return res.status(400).json({ error: "pageStart 不能小于 0" });
|
||||
}
|
||||
|
||||
if (tag === "top250") {
|
||||
return handleTop250(pageStart, res);
|
||||
}
|
||||
|
||||
const target = `https://movie.douban.com/j/search_subjects?type=${type}&tag=${tag}&sort=recommend&page_limit=${pageSize}&page_start=${pageStart}`;
|
||||
|
||||
try {
|
||||
const doubanData = await fetchDoubanData(target);
|
||||
const list: DoubanItem[] = doubanData.subjects.map((item) => ({
|
||||
title: item.title,
|
||||
poster: item.cover,
|
||||
rate: item.rate,
|
||||
}));
|
||||
|
||||
const response: DoubanResponse = {
|
||||
code: 200,
|
||||
message: "获取成功",
|
||||
list: list,
|
||||
};
|
||||
const cacheTime = getCacheTime();
|
||||
res.setHeader("Cache-Control", `public, max-age=${cacheTime}`);
|
||||
res.json(response);
|
||||
} catch (error) {
|
||||
res.status(500).json({
|
||||
error: "获取豆瓣数据失败",
|
||||
details: (error as Error).message,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
export default router;
|
||||
43
backend/src/routes/image-proxy.ts
Normal file
43
backend/src/routes/image-proxy.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { Router, Request, Response } from "express";
|
||||
import { Readable } from "node:stream";
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.get("/", async (req: Request, res: Response) => {
|
||||
const imageUrl = req.query.url as string;
|
||||
|
||||
if (!imageUrl) {
|
||||
return res.status(400).send("Missing image URL");
|
||||
}
|
||||
|
||||
try {
|
||||
const imageResponse = await fetch(imageUrl, {
|
||||
headers: {
|
||||
Referer: "https://movie.douban.com/",
|
||||
"User-Agent":
|
||||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36",
|
||||
},
|
||||
});
|
||||
|
||||
if (!imageResponse.ok) {
|
||||
return res.status(imageResponse.status).send(imageResponse.statusText);
|
||||
}
|
||||
|
||||
const contentType = imageResponse.headers.get("content-type");
|
||||
if (contentType) {
|
||||
res.setHeader("Content-Type", contentType);
|
||||
}
|
||||
|
||||
if (imageResponse.body) {
|
||||
const nodeStream = Readable.fromWeb(imageResponse.body as any);
|
||||
nodeStream.pipe(res);
|
||||
} else {
|
||||
res.status(500).send("Image response has no body");
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Image proxy error:", error);
|
||||
res.status(500).send("Error fetching image");
|
||||
}
|
||||
});
|
||||
|
||||
export default router;
|
||||
18
backend/src/routes/index.ts
Normal file
18
backend/src/routes/index.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { Router } from "express";
|
||||
import searchRouter from "./search";
|
||||
import detailRouter from "./detail";
|
||||
import doubanRouter from "./douban";
|
||||
import loginRouter from "./login";
|
||||
import playRecordsRouter from "./playrecords";
|
||||
import imageProxyRouter from "./image-proxy";
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.use("/search", searchRouter);
|
||||
router.use("/detail", detailRouter);
|
||||
router.use("/douban", doubanRouter);
|
||||
router.use("/login", loginRouter);
|
||||
router.use("/playrecords", playRecordsRouter);
|
||||
router.use("/image-proxy", imageProxyRouter);
|
||||
|
||||
export default router;
|
||||
31
backend/src/routes/login.ts
Normal file
31
backend/src/routes/login.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { Router, Request, Response } from "express";
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.post("/", async (req: Request, res: Response) => {
|
||||
try {
|
||||
const masterPassword = process.env.PASSWORD;
|
||||
|
||||
// If no password is set in the environment, allow access.
|
||||
if (!masterPassword) {
|
||||
return res.json({ ok: true });
|
||||
}
|
||||
|
||||
const { password } = req.body;
|
||||
if (typeof password !== "string") {
|
||||
return res.status(400).json({ error: "密码不能为空" });
|
||||
}
|
||||
|
||||
const matched = password === masterPassword;
|
||||
|
||||
if (!matched) {
|
||||
return res.status(401).json({ ok: false, error: "密码错误" });
|
||||
}
|
||||
|
||||
return res.json({ ok: true });
|
||||
} catch (error) {
|
||||
return res.status(500).json({ error: "服务器错误" });
|
||||
}
|
||||
});
|
||||
|
||||
export default router;
|
||||
49
backend/src/routes/playrecords.ts
Normal file
49
backend/src/routes/playrecords.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import { Router, Request, Response } from "express";
|
||||
import { getAllPlayRecords, savePlayRecord } from "../services/db";
|
||||
import { PlayRecord } from "../types";
|
||||
|
||||
const router = Router();
|
||||
|
||||
// GET all play records
|
||||
router.get("/", async (_req: Request, res: Response) => {
|
||||
try {
|
||||
const records = await getAllPlayRecords();
|
||||
res.json(records);
|
||||
} catch (err) {
|
||||
console.error("获取播放记录失败", err);
|
||||
res.status(500).json({ error: "Internal Server Error" });
|
||||
}
|
||||
});
|
||||
|
||||
// POST a new play record
|
||||
router.post("/", async (req: Request, res: Response) => {
|
||||
try {
|
||||
const { key, record }: { key: string; record: PlayRecord } = req.body;
|
||||
|
||||
if (!key || !record) {
|
||||
return res.status(400).json({ error: "Missing key or record" });
|
||||
}
|
||||
|
||||
// Basic validation
|
||||
if (!record.title || !record.source_name || record.index < 0) {
|
||||
return res.status(400).json({ error: "Invalid record data" });
|
||||
}
|
||||
|
||||
const [source, id] = key.split("+");
|
||||
if (!source || !id) {
|
||||
return res.status(400).json({ error: "Invalid key format" });
|
||||
}
|
||||
|
||||
// The user_id will be stripped and re-added in the service to ensure it's always 0
|
||||
const recordToSave: Omit<PlayRecord, "user_id"> = record;
|
||||
|
||||
await savePlayRecord(source, id, recordToSave);
|
||||
|
||||
res.status(201).json({ success: true });
|
||||
} catch (err) {
|
||||
console.error("保存播放记录失败", err);
|
||||
res.status(500).json({ error: "Internal Server Error" });
|
||||
}
|
||||
});
|
||||
|
||||
export default router;
|
||||
222
backend/src/routes/search.ts
Normal file
222
backend/src/routes/search.ts
Normal file
@@ -0,0 +1,222 @@
|
||||
import { Router, Request, Response } from "express";
|
||||
import { API_CONFIG, ApiSite, getApiSites, getCacheTime } from "../config";
|
||||
import { cleanHtmlTags } from "../utils";
|
||||
|
||||
const router = Router();
|
||||
|
||||
// 根据环境变量决定最大搜索页数,默认 5
|
||||
const MAX_SEARCH_PAGES: number = Number(process.env.SEARCH_MAX_PAGE) || 5;
|
||||
|
||||
export interface SearchResult {
|
||||
id: string;
|
||||
title: string;
|
||||
poster: string;
|
||||
episodes: string[];
|
||||
source: string;
|
||||
source_name: string;
|
||||
class?: string;
|
||||
year: string;
|
||||
desc?: string;
|
||||
type_name?: string;
|
||||
}
|
||||
|
||||
interface ApiSearchItem {
|
||||
vod_id: string;
|
||||
vod_name: string;
|
||||
vod_pic: string;
|
||||
vod_remarks?: string;
|
||||
vod_play_url?: string;
|
||||
vod_class?: string;
|
||||
vod_year?: string;
|
||||
vod_content?: string;
|
||||
type_name?: string;
|
||||
}
|
||||
|
||||
async function searchFromApi(
|
||||
apiSite: ApiSite,
|
||||
query: string
|
||||
): Promise<SearchResult[]> {
|
||||
try {
|
||||
const apiBaseUrl = apiSite.api;
|
||||
const apiUrl =
|
||||
apiBaseUrl + API_CONFIG.search.path + encodeURIComponent(query);
|
||||
const apiName = apiSite.name;
|
||||
|
||||
// 添加超时处理
|
||||
const controller = new AbortController();
|
||||
const timeoutId = setTimeout(() => controller.abort(), 8000);
|
||||
|
||||
const response = await fetch(apiUrl, {
|
||||
headers: API_CONFIG.search.headers,
|
||||
signal: controller.signal,
|
||||
});
|
||||
|
||||
clearTimeout(timeoutId);
|
||||
|
||||
if (!response.ok) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (
|
||||
!data ||
|
||||
!data.list ||
|
||||
!Array.isArray(data.list) ||
|
||||
data.list.length === 0
|
||||
) {
|
||||
return [];
|
||||
}
|
||||
// 处理第一页结果
|
||||
const results = data.list.map((item: ApiSearchItem) => {
|
||||
let episodes: string[] = [];
|
||||
|
||||
// 使用正则表达式从 vod_play_url 提取 m3u8 链接
|
||||
if (item.vod_play_url) {
|
||||
const m3u8Regex = /\$(https?:\/\/[^"'\s]+?\.m3u8)/g;
|
||||
// 先用 $$$ 分割
|
||||
const vod_play_url_array = item.vod_play_url.split("$$$");
|
||||
// 对每个分片做匹配,取匹配到最多的作为结果
|
||||
vod_play_url_array.forEach((url: string) => {
|
||||
const matches = url.match(m3u8Regex) || [];
|
||||
if (matches.length > episodes.length) {
|
||||
episodes = matches;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
episodes = Array.from(new Set(episodes)).map((link: string) => {
|
||||
link = link.substring(1); // 去掉开头的 $
|
||||
const parenIndex = link.indexOf("(");
|
||||
return parenIndex > 0 ? link.substring(0, parenIndex) : link;
|
||||
});
|
||||
|
||||
return {
|
||||
id: item.vod_id,
|
||||
title: item.vod_name,
|
||||
poster: item.vod_pic,
|
||||
episodes,
|
||||
source: apiSite.key,
|
||||
source_name: apiName,
|
||||
class: item.vod_class,
|
||||
year: item.vod_year ? item.vod_year.match(/\d{4}/)?.[0] || "" : "",
|
||||
desc: cleanHtmlTags(item.vod_content || ""),
|
||||
type_name: item.type_name,
|
||||
};
|
||||
});
|
||||
|
||||
// 获取总页数
|
||||
const pageCount = data.pagecount || 1;
|
||||
// 确定需要获取的额外页数
|
||||
const pagesToFetch = Math.min(pageCount - 1, MAX_SEARCH_PAGES - 1);
|
||||
|
||||
// 如果有额外页数,获取更多页的结果
|
||||
if (pagesToFetch > 0) {
|
||||
const additionalPagePromises = [];
|
||||
|
||||
for (let page = 2; page <= pagesToFetch + 1; page++) {
|
||||
const pageUrl =
|
||||
apiBaseUrl +
|
||||
API_CONFIG.search.pagePath
|
||||
.replace("{query}", encodeURIComponent(query))
|
||||
.replace("{page}", page.toString());
|
||||
|
||||
const pagePromise = (async () => {
|
||||
try {
|
||||
const pageController = new AbortController();
|
||||
const pageTimeoutId = setTimeout(
|
||||
() => pageController.abort(),
|
||||
8000
|
||||
);
|
||||
|
||||
const pageResponse = await fetch(pageUrl, {
|
||||
headers: API_CONFIG.search.headers,
|
||||
signal: pageController.signal,
|
||||
});
|
||||
|
||||
clearTimeout(pageTimeoutId);
|
||||
|
||||
if (!pageResponse.ok) return [];
|
||||
|
||||
const pageData = await pageResponse.json();
|
||||
|
||||
if (!pageData || !pageData.list || !Array.isArray(pageData.list))
|
||||
return [];
|
||||
|
||||
return pageData.list.map((item: ApiSearchItem) => {
|
||||
let episodes: string[] = [];
|
||||
|
||||
if (item.vod_play_url) {
|
||||
const m3u8Regex = /\$(https?:\/\/[^"'\s]+?\.m3u8)/g;
|
||||
episodes = item.vod_play_url.match(m3u8Regex) || [];
|
||||
}
|
||||
|
||||
episodes = Array.from(new Set(episodes)).map((link: string) => {
|
||||
link = link.substring(1); // 去掉开头的 $
|
||||
const parenIndex = link.indexOf("(");
|
||||
return parenIndex > 0 ? link.substring(0, parenIndex) : link;
|
||||
});
|
||||
|
||||
return {
|
||||
id: item.vod_id,
|
||||
title: item.vod_name,
|
||||
poster: item.vod_pic,
|
||||
episodes,
|
||||
source: apiSite.key,
|
||||
source_name: apiName,
|
||||
class: item.vod_class,
|
||||
year: item.vod_year
|
||||
? item.vod_year.match(/\d{4}/)?.[0] || ""
|
||||
: "",
|
||||
desc: cleanHtmlTags(item.vod_content || ""),
|
||||
type_name: item.type_name,
|
||||
};
|
||||
});
|
||||
} catch (error) {
|
||||
return [];
|
||||
}
|
||||
})();
|
||||
|
||||
additionalPagePromises.push(pagePromise);
|
||||
}
|
||||
|
||||
const additionalResults = await Promise.all(additionalPagePromises);
|
||||
|
||||
additionalResults.forEach((pageResults) => {
|
||||
if (pageResults.length > 0) {
|
||||
results.push(...pageResults);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return results;
|
||||
} catch (error) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
router.get("/", async (req: Request, res: Response) => {
|
||||
const query = req.query.q as string;
|
||||
|
||||
if (!query) {
|
||||
const cacheTime = getCacheTime();
|
||||
res.setHeader("Cache-Control", `public, max-age=${cacheTime}`);
|
||||
return res.json({ results: [] });
|
||||
}
|
||||
|
||||
const apiSites = getApiSites();
|
||||
const searchPromises = apiSites.map((site) => searchFromApi(site, query));
|
||||
|
||||
try {
|
||||
const results = await Promise.all(searchPromises);
|
||||
const flattenedResults = results.flat();
|
||||
const cacheTime = getCacheTime();
|
||||
|
||||
res.setHeader("Cache-Control", `public, max-age=${cacheTime}`);
|
||||
res.json({ results: flattenedResults });
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: "搜索失败" });
|
||||
}
|
||||
});
|
||||
|
||||
export default router;
|
||||
73
backend/src/services/db.ts
Normal file
73
backend/src/services/db.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
import fs from "fs/promises";
|
||||
import path from "path";
|
||||
import { PlayRecord } from "../types";
|
||||
|
||||
const DATA_DIR = path.join(process.cwd(), "data");
|
||||
const PLAY_RECORDS_FILE = path.join(DATA_DIR, "playrecords.json");
|
||||
|
||||
type DbData = {
|
||||
[key: string]: PlayRecord;
|
||||
};
|
||||
|
||||
// Ensure data directory exists
|
||||
async function ensureDataDir(): Promise<void> {
|
||||
try {
|
||||
await fs.mkdir(DATA_DIR, { recursive: true });
|
||||
} catch (error) {
|
||||
console.error("Error creating data directory:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// Read the entire DB file
|
||||
async function readDb(): Promise<DbData> {
|
||||
await ensureDataDir();
|
||||
try {
|
||||
const data = await fs.readFile(PLAY_RECORDS_FILE, "utf-8");
|
||||
return JSON.parse(data) as DbData;
|
||||
} catch (error: any) {
|
||||
// If file does not exist, return empty object
|
||||
if (error.code === "ENOENT") {
|
||||
return {};
|
||||
}
|
||||
console.error("Error reading database file:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// Write the entire DB file
|
||||
async function writeDb(data: DbData): Promise<void> {
|
||||
await ensureDataDir();
|
||||
try {
|
||||
await fs.writeFile(
|
||||
PLAY_RECORDS_FILE,
|
||||
JSON.stringify(data, null, 2),
|
||||
"utf-8"
|
||||
);
|
||||
} catch (error) {
|
||||
console.error("Error writing to database file:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// --- Public DB Methods ---
|
||||
|
||||
export function generateStorageKey(source: string, id: string): string {
|
||||
return `${source}+${id}`;
|
||||
}
|
||||
|
||||
export async function getAllPlayRecords(): Promise<DbData> {
|
||||
return readDb();
|
||||
}
|
||||
|
||||
export async function savePlayRecord(
|
||||
source: string,
|
||||
id: string,
|
||||
record: Omit<PlayRecord, "user_id">
|
||||
): Promise<void> {
|
||||
const db = await readDb();
|
||||
const key = generateStorageKey(source, id);
|
||||
const fullRecord: PlayRecord = { ...record, user_id: 0 }; // user_id is always 0 for now
|
||||
db[key] = fullRecord;
|
||||
await writeDb(db);
|
||||
}
|
||||
33
backend/src/types/index.ts
Normal file
33
backend/src/types/index.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
// Data structure for play records
|
||||
export interface PlayRecord {
|
||||
title: string;
|
||||
source_name: string;
|
||||
cover: string;
|
||||
index: number; // Episode number
|
||||
total_episodes: number; // Total number of episodes
|
||||
play_time: number; // Play progress in seconds
|
||||
total_time: number; // Total duration in seconds
|
||||
save_time: number; // Timestamp of when the record was saved
|
||||
user_id: number; // User ID, always 0 in this version
|
||||
}
|
||||
|
||||
// You can add other shared types here
|
||||
export interface VideoDetail {
|
||||
code: number;
|
||||
episodes: string[];
|
||||
detailUrl: string;
|
||||
videoInfo: {
|
||||
title: string;
|
||||
cover: string;
|
||||
desc: string;
|
||||
source_name: string;
|
||||
source: string;
|
||||
id: string;
|
||||
type?: string;
|
||||
year?: string;
|
||||
area?: string;
|
||||
director?: string;
|
||||
actor?: string;
|
||||
remarks?: string;
|
||||
};
|
||||
}
|
||||
10
backend/src/utils/index.ts
Normal file
10
backend/src/utils/index.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
export function cleanHtmlTags(text: string): string {
|
||||
if (!text) return "";
|
||||
return text
|
||||
.replace(/<[^>]+>/g, "\n") // 将 HTML 标签替换为换行
|
||||
.replace(/\n+/g, "\n") // 将多个连续换行合并为一个
|
||||
.replace(/[ \t]+/g, " ") // 将多个连续空格和制表符合并为一个空格,但保留换行符
|
||||
.replace(/^\n+|\n+$/g, "") // 去掉首尾换行
|
||||
.replace(/ /g, " ") // 将 替换为空格
|
||||
.trim(); // 去掉首尾空格
|
||||
}
|
||||
Reference in New Issue
Block a user