fix(scrape): 平铺目录只刮本作品视频、图片按视频名命名;图片扩展名按实际类型;取消时记录不停在进行中
- 作品重刮对电影传入该作品已入库的视频,不给同目录别的电影写本作品 NFO; - 电影目录里有别的作品的视频时海报 / 背景图写 <视频名>-poster / -fanart,不写目录共用的 poster.jpg; - 图片扩展名按下载到的 MIME,URL 认不出或与内容不符时以内容为准; - 任务被取消时记录收尾为失败 scrape_canceled,保留已写文件结果。 Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Cxwgnn2pbzQ7rXv3rq7fei
This commit is contained in:
1 parent
cdf62dbf02
commit
89aa70bb9c
4 files changed
+220
-18
No files matched your search
@@ -81,6 +81,8 @@ export interface ScrapeLocation {
|
||||
rootPath: string;
|
||||
libraryRootId: string | null;
|
||||
path: string;
|
||||
/** 只刮这些视频(作品重刮:电影目录里属于本作品的视频);缺省 = 目录下全部 */
|
||||
videos?: readonly string[] | undefined;
|
||||
}
|
||||
|
||||
export interface QueueScrapeInput extends ScrapeLocation {
|
||||
@@ -341,6 +343,8 @@ export async function entityScrapeLocations(
|
||||
.where("status", "=", "active")
|
||||
.execute();
|
||||
const out = new Map<string, ScrapeLocation>();
|
||||
/** 电影:每个目录里属于本作品的视频(重刮只刮这些,同目录别的电影不写本作品的 NFO);有原盘目录的不限定 */
|
||||
const own = new Map<string, string[] | null>();
|
||||
for (const l of locs) {
|
||||
const bound = bindings.find(
|
||||
(b) =>
|
||||
@@ -358,9 +362,26 @@ export async function entityScrapeLocations(
|
||||
}
|
||||
// 父目录已在列表里的子路径去掉,避免同一处刮两次
|
||||
const all = [...out.values()];
|
||||
return all.filter(
|
||||
const kept = all.filter(
|
||||
(x) => !all.some((y) => y !== x && y.rootId === x.rootId && !!y.path && x.path.startsWith(`${y.path}/`)),
|
||||
);
|
||||
if (kind !== "movie") return kept;
|
||||
for (const l of locs) {
|
||||
const loc = kept.find(
|
||||
(x) =>
|
||||
x.libraryRootId === l.root_id &&
|
||||
(!x.path || l.relative_path === x.path || l.relative_path.startsWith(`${x.path}/`)),
|
||||
);
|
||||
if (!loc) continue;
|
||||
const key = `${loc.rootId}\u0000${loc.path}`;
|
||||
if (l.kind !== "video") own.set(key, null);
|
||||
else if (own.get(key) !== null) own.set(key, [...(own.get(key) ?? []), l.relative_path]);
|
||||
}
|
||||
return kept.map((x) => {
|
||||
const videos = own.get(`${x.rootId}\u0000${x.path}`);
|
||||
// 选中的就是视频文件本身时不用限定
|
||||
return videos?.length && !(videos.length === 1 && videos[0] === x.path) ? { ...x, videos } : x;
|
||||
});
|
||||
}
|
||||
|
||||
/** 对作品的目录刮削(每个目录一条记录);没有已入库文件时返回空 */
|
||||
@@ -611,6 +632,8 @@ async function listDir(rootReal: string, rel: string) {
|
||||
|
||||
interface Layout {
|
||||
videos: string[];
|
||||
/** 选中目录里的全部视频(不进子目录);判断电影目录里有没有别的作品 */
|
||||
dirVideos: string[];
|
||||
workDir: string | null;
|
||||
parts: { work: boolean; season: boolean };
|
||||
/** 季目录刮削时目录名给出的季号 */
|
||||
@@ -623,18 +646,31 @@ async function layoutOf(rootReal: string, rel: string, isDir: boolean, kind: "mo
|
||||
const all = { work: true, season: true };
|
||||
if (!isDir)
|
||||
return kind === "movie"
|
||||
? { videos: [rel], workDir: parent, parts: all, dirSeason: null }
|
||||
: { videos: [rel], workDir: null, parts: { work: false, season: false }, dirSeason: null };
|
||||
? {
|
||||
videos: [rel],
|
||||
dirVideos: (await listDir(rootReal, parent ?? "")).videos,
|
||||
workDir: parent,
|
||||
parts: all,
|
||||
dirSeason: null,
|
||||
}
|
||||
: { videos: [rel], dirVideos: [], workDir: null, parts: { work: false, season: false }, dirSeason: null };
|
||||
const here = await listDir(rootReal, rel);
|
||||
if (kind === "movie") {
|
||||
// 蓝光原盘目录:写 <目录名>.nfo(借一个同名的虚拟视频路径让布局按目录名命名)+ 目录海报
|
||||
if (rel && here.dirs.some((n) => /^(BDMV|VIDEO_TS)$/i.test(n)))
|
||||
return { videos: [joinRel(rel, `${baseOf(rel)}.bluray`)], workDir: rel, parts: all, dirSeason: null };
|
||||
return { videos: here.videos, workDir: rel || null, parts: all, dirSeason: null };
|
||||
return {
|
||||
videos: [joinRel(rel, `${baseOf(rel)}.bluray`)],
|
||||
dirVideos: [],
|
||||
workDir: rel,
|
||||
parts: all,
|
||||
dirSeason: null,
|
||||
};
|
||||
return { videos: here.videos, dirVideos: here.videos, workDir: rel || null, parts: all, dirSeason: null };
|
||||
}
|
||||
if (rel && isSeasonDirName(baseOf(rel)))
|
||||
return {
|
||||
videos: here.videos,
|
||||
dirVideos: here.videos,
|
||||
workDir: parent,
|
||||
parts: { work: false, season: true },
|
||||
dirSeason: seasonOfDirName(baseOf(rel)),
|
||||
@@ -642,7 +678,7 @@ async function layoutOf(rootReal: string, rel: string, isDir: boolean, kind: "mo
|
||||
const videos = [...here.videos];
|
||||
for (const sub of here.dirs)
|
||||
if (isSeasonDirName(sub)) videos.push(...(await listDir(rootReal, joinRel(rel, sub))).videos);
|
||||
return { videos, workDir: rel || null, parts: all, dirSeason: null };
|
||||
return { videos, dirVideos: here.videos, workDir: rel || null, parts: all, dirSeason: null };
|
||||
}
|
||||
|
||||
/** 已登记视频的季集:覆盖单元里最小的一集为首集,同一季覆盖的其余集一并列出(多集文件每集一个 NFO 块) */
|
||||
@@ -724,8 +760,53 @@ function episodeByAirDate(
|
||||
return hits.length === 1 ? hits[0]! : null;
|
||||
}
|
||||
|
||||
/** 图片 MIME → 扩展名;认不出返回 null(沿用按 URL 定的扩展名) */
|
||||
function imageExtOf(mime: string | null | undefined): string | null {
|
||||
const m = (mime ?? "").split(";")[0]!.trim().toLowerCase();
|
||||
return m === "image/jpeg" || m === "image/jpg"
|
||||
? ".jpg"
|
||||
: m === "image/png"
|
||||
? ".png"
|
||||
: m === "image/webp"
|
||||
? ".webp"
|
||||
: null;
|
||||
}
|
||||
|
||||
/** 预期内的跳过(不影响记录状态):已存在、内容没变、被人改过 */
|
||||
const EXPECTED_SKIPS = new Set(["scrape_exists", "scrape_unchanged", "scrape_user_modified"]);
|
||||
/**
|
||||
* 电影目录里有没有别的作品:目录里的视频登记在别的作品下,或(只刮部分视频时)有没登记到本作品、也不在本次范围里的视频。
|
||||
* 同一作品的多个版本不算。
|
||||
*/
|
||||
async function movieDirShared(
|
||||
d: AoDeps,
|
||||
rec: { library_root_id: string | null; videos: string[] | null },
|
||||
lay: Layout,
|
||||
entityId: string | null,
|
||||
): Promise<boolean> {
|
||||
const others = lay.dirVideos.filter((v) => !lay.videos.includes(v));
|
||||
if (!lay.dirVideos.length) return false;
|
||||
const owners = new Map<string, Set<string>>();
|
||||
if (rec.library_root_id) {
|
||||
const rows = await d.ctx.db
|
||||
.selectFrom("asset_location as l")
|
||||
.innerJoin("media_asset as a", "a.id", "l.media_asset_id")
|
||||
.select(["l.relative_path", "a.media_entity_id"])
|
||||
.where("l.library_root_id", "=", rec.library_root_id)
|
||||
.where("l.relative_path", "in", lay.dirVideos)
|
||||
.where("l.status", "<>", "removed")
|
||||
.where("a.status", "=", "active")
|
||||
.execute();
|
||||
for (const r of rows)
|
||||
owners.set(r.relative_path, (owners.get(r.relative_path) ?? new Set()).add(r.media_entity_id));
|
||||
}
|
||||
return lay.dirVideos.some((v) => {
|
||||
const own = owners.get(v);
|
||||
if (own && [...own].some((e) => e !== entityId)) return true;
|
||||
return !own?.size && others.includes(v);
|
||||
});
|
||||
}
|
||||
|
||||
const sha256 = (c: Uint8Array | string) => createHash("sha256").update(c).digest("hex");
|
||||
/** 旧版本写的文件没有 hash:修改时间不晚于那次刮削结束(加这点余量)就当没被改过 */
|
||||
const LEGACY_MTIME_SLACK_MS = 2_000;
|
||||
@@ -937,6 +1018,9 @@ export async function runScrapeRecord(
|
||||
}
|
||||
}
|
||||
|
||||
// 电影目录里还有别的作品(自定义平铺模板):图片按视频名命名,不写目录共用的 poster / fanart
|
||||
const sharedDir = kind === "movie" && (await movieDirShared(d, rec, lay, match.mediaEntityId));
|
||||
|
||||
// 季 / 集号:已登记的按覆盖单元;其余按路径解析(季目录名兜底,同 MP 对每集重新 MetaInfoPath)
|
||||
const videos: ScrapeVideo[] = [];
|
||||
/** 集数待确认(不写分集 NFO,原因 episode_unresolved) */
|
||||
@@ -1063,14 +1147,15 @@ export async function runScrapeRecord(
|
||||
images: { ...imagePathsOf(fields.entity.metadata), seasonPosters, stills },
|
||||
options: { images: settings.scrapeImages, episodeThumbs: settings.scrapeEpisodeThumbs },
|
||||
parts: lay.parts,
|
||||
sharedDir,
|
||||
});
|
||||
// 「更新」模式:本工具以前写过的文件(路径 → 写入时的 hash / 时间)
|
||||
const ours = mode === "update" ? await previousWrites(d, rec.root_id, recordId) : new Map<string, PrevWrite>();
|
||||
const imageCache = new Map<string, Promise<Uint8Array>>();
|
||||
const imageCache = new Map<string, Promise<{ content: Uint8Array; mime: string }>>();
|
||||
const fetchImage = (p: string) => {
|
||||
let hit = imageCache.get(p);
|
||||
if (!hit) {
|
||||
hit = d.ports.scrapeImage(p).then((r) => r.content);
|
||||
hit = d.ports.scrapeImage(p);
|
||||
imageCache.set(p, hit);
|
||||
}
|
||||
return hit;
|
||||
@@ -1147,18 +1232,27 @@ export async function runScrapeRecord(
|
||||
// 图片:限并发,失败不影响 NFO
|
||||
const imgs = targets.filter((x) => x.kind === "image" && x.imagePath);
|
||||
files.push(
|
||||
...(await pool(imgs, IMAGE_CONCURRENCY, async (t): Promise<ScrapeFileResult> => {
|
||||
...(await pool(imgs, IMAGE_CONCURRENCY, async (planned0): Promise<ScrapeFileResult> => {
|
||||
if (signal?.aborted) throw new Error("aborted");
|
||||
const dec = await decide(t);
|
||||
let t = planned0;
|
||||
let dec = await decide(t);
|
||||
if (!("abs" in dec)) return dec;
|
||||
let bytes: Uint8Array;
|
||||
let got: { content: Uint8Array; mime: string };
|
||||
try {
|
||||
bytes = await fetchImage(t.imagePath!);
|
||||
got = await fetchImage(t.imagePath!);
|
||||
} catch (err) {
|
||||
d.ctx.logger.warn({ recordId, image: t.imagePath, err: { message: errMessage(err) } }, "刮削图片下载失败");
|
||||
return { path: t.path, kind: t.kind, role: t.role, action: "failed", reason: "scrape_image_failed" };
|
||||
}
|
||||
return write(t, dec, bytes);
|
||||
// 扩展名按实际类型(URL 认不出或与内容不符时,例如 Local 封面链接是 webp、内容是 jpeg)
|
||||
const ext = imageExtOf(got.mime);
|
||||
const planned = /\.(jpe?g|png|webp)$/i.exec(t.path)?.[0]?.toLowerCase();
|
||||
if (ext && planned && ext !== planned && !(ext === ".jpg" && planned === ".jpeg")) {
|
||||
t = { ...t, path: t.path.slice(0, -planned.length) + ext };
|
||||
dec = await decide(t);
|
||||
if (!("abs" in dec)) return dec;
|
||||
}
|
||||
return write(t, dec, got.content);
|
||||
})),
|
||||
);
|
||||
|
||||
@@ -1185,7 +1279,11 @@ export async function runScrapeRecord(
|
||||
files,
|
||||
});
|
||||
} catch (err) {
|
||||
if (signal?.aborted) throw err;
|
||||
if (signal?.aborted) {
|
||||
// 任务被取消(引擎不会再跑这一步):记录收尾为失败,已写的文件结果照留,不停在进行中
|
||||
await finish({ status: "failed", reason: "scrape_canceled", files }).catch(() => {});
|
||||
throw err;
|
||||
}
|
||||
d.ctx.logger.warn({ recordId, err: { message: errMessage(err) } }, "刮削失败");
|
||||
// 不是写文件的错(如查库失败):记内部错误,已写的文件结果照留
|
||||
const anyWritten = files.some((f) => f.action === "written");
|
||||
|
||||
Reference in new issue
Block a user