Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

feat(scan): 兼容主流媒体服务器的附属媒体命名

longmeidao6985035a76f3
diff --git a/docs/REUSE.md b/docs/REUSE.md
index f94fbe0a5..b38be83d0 100644
--- a/docs/REUSE.md
+++ b/docs/REUSE.md
@@ -898,9 +898,13 @@ Python 源文件纳入 `src/peach/library_sharing.py` 与 `src/peach/library_pro
普通 Toast 与扫描任务卡共用 Agent Progress 外框,16px 半径由通知栈内的既有 token 解析,细边框与接触阴影分别复用 `border-button-default`、`elevation-xs`,不改变内容排版。浏览器回归核对桌面、手机和明暗主题下的外框一致性,并以同一任务、同一内容回放外框基线生成对比图。
-## 本地预告片识别
+## 本地附属媒体识别
-本地预告片识别复用 `catalog_rules.is_local_trailer` 与现有 `scan.is_sidecar`,行为约定来自 [Kodi 本地预告片](https://kodi.wiki/view/Trailers)与 [Jellyfin 附属文件命名](https://jellyfin.org/docs/general/server/media/movies/#file-suffix)(2026-10-23 核对)。仅参照公开命名规则,不复制上游源码或引入媒体服务器依赖。`trailer` 裸名及 `-trailer`、`.trailer`、`_trailer`、空格加 `trailer` 的末尾标记不区分大小写,扩展名沿用现有视频集合。扫描、推送发现、首次预检、后台探测、资料采集和馆藏查询使用同一判据;已登记记录只在作品列表、版本组与库存计数中排除,保留媒体和个人记录,不按时长猜测。隔离回归复现正片与预告片共存的目录,并验证短正片、普通片名和账本只读性。Python 3.14 标准库 `re`(PSF 许可)已随运行时提供,无新增依赖与安装体积;不采用额外视频解析库,因为本项只需读取文件名。取证范围见[本地预告片证据](reference-snapshots/local-trailer-evidence.md)。
+附属媒体识别复用 `catalog_rules.local_extra_type`、`is_local_extra` 与现有 `scan.is_sidecar`,兼容 Plex、Kodi、Emby、Jellyfin 的显式文件标记和专用目录。规则来源、完整标记及边界见[附属媒体命名证据](reference-snapshots/local-trailer-evidence.md)。
+
+分类模型和末尾数字处理参照 Jellyfin 的 `Emby.Naming`,固定提交 [`c7ce6a1b56ee7dcc1896250df0f2f9deaa828a81`](https://github.com/jellyfin/jellyfin/tree/c7ce6a1b56ee7dcc1896250df0f2f9deaa828a81/Emby.Naming),项目为持续维护的媒体服务器;该模块声明 GPL-3.0-only,目标框架为 .NET 10,并依赖 `MediaBrowser.Common` 和 `MediaBrowser.Model`。已通过 opensrc 阅读规则与解析实现,并将三个缓存文件与固定提交的上游原始文件逐字节核对。Peach 参照规则事实与解析边界,用 Python 3.14 标准库 `pathlib`、字串操作及 `functools` 实现自身策略,不复制上游源文件或引入 .NET 服务、运行时和媒体服务器依赖;没有新增 Python 依赖及安装体积。
+
+同一分类器用于扫描、推送发现、首次预检、后台探测、资料采集、馆藏列表、版本组、实体数量与库存统计。专用目录支持嵌套,按最长已声明媒体库根截断路径;媒体库根本身即使叫 `Shorts` 或 `Extras`,其中的普通影片也不按附属目录处理。明确文件名标记照常生效;不根据时长、分辨率或片名中间的单词推测。旧记录在普通馆藏与后台工作量中排除,单项详情、媒体、评分和播放历史保留;不增加迁移或清理真实 ledger。临时目录和账本回归覆盖四家命名、编号、嵌套目录、库根保护、跨平台路径和只读性。
## 媒体库与根目录身份
diff --git a/docs/reference-snapshots/local-trailer-evidence.md b/docs/reference-snapshots/local-trailer-evidence.md
index b29ccdbcf..653e32221 100644
--- a/docs/reference-snapshots/local-trailer-evidence.md
+++ b/docs/reference-snapshots/local-trailer-evidence.md
@@ -1,14 +1,24 @@
-# 本地预告片命名证据
+# 本地附属媒体命名证据
核对日期:2026-10-23。本文件是人工取证笔记,记录网页命名事实与 Peach 的采用范围,不是上游原文,因此不登记进 `docs/reference-sources.json`。
## 公开约定
-- **实测**:[Kodi 官方 Trailers 页面](https://kodi.wiki/view/Trailers)的 Local 一节要求本地预告片与正片同目录,文件主名追加 `-trailer`。页面提供的固定历史版本抓取返回 `Cache miss`,本次证据使用官方当前页面,不声称取得固定版本原文。
-- **实测**:[Jellyfin 官方 Movies 页面](https://jellyfin.org/docs/general/server/media/movies/#file-name)列出裸名 `trailer`;[File Suffix 一节](https://jellyfin.org/docs/general/server/media/movies/#file-suffix)列出连字符、点、下划线和空格分隔的末尾预告片标记。网页内容按 CC-BY-ND-4.0 发布;本实现只参照文件命名事实,不复制或改写上游文档作为代码。
+- **已核对**:[Plex 电影附属文件](https://support.plex.tv/articles/local-files-for-trailers-and-extras/)规定同目录末尾类型标记及 `Trailers`、`Interviews` 等专用目录;[剧集附属文件](https://support.plex.tv/articles/local-files-for-tv-show-trailers-and-extras/)还列出带数字编号的末尾标记。电影页面直接抓取返回 403,搜索服务取得官方页面索引正文;不声称完成该页面的浏览器访问。
+- **已核对**:[Kodi 本地预告片](https://kodi.wiki/view/Trailers)使用与正片同名的 `-trailer`;[Video Extras](https://kodi.wiki/view/Video_extras)和 [Extras 插件](https://kodi.wiki/view/Add-on:Extras)使用影片目录下的 `Extras`,允许嵌套子目录。Kodi 的扫描配置会影响其实际展示;本次只兼容命名语义。
+- **已核对**:[Emby 预告片](https://emby.media/support/articles/Trailers.html)规定 `-trailer` 和 `trailers/`;[电影命名](https://emby.media/support/articles/Movie-Naming.html#movie-extras)列出 `extras`、`specials`、`shorts` 等附属目录。Emby 文档不支持花絮嵌套,Peach 的嵌套支持来自 Kodi 的约定。
+- **已核对**:[Jellyfin 附属媒体](https://jellyfin.org/docs/general/server/media/movies/#extras)列出裸名、文件名后缀、专用目录和主题媒体;网页以 CC-BY-ND-4.0 发布,仅参照命名事实,不复制网页正文。固定提交 [`c7ce6a1b56ee7dcc1896250df0f2f9deaa828a81`](https://github.com/jellyfin/jellyfin/tree/c7ce6a1b56ee7dcc1896250df0f2f9deaa828a81/Emby.Naming)的 `Common/NamingOptions.cs`、`Video/ExtraRuleResolver.cs` 与项目清单已通过 opensrc 阅读,并与该提交的原始文件逐字节核对;解析器去掉末尾 ASCII 数字,再匹配裸名或后缀,并排除媒体库根本身。
## Peach 的采用范围
-Peach 复用标准库正则表达式独立实现文件名判据,扩展名沿用现有扫描器。它排除预告片的独立作品卡片,不新增预告片播放入口,不扩展到 `sample`、花絮或 `trailers/` 子目录约定。命名明确时不依赖正片先到达,也不读取视频内容;普通短正片和未匹配末尾标记的片名照常登记。
+| 形式 | 支持范围 |
+| --- | --- |
+| 视频裸名 | `trailer`、`sample`,包括 `trailer2.mp4`、`sample1.mkv` |
+| 预告片和样片后缀 | `trailer`、`sample` 前使用连字符、点、下划线或空格;支持 `- trailer`、`- sample` 及末尾数字 |
+| 其他视频后缀 | `-behindthescenes`、`-deleted`、`-deletedscene`、`-featurette`、`-interview`、`-scene`、`-clip`、`-short`、`-extra`、`-other`,支持末尾数字 |
+| 视频专用目录 | `trailers`、`behind the scenes`、`deleted scenes`、`interviews`、`scenes`、`samples`、`sample`、`shorts`、`featurettes`、`clips`、`extras`、`extra`、`other`、`specials`、`backdrops` |
+| 主题媒体 | 音频裸名 `theme` 及末尾数字;`theme-music/` 中的音频;`backdrops/` 中的视频 |
-已登记的预告片保留原始行、评分与播放记录,查询时排除;不增加迁移,不清理真实 ledger。临时文件与临时账本用例覆盖截图中的目录、推送发现、首次预检、后台探测、资料采集、列表、搜索、补全、版本组及库存计数。未部署 Kodi 或 Jellyfin,二者完整服务的端到端兼容性未验证。
+匹配不区分大小写,媒体扩展名沿用 Peach 的视频与音频集合。目录规则可以作用于嵌套内容,但只看最长声明媒体库根以下的路径。库根本身名为 `Extras` 或 `Shorts` 时不触发目录规则;明确裸名或文件后缀仍触发。普通短片、`Trailer Park.mp4`、`Interview.mp4` 和片名中间的关键词不参与猜测。命名明确时不依赖正片先到达,不读取视频内容。Plex 对 ISO、宽泛关键词和文件大小的扫描排除属于其扫描器策略,Peach 不据此屏蔽自身支持的媒体格式或普通片名。
+
+附属媒体不作为独立正片卡片或后台处理工作量;已登记行、媒体、评分和播放记录保留,可继续通过单项详情访问。不新增花絮播放界面、预告片下载或社区刮削插件,不增加迁移或清理真实 ledger。临时目录与账本回归覆盖扫描、推送发现、预检、探测、采集、查询、版本组、实体数量和库存计数。未部署四家服务器,其完整服务、客户端及插件的端到端兼容性未验证。
diff --git a/src/peach/api.py b/src/peach/api.py
index 34ea69807..c7441be1d 100644
--- a/src/peach/api.py
+++ b/src/peach/api.py
@@ -39,7 +39,7 @@ from . import (__version__, browser_transport, feeds, tunnel, web_contract,
web_feeds, web_follow, web_settings)
from . import routes_api, routes_auth, routes_configuration, routes_jav_browser, routes_media, routes_pages, routes_subtitles
from .buildinfo import frozen_build
-from .config import LOCATION_ROOT_DECLARATIONS, PeachSettings
+from .config import LOCATION_ROOT_DECLARATIONS, MEDIA_ROOT_DECLARATIONS, PeachSettings
from .ffmpeg import FFmpegResolver
from .follow_scheduler import FEED_JOB_ID, FollowScheduleConfig, FollowUpdateScheduler
from .follow_covers import FollowCoverService
@@ -245,7 +245,7 @@ def create_app(
settings = settings or PeachSettings()
if tunnel_manager is None:
tunnel_manager = tunnel.TunnelManager(settings.tunnel_state_root, settings.tunnel_log_root)
- database = LedgerDatabase(settings.db_path)
+ database = LedgerDatabase(settings.db_path, media_roots=MEDIA_ROOT_DECLARATIONS)
contract = web_contract.WebContract(
settings.db_path, settings.snapshot_root,
candidate_root=settings.candidate_root,
diff --git a/src/peach/catalog_rules.py b/src/peach/catalog_rules.py
index 252d42c84..bef0be2a0 100644
--- a/src/peach/catalog_rules.py
+++ b/src/peach/catalog_rules.py
@@ -12,6 +12,7 @@ from __future__ import annotations
import re
import unicodedata
from collections.abc import Iterable
+from functools import lru_cache
from pathlib import PureWindowsPath
@@ -19,15 +20,57 @@ VIDEO_EXTENSIONS = frozenset({
'.mp4', '.m4v', '.mkv', '.avi', '.wmv', '.mov', '.ts', '.flv', '.rmvb', '.mpg',
'.m2ts', '.strm', '.iso',
})
-_LOCAL_TRAILER = re.compile(
- r'(?:^|[-._ ])trailer(?:' + '|'.join(re.escape(ext) for ext in sorted(VIDEO_EXTENSIONS)) + r')\Z',
- re.IGNORECASE,
-)
+AUDIO_EXTENSIONS = frozenset({'.mp3', '.flac', '.wav', '.m4a', '.ogg', '.opus'})
+_EXTRA_SUFFIXES = {
+ '-behindthescenes': 'behindthescenes', '-deleted': 'deleted', '-deletedscene': 'deleted',
+ '-featurette': 'featurette', '-interview': 'interview', '-scene': 'scene',
+ '-clip': 'clip', '-short': 'short', '-extra': 'other', '-other': 'other',
+ **{separator + kind: kind for kind in ('trailer', 'sample') for separator in ('-', '.', '_', ' ')},
+}
+_EXTRA_DIRECTORIES = {
+ 'trailers': 'trailer', 'behind the scenes': 'behindthescenes', 'deleted scenes': 'deleted',
+ 'interviews': 'interview', 'scenes': 'scene', 'samples': 'sample', 'sample': 'sample',
+ 'shorts': 'short', 'featurettes': 'featurette', 'clips': 'clip',
+ 'extras': 'other', 'extra': 'other', 'other': 'other', 'specials': 'other',
+ 'backdrops': 'themevideo', 'theme-music': 'themeaudio',
+}
-def is_local_trailer(name: str | None) -> bool:
- """Kodi/Jellyfin 的裸名或末尾 trailer 标记;时长不参与判定。"""
- return bool(_LOCAL_TRAILER.search(name or ''))
+@lru_cache(maxsize=8192)
+def _extra_directory(directory: str, library_roots: tuple[str, ...], audio: bool) -> str | None:
+ parent = PureWindowsPath(directory)
+ roots = [PureWindowsPath(root) for root in library_roots if root]
+ containing = [root for root in roots if parent.is_relative_to(root)]
+ parts = (parent.relative_to(max(containing, key=lambda root: len(root.parts))).parts
+ if containing else parent.parts)
+ kinds = (_EXTRA_DIRECTORIES.get(part.casefold()) for part in reversed(parts))
+ return next((kind for kind in kinds if kind is not None and (kind == 'themeaudio') == audio), None)
+
+
+def local_extra_type(name: str | None, path: str | None = None, *,
+ library_roots: tuple[str, ...] = ()) -> str | None:
+ """显式文件标记或库根以下的附属目录;判定只读名字和路径。"""
+ filename = PureWindowsPath(name or path or '').name
+ suffix = PureWindowsPath(filename).suffix.casefold()
+ video, audio = suffix in VIDEO_EXTENSIONS, suffix in AUDIO_EXTENSIONS
+ if not (video or audio):
+ return None
+ stem = filename[:-len(suffix)].casefold().rstrip('0123456789')
+ if video:
+ if stem in ('trailer', 'sample'):
+ return stem
+ tagged = next((kind for token, kind in _EXTRA_SUFFIXES.items() if stem.endswith(token)), None)
+ if tagged:
+ return tagged
+ if audio and stem == 'theme':
+ return 'themeaudio'
+ return _extra_directory(str(PureWindowsPath(path).parent), tuple(library_roots), audio) if path else None
+
+
+def is_local_extra(name: str | None, path: str | None = None, *,
+ library_roots: tuple[str, ...] = ()) -> bool:
+ """是否为本地预告片、花絮或主题媒体。"""
+ return local_extra_type(name, path, library_roots=library_roots) is not None
LENGTH_TAGS = {"短片-2分内", "中片-10分内", "长片-30分内", "超长片-30分上"}
diff --git a/src/peach/library_processing.py b/src/peach/library_processing.py
index 0861f924c..3ccefe128 100644
--- a/src/peach/library_processing.py
+++ b/src/peach/library_processing.py
@@ -20,7 +20,7 @@ from urllib.parse import urlparse
from filelock import FileLock, Timeout
from PIL import Image
-from .catalog_rules import (compile_code_patterns, is_jav_code, is_local_trailer, normalise_code_key,
+from .catalog_rules import (compile_code_patterns, is_jav_code, is_local_extra, normalise_code_key,
release_code_from_filename, same_release_code, scrapes_as_jav)
from .field_owners import SCAN_FILENAME, write_owned_fields
from .profiles import read_settings
@@ -1913,11 +1913,14 @@ def _collection_rows(db_path, *, locations, online_roots, asset_ids):
query += ' AND id IN (' + ','.join('?' * len(asset_ids)) + ')'
parameters = asset_ids
query += ' ORDER BY id'
+ library_roots = tuple(root for roots in locations.values() for root in roots)
with closing(sqlite3.connect(db_path, timeout=30)) as connection:
connection.row_factory = sqlite3.Row
# 路径归属只比字面,避免逐行 resolve() 访问挂载服务。
rows = [dict(row) for row in connection.execute(query, parameters)
- if not is_local_trailer(row['name']) and row['location'] in locations
+ if not is_local_extra(row['name'], row['path'],
+ library_roots=library_roots)
+ and row['location'] in locations
and any(translate_ledger_path(row['path']).is_relative_to(root) for root in online_roots)]
genre_decisions = load_genre_decisions(connection)
return rows, genre_decisions
diff --git a/src/peach/media_probe.py b/src/peach/media_probe.py
index 62bf8001d..a94e033f5 100644
--- a/src/peach/media_probe.py
+++ b/src/peach/media_probe.py
@@ -27,7 +27,7 @@ from .media import resolve_case_insensitive
from .media_sources import cost_for
from .strm import skip_background_read
from .processing_control import CURRENT, checkpoint
-from .catalog_rules import is_local_trailer
+from .catalog_rules import is_local_extra
from . import junk, record_rehome
#: 一次探测最多等多久。ffprobe 只读文件头,网盘上实测一两秒。
@@ -139,7 +139,9 @@ def probe_unmeasured(
f"SELECT id,path FROM asset WHERE {_UNMEASURED} AND (path=? OR substr(path,1,?)=?){selected} ORDER BY id",
parameters).fetchall()
rows = [row for row in rows if cost_for(location, row[1], config) == 'free']
- return _record(db_path, ffprobe, rows, report, protected_ids=protected_ids, on_junk=on_junk)
+ roots = tuple(value for values in config.locations.values() for value in values) if config else (root,)
+ return _record(db_path, ffprobe, rows, report, protected_ids=protected_ids, on_junk=on_junk,
+ library_roots=roots)
def probe_path(db_path, ffprobe: str | None, location: str, path: str, *, config=None) -> int:
@@ -151,14 +153,15 @@ def probe_path(db_path, ffprobe: str | None, location: str, path: str, *, config
rows = connection.execute(
f"SELECT id,path FROM asset WHERE {_UNMEASURED} AND path=?",
(location, str(PureWindowsPath(path)))).fetchall()
- return _record(db_path, ffprobe, rows, lambda done, total: None)
+ roots = tuple(value for values in config.locations.values() for value in values) if config else ()
+ return _record(db_path, ffprobe, rows, lambda done, total: None, library_roots=roots)
def _record(db_path, ffprobe: str, rows: Sequence, report: Callable[[int, int], None], *,
- protected_ids=(), on_junk=lambda result: None) -> int:
+ protected_ids=(), on_junk=lambda result: None, library_roots=()) -> int:
"""探完一条写一条。`report` 抛出来(任务被停)时,还没开始的那些不再探。"""
rows = [row for row in rows if not skip_background_read(row[1])
- and not is_local_trailer(PureWindowsPath(row[1]).name)]
+ and not is_local_extra(PureWindowsPath(row[1]).name, row[1], library_roots=library_roots)]
if not rows:
return 0
done = 0
diff --git a/src/peach/processing_inventory.py b/src/peach/processing_inventory.py
index 355c8086b..f4ab77f08 100644
--- a/src/peach/processing_inventory.py
+++ b/src/peach/processing_inventory.py
@@ -38,6 +38,7 @@ class InventoryPreview:
def _files(self, config):
from .scan import _walk, is_sidecar, medium_of, walk_root_for
known = {self.meter._media_key(row['path'], row['location']) for row in self.rows}
+ library_roots = tuple(root for roots in config.locations.values() for root in roots)
seen = set()
for key, source in self.meter.rows.items():
if not source['root'] or key in known:
@@ -54,8 +55,9 @@ class InventoryPreview:
relative = Path(directory).relative_to(top).parts
for entry in entries:
row = None
- if medium_of(entry.name) == 'video' and not is_sidecar(entry.name, frozenset()):
- path = str(PureWindowsPath(root, *relative, entry.name))
+ path = str(PureWindowsPath(root, *relative, entry.name))
+ if medium_of(entry.name) == 'video' and not is_sidecar(
+ entry.name, frozenset(), path=path, library_roots=library_roots):
owner = self.meter._media_key(path, location)
identity = location, path.casefold()
if owner not in known and identity not in seen:
diff --git a/src/peach/processing_traffic.py b/src/peach/processing_traffic.py
index 5e151e200..b02cbca2f 100644
--- a/src/peach/processing_traffic.py
+++ b/src/peach/processing_traffic.py
@@ -140,7 +140,8 @@ class ProcessingTraffic:
"""账本统计不访问媒体目录;保护开启的目录不预估自动探测。"""
if rows is None:
from .processing_traffic_plan import ledger_rows
- rows = ledger_rows(self.db_path, self.asset_ids)
+ roots = tuple(root for values in self.config.locations.values() for root in values)
+ rows = ledger_rows(self.db_path, self.asset_ids, library_roots=roots)
with self.lock:
self.plan.refresh(rows)
for row in self.rows.values():
diff --git a/src/peach/processing_traffic_plan.py b/src/peach/processing_traffic_plan.py
index 6d847def8..e1b98d0f0 100644
--- a/src/peach/processing_traffic_plan.py
+++ b/src/peach/processing_traffic_plan.py
@@ -7,7 +7,7 @@ import sqlite3
from statistics import median
from contextlib import closing
-from .catalog_rules import is_local_trailer
+from .catalog_rules import is_local_extra
MIB = 1024 ** 2
@@ -20,7 +20,7 @@ def cached_names(directory):
return set()
-def ledger_rows(db_path, asset_ids=None):
+def ledger_rows(db_path, asset_ids=None, *, library_roots=()):
query = """SELECT asset.*,
EXISTS(SELECT 1 FROM asset_entity ae JOIN entity e ON ae.entity_id=e.id
WHERE ae.asset_id=asset.id AND e.kind='performer') AS performers,
@@ -33,7 +33,7 @@ def ledger_rows(db_path, asset_ids=None):
with closing(sqlite3.connect(db_path, timeout=30)) as connection:
connection.row_factory = sqlite3.Row
return [dict(row) for row in connection.execute(query, parameters)
- if not is_local_trailer(row['name'])]
+ if not is_local_extra(row['name'], row['path'], library_roots=library_roots)]
def typical_size(directory, pattern, fallback):
diff --git a/src/peach/repository.py b/src/peach/repository.py
index 9a0f4b435..3f8df0bac 100644
--- a/src/peach/repository.py
+++ b/src/peach/repository.py
@@ -6,7 +6,7 @@ from contextlib import contextmanager
from dataclasses import dataclass
from pathlib import Path
-from .catalog_rules import is_jav_code, is_local_trailer, normalise_code_key
+from .catalog_rules import is_jav_code, is_local_extra, normalise_code_key
from .entities import normalize_entity_name
from .regions import infer_region
@@ -14,8 +14,9 @@ from .regions import infer_region
class LedgerDatabase:
"""Shared SQLite connection and transaction boundary for one Peach app."""
- def __init__(self, db_path: Path):
+ def __init__(self, db_path: Path, *, media_roots=()):
self.db_path = Path(db_path)
+ self.media_roots = tuple(media_roots)
self.write_lock = threading.Lock()
self.after_commit = lambda: None
@@ -30,8 +31,9 @@ class LedgerDatabase:
)
connection.row_factory = sqlite3.Row
connection.create_function("is_jav_code", 1, is_jav_code, deterministic=True)
- connection.create_function(
- "peach_is_local_trailer", 1, is_local_trailer, deterministic=True)
+ roots = self.media_roots
+ connection.create_function("peach_is_local_extra", 2,
+ lambda name, path: is_local_extra(name, path, library_roots=roots), deterministic=True)
connection.create_function(
"normalise_code_key", 1, normalise_code_key, deterministic=True)
# 产地推断要和 Python 侧同一份实现。SQL 里重写一遍前缀名单的话,账本筛出来的
diff --git a/src/peach/scan.py b/src/peach/scan.py
index cdc6513a0..0bfc55b56 100644
--- a/src/peach/scan.py
+++ b/src/peach/scan.py
@@ -20,7 +20,7 @@
字幕 sidecar 顺带在这里登记:遍历时每个目录的文件名已经在手上,配对判据只看同目录,
`subtitles.py` 负责判定,本模块只负责把结论落进 `asset_subtitle`。
-刮削器与播放器写在正片旁边的附属文件不登记(`is_sidecar`):本地预告片、NFO、Kodi 命名的海报与背景图、
+刮削器与播放器写在正片旁边的附属文件不登记(`is_sidecar`):本地预告片、花絮、主题媒体、NFO、海报与背景图、
`extrafanart/` 里的剧照、截图工具拼出的缩略图。它们描述一部片,本身不是作品,登记进来就会在
馆藏里冒出成百张「图片」,把真正的图集淹掉。
@@ -38,6 +38,7 @@ import sqlite3
import time
from collections.abc import Callable, Mapping, Sequence
from dataclasses import asdict, dataclass
+from itertools import chain
from pathlib import Path, PureWindowsPath
from filelock import FileLock
@@ -45,11 +46,11 @@ from . import folder_rules, junk, record_rehome, subtitles, wants
from .platform import is_windows_path, is_unmapped, resolve_location, resolve_root
from .processing_control import checkpoint
from .fsutil import atomic_write_text
-from .catalog_rules import VIDEO_EXTENSIONS, is_local_trailer
+from .catalog_rules import AUDIO_EXTENSIONS, VIDEO_EXTENSIONS, is_local_extra
VIDEO = VIDEO_EXTENSIONS
IMAGE = {".jpg", ".jpeg", ".png", ".gif", ".bmp", ".webp"}
-AUDIO = {".mp3", ".flac", ".wav", ".m4a", ".ogg", ".opus"}
+AUDIO = AUDIO_EXTENSIONS
ARCHIVE = {".zip", ".rar", ".7z", ".tar", ".gz"}
#: 每攒够这么多行落一次盘,并报一行进度。
@@ -123,10 +124,11 @@ def medium_of(name: str) -> str:
def video_stems(names) -> frozenset[str]:
"""一个目录里视频文件的片名(casefold),`is_sidecar` 拿它判同目录有没有正片。"""
return frozenset(os.path.splitext(name)[0].casefold() for name in names
- if medium_of(name) == "video" and not is_local_trailer(name))
+ if medium_of(name) == "video" and not is_local_extra(name))
-def is_sidecar(name: str, videos: frozenset[str], *, artwork_dir: bool = False) -> bool:
+def is_sidecar(name: str, videos: frozenset[str], *, artwork_dir: bool = False,
+ path: str | None = None, library_roots: tuple[str, ...] = ()) -> bool:
"""`name` 是否为不登记的附属文件或工具隔离文件。
`videos` 是同目录的 `video_stems`;`artwork_dir` 表示这个目录叫 `extrafanart` 之类、
@@ -137,7 +139,7 @@ def is_sidecar(name: str, videos: frozenset[str], *, artwork_dir: bool = False)
stem, suffix = os.path.splitext(name)
if suffix.lower() == ".nfo":
return True
- if is_local_trailer(name):
+ if is_local_extra(name, path, library_roots=library_roots):
return True
if medium_of(name) != "image":
return False
@@ -260,9 +262,10 @@ def _directory_stats(directory: Path) -> dict[str, tuple[int, str]]:
return stats
-def _sidecar_on_disk(name: str, directory: Path) -> bool:
+def _sidecar_on_disk(name: str, directory: Path, *, ledger_path: str | None = None,
+ library_roots: tuple[str, ...] = ()) -> bool:
"""单条登记时的 `is_sidecar`:只在名字本身判不完时才列目录。"""
- if is_sidecar(name, frozenset()):
+ if is_sidecar(name, frozenset(), path=ledger_path, library_roots=library_roots):
return True
if medium_of(name) != "image":
return False
@@ -305,7 +308,8 @@ def ingest_path(
stat = (directory / name).stat()
except OSError:
return IngestResult(location, str(ledger_path), False)
- if _sidecar_on_disk(name, directory):
+ library_roots = tuple(chain.from_iterable(declared_roots.values()))
+ if _sidecar_on_disk(name, directory, ledger_path=str(ledger_path), library_roots=library_roots):
return IngestResult(location, str(ledger_path), True, stat.st_size, sidecar=True)
now = time.strftime("%Y-%m-%d %H:%M:%S")
mtime = time.strftime("%Y-%m-%d", time.localtime(stat.st_mtime))
@@ -476,6 +480,7 @@ def _scan_location(
if previous.get('result'):
return ScanResult(**{**previous['result'], 'new_ids': tuple(previous['result']['new_ids'])})
ledger_root = PureWindowsPath(root)
+ library_roots = tuple(chain.from_iterable(declared_roots.values()))
now = previous.get('scan_time') or time.strftime("%Y-%m-%d %H:%M:%S")
started = time.time()
progress = _ScanProgress(progress)
@@ -519,14 +524,15 @@ def _scan_location(
name = entry.name
if cursor is not None and name <= cursor:
continue
- if is_sidecar(name, videos, artwork_dir=artwork_dir):
+ ledger_path = str(ledger_root.joinpath(*relative, name))
+ if is_sidecar(name, videos, artwork_dir=artwork_dir,
+ path=ledger_path, library_roots=library_roots):
skipped += 1
continue
try:
stat = entry.stat()
except OSError:
continue
- ledger_path = str(ledger_root.joinpath(*relative, name))
mtime = time.strftime("%Y-%m-%d", time.localtime(stat.st_mtime))
here[name] = (stat.st_size, mtime)
batch.append((location, ledger_path, name, medium_of(name),
diff --git a/src/peach/web_catalog.py b/src/peach/web_catalog.py
index 162478280..96a45ed50 100644
--- a/src/peach/web_catalog.py
+++ b/src/peach/web_catalog.py
@@ -168,12 +168,12 @@ AGENCY_SEARCH_CLAUSE = (
)
-#: 普通馆藏展示在库正片,排除预告片、回收站和标「已消失」的资源。
+#: 普通馆藏展示在库正片,排除附属媒体、回收站和标「已消失」的资源。
#: (`disposal` 为空,ADR-0087)。列表、搜索和补全
#: 共用这一份。各写各的后果是补全会补出回收站里的作品——那一条点开是已经删掉的片,
#: 而列表本身从来不显示它,所以只会在补全这一个表面上露出来。
VISIBLE_CATALOG_ASSET = (
- "a.medium='video' AND a.disposal IS NULL AND NOT peach_is_local_trailer(a.name)"
+ "a.medium='video' AND a.disposal IS NULL AND NOT peach_is_local_extra(a.name,a.path)"
)
@@ -678,7 +678,7 @@ def _read_group_rows(contract: WebContract) -> list[dict]:
"SELECT id,name,path,code,size,duration,width,height,ctx_orient,disposal,catalog_title,field_owners "
"FROM asset "
"WHERE medium='video' "
- "AND COALESCE(disposal,'')<>'vanished' AND NOT peach_is_local_trailer(name) ORDER BY id",
+ "AND COALESCE(disposal,'')<>'vanished' AND NOT peach_is_local_extra(name,path) ORDER BY id",
)]
if not rows:
return []
@@ -1116,7 +1116,7 @@ def q_related(contract: WebContract, aid, limit=24):
"FROM asset a JOIN asset_entity shared "
"ON shared.asset_id=a.id WHERE a.medium='video' AND a.id<>? "
"AND (a.feedback IS NULL OR a.feedback<>'dislike') AND a.disposal IS NULL "
- "AND NOT peach_is_local_trailer(a.name) "
+ "AND NOT peach_is_local_extra(a.name,a.path) "
f"AND shared.entity_id IN ({source_marks}) GROUP BY a.id "
"ORDER BY count(DISTINCT shared.entity_id) DESC,a.id LIMIT 4000",
(aid, *source_entity_ids),
@@ -1213,7 +1213,7 @@ JAV_ASSET_PREDICATE = (
JAV_ASSET_CLAUSE = "AND " + JAV_ASSET_PREDICATE + " "
#: 筛选项、侧栏读数与顶部三层排掉标「已消失」的行;回收站里的照旧算(`q_facets`、`q_tops`)。
PRESENT_ASSET_CLAUSE = (
- "AND COALESCE(a.disposal,'')<>'vanished' AND NOT peach_is_local_trailer(a.name) "
+ "AND COALESCE(a.disposal,'')<>'vanished' AND NOT peach_is_local_extra(a.name,a.path) "
)
diff --git a/src/peach/web_entity.py b/src/peach/web_entity.py
index 52f53bba6..c479eaca7 100644
--- a/src/peach/web_entity.py
+++ b/src/peach/web_entity.py
@@ -102,7 +102,7 @@ def _missing_entity_result(connection, kind: str, name: str, entity_id: int | No
#: 资料页与索引页数作品的口径:视频,且没有标「已消失」(ADR-0087)。回收站里的照旧算;
#: 代表作的取图不套这一条。
COUNTED_VIDEO = (
- "a.medium='video' AND COALESCE(a.disposal,'')<>'vanished' AND NOT peach_is_local_trailer(a.name)"
+ "a.medium='video' AND COALESCE(a.disposal,'')<>'vanished' AND NOT peach_is_local_extra(a.name,a.path)"
)
diff --git a/src/peach/web_state.py b/src/peach/web_state.py
index 4ca7f6985..6d4355361 100644
--- a/src/peach/web_state.py
+++ b/src/peach/web_state.py
@@ -29,6 +29,7 @@ from .config import (
COVER_DIR,
DATA_ROOT,
GENERATED_DIR,
+ MEDIA_ROOT_DECLARATIONS,
SECRETS_DIR,
SHARED_CREDENTIAL_ROOT,
SOURCES_DIR,
@@ -166,7 +167,7 @@ class WebContract:
self.taste_history_manifest = (Path(taste_history_manifest)
if taste_history_manifest is not None
else STATE_DIR / "taste-history" / "manifest.json")
- self.database = database or LedgerDatabase(db_path)
+ self.database = database or LedgerDatabase(db_path, media_roots=MEDIA_ROOT_DECLARATIONS)
self.db_path = self.database.db_path
self.snapshot_root = Path(snapshot_root) if snapshot_root is not None else None
#: 键 → (算出时刻, 输入版本, 结果),见 `cached()`。
diff --git a/src/peach/web_stats.py b/src/peach/web_stats.py
index 63e7b0590..d6f25aba9 100644
--- a/src/peach/web_stats.py
+++ b/src/peach/web_stats.py
@@ -396,8 +396,8 @@ def _storage_volumes() -> list[dict[str, object]]:
#: 库存一节只数盘上还在的:标「已消失」的行留着只为个人记录(ADR-0087),不算库存。
#: 回收站里的照旧算进来。播放与消费一节是个人记录本身,不套这一条。
-PRESENT = "COALESCE(disposal,'')<>'vanished' AND NOT peach_is_local_trailer(name)"
-PRESENT_A = "COALESCE(a.disposal,'')<>'vanished' AND NOT peach_is_local_trailer(a.name)"
+PRESENT = "COALESCE(disposal,'')<>'vanished' AND NOT peach_is_local_extra(name,path)"
+PRESENT_A = "COALESCE(a.disposal,'')<>'vanished' AND NOT peach_is_local_extra(a.name,a.path)"
#: 视频的时长与画质分档,顺序同 `media_probe.context_fields` 的判据:由短到长、由高到低。
diff --git a/tests/test_media_probe.py b/tests/test_media_probe.py
index 402556f83..d3fe31b5b 100644
--- a/tests/test_media_probe.py
+++ b/tests/test_media_probe.py
@@ -91,6 +91,15 @@ class ProbeUnmeasuredTests(unittest.TestCase):
self.assertEqual(self.row(main)[0], 1800)
self.assertIsNone(self.row(trailer)[0])
+ def test_directory_extras_and_numbered_suffixes_are_not_probed(self):
+ main = self.add(ROOT + r'\Film\Film.mp4')
+ extras = [self.add(ROOT + relative) for relative in (
+ r'\Film\Trailers\Teaser.mp4', r'\Film\Extras\Disc 1\Making of.mkv',
+ r'\Film\Actor-interview2.mp4', r'\Film\sample2.mp4')]
+ self.assertEqual(self.probe(), 1)
+ self.assertEqual(self.row(main)[0], 1800)
+ self.assertTrue(all(self.row(aid)[0] is None for aid in extras))
+
def test_without_ffprobe_the_scan_still_finishes_and_leaves_rows_pending(self):
pending = self.add(ROOT + r"\a.mp4")
self.assertEqual(self.probe(ffprobe=None), 0)
diff --git a/tests/test_metadata_library.py b/tests/test_metadata_library.py
index 9bb8b6748..4d4f4c13c 100644
--- a/tests/test_metadata_library.py
+++ b/tests/test_metadata_library.py
@@ -101,6 +101,20 @@ class LibraryNfoTests(unittest.TestCase):
self.assertEqual([row['name'] for row in rows], ['ABC-123.mp4'])
self.assertEqual(db.read_bytes(), before)
+ def test_collection_preserves_directory_extras_and_selects_only_the_feature(self):
+ from peach.library_processing import _collection_rows
+ db = fresh_ledger(self.root)
+ names = ('Film/Film.mp4', 'Film/Extras/Disc 1/Making of.mkv', 'Film/Actor-interview2.mp4')
+ with closing(sqlite3.connect(db)) as connection, connection:
+ for name in names:
+ connection.execute('INSERT INTO asset(location,path,name,medium) VALUES(?,?,?,?)',
+ ('local', str(self.root / name), Path(name).name, 'video'))
+ before = db.read_bytes()
+ rows, _ = _collection_rows(db, locations={'local': (str(self.root),)},
+ online_roots=[self.root], asset_ids=None)
+ self.assertEqual([row['name'] for row in rows], ['Film.mp4'])
+ self.assertEqual(db.read_bytes(), before)
+
def test_task_sidecar_index_does_not_walk_the_folder_for_each_video(self):
from peach.library_nfo import SidecarIndex
class CountedFiles(dict):
diff --git a/tests/test_processing_inventory.py b/tests/test_processing_inventory.py
index 7df884975..9a94cff63 100644
--- a/tests/test_processing_inventory.py
+++ b/tests/test_processing_inventory.py
@@ -78,6 +78,26 @@ class InventoryTests(unittest.TestCase):
self.assertEqual(registered['workload']['video_files'], 1)
self.assertEqual(self.db.read_bytes(), before)
+ def test_extra_directories_have_no_discovered_or_registered_workload(self):
+ relative = ('Film/Film.mp4', 'Film/Trailers/Teaser.mp4',
+ 'Film/Extras/Disc 1/Making of.mp4', 'Film/Actor-interview2.mkv')
+ for value in relative:
+ target = self.media / value
+ target.parent.mkdir(parents=True, exist_ok=True)
+ target.touch()
+ before = self.db.read_bytes()
+ result = preview(self.config, self.db, 'all', None)
+ self.assertEqual(result['workload']['video_files'], 1)
+ self.assertEqual(self.db.read_bytes(), before)
+ with closing(sqlite3.connect(self.db)) as connection, connection:
+ for value in relative:
+ connection.execute('INSERT INTO asset(location,path,name,medium) VALUES(?,?,?,?)',
+ ('mounted', 'A:/Cloud/' + value, Path(value).name, 'video'))
+ before = self.db.read_bytes()
+ result = preview(self.config, self.db, 'all', None)
+ self.assertEqual(result['workload']['video_files'], 1)
+ self.assertEqual(self.db.read_bytes(), before)
+
def test_each_query_advances_a_batch_and_preserves_deduplication(self):
for number in range(1100):
(self.media / f'ABW-001-CD{number}.mp4').touch()
diff --git a/tests/test_push_discovery.py b/tests/test_push_discovery.py
index 4b6f6f1ea..f11958900 100644
--- a/tests/test_push_discovery.py
+++ b/tests/test_push_discovery.py
@@ -396,6 +396,20 @@ class IngestPathTests(_ServiceCase):
self.assertEqual((result.found, result.sidecar), (True, True), parts)
self.assertEqual(self.rows(), {})
+ def test_extra_suffixes_and_nested_folders_are_found_without_ledger_writes(self):
+ relative = ('Film/Interview-interview2.mkv', 'Film/Extras/Trailers/Teaser.mp4',
+ 'Film/deleted scenes/Ending.mp4', 'Film/backdrops/Menu.mkv',
+ 'Film/theme-music/Score.flac')
+ for value in relative:
+ target = self.media / value
+ target.parent.mkdir(parents=True, exist_ok=True)
+ target.write_bytes(b'clip')
+ for value in relative:
+ result = scan.ingest_path(self.db, 'local', self.ledger('local', *value.split('/')),
+ declared_roots=self.declared, mounts=self.mounts)
+ self.assertEqual((result.found, result.sidecar, result.asset_id), (True, True, None), value)
+ self.assertEqual(self.rows(), {})
+
def test_a_slow_directory_listing_does_not_hold_the_ledger_write_lock(self):
"""挂载卡在列目录上时,同一账本的另一个写入者照样能在短超时内拿到写锁。"""
(self.media / "a.mp4").write_bytes(b"0" * 4)
diff --git a/tests/test_rm_web.py b/tests/test_rm_web.py
index 751a4085c..fcc83516d 100644
--- a/tests/test_rm_web.py
+++ b/tests/test_rm_web.py
@@ -25,6 +25,34 @@ from support.ledger import fresh_ledger
class WebDataTests(unittest.TestCase):
+ def test_registered_directory_extras_are_excluded_and_personal_records_remain_accessible(self):
+ self.contract.database.media_roots = (r'R:\Media',)
+ with self.contract.write_transaction() as connection:
+ connection.execute("UPDATE asset SET name='Film.mp4',path=? WHERE id=1", (r'R:\Media\Film\Film.mp4',))
+ connection.execute("UPDATE asset SET name='Featurette.mp4',path=?,location='local',rating=4,"
+ "play_count=3 WHERE id=2", (r'R:\Media\Film\Extras\Disc 1\Featurette.mp4',))
+ connection.execute("INSERT INTO asset_entity(asset_id,entity_id,role,source,confidence) "
+ "VALUES(2,11,'performer','test',1.0)")
+ with self.contract.read_connection() as connection:
+ before = [tuple(row) for row in connection.execute('SELECT * FROM asset ORDER BY id')]
+ self.assertEqual([row['id'] for row in q_items(self.contract, {})['items']], [1])
+ self.assertEqual(rm_web.q_related(self.contract, 1)['items'], [])
+ self.assertEqual(rm_web.q_facets(self.contract)['stats']['total'], 1)
+ self.assertEqual(rm_web.q_stats(self.contract)['attribution']['videos'], 1)
+ self.assertEqual([row['id'] for row in web_catalog._catalog_group_rows(self.contract)], [1])
+ self.assertEqual(rm_web.q_item(self.contract, 2)['rating'], 4)
+ people = rm_web.q_index(self.contract, 'performers')['items']
+ self.assertEqual(next(row['n'] for row in people if row['k'] == 'Canonical Alice'), 1)
+ with self.contract.read_connection() as connection:
+ self.assertEqual([tuple(row) for row in connection.execute('SELECT * FROM asset ORDER BY id')], before)
+
+ def test_named_extra_directory_can_be_an_independent_library_root(self):
+ self.contract.database.media_roots = ('R:/Media/Shorts',)
+ with self.contract.write_transaction() as connection:
+ connection.execute("UPDATE asset SET path='R:/Media/Shorts/one.mp4' WHERE id=1")
+ connection.execute("UPDATE asset SET path='R:/Media/Shorts/Film/extras/two.mp4' WHERE id=2")
+ self.assertEqual([row['id'] for row in q_items(self.contract, {})['items']], [1])
+
def test_registered_trailers_stay_out_of_catalog_and_counts_without_changing_records(self):
with self.contract.write_transaction() as connection:
connection.execute("UPDATE asset SET name='ABC-001.mp4',duration=7200 WHERE id=1")
diff --git a/tests/test_scan.py b/tests/test_scan.py
index 9c62a6cb1..4dd73f223 100644
--- a/tests/test_scan.py
+++ b/tests/test_scan.py
@@ -19,7 +19,7 @@ from contextlib import closing, redirect_stdout
from pathlib import Path, PureWindowsPath
from unittest.mock import patch
-from peach import scan
+from peach import catalog_rules, scan
from peach.migrations import upgrade
from support.web import route_client
@@ -310,12 +310,12 @@ class SidecarTests(_ScanCase):
def test_movie_trailers_are_sidecars_and_short_main_features_are_kept(self):
self._layout(['ABC-123/ABC-123.mp4', 'ABC-123/ABC-123-trailer.mp4',
'ABC-123/ABC-123.nfo', 'ABC-123/ABC-123-poster.jpg',
- 'ABC-123/extrafanart/1.jpg', 'ABC-123/ABC-123-short.mp4'])
+ 'ABC-123/extrafanart/1.jpg', 'ABC-123/short-film.mp4'])
result, _ = self._scan(r'R:\media\ABC-123', declared_roots=self.DECLARED,
mounts={'local': (self.media,)}, windows=False)
self.assertEqual((result.files, result.sidecars), (2, 4))
self.assertEqual(set(self._paths()), {
- r'R:\media\ABC-123\ABC-123.mp4', r'R:\media\ABC-123\ABC-123-short.mp4'})
+ r'R:\media\ABC-123\ABC-123.mp4', r'R:\media\ABC-123\short-film.mp4'})
self.assertTrue((self.media / 'ABC-123/ABC-123-trailer.mp4').is_file())
def test_trailer_names_use_an_explicit_terminal_marker(self):
@@ -330,6 +330,96 @@ class SidecarTests(_ScanCase):
with self.subTest(name=name):
self.assertFalse(scan.is_sidecar(name, frozenset()))
+ def test_media_server_extras_and_nested_directories_are_not_registered(self):
+ paths = ['Film/Film.mkv', 'Film/Trailer 1-trailer.MP4', 'Film/Interview-interview2.mkv',
+ 'Film/trailers/Teaser.mp4', 'Film/Extras/Trailers/Trailer 2.mkv',
+ 'Film/behind the scenes/Making of.mp4', 'Film/deleted scenes/Ending.mp4',
+ 'Film/shorts/Bonus.mp4', 'Film/specials/Bonus.mp4',
+ 'Film/backdrops/Menu.mkv', 'Film/theme-music/Score.flac', 'Film/theme.mp3']
+ self._layout(paths)
+ result, _ = self._scan(r'R:\media\Film', declared_roots=self.DECLARED,
+ mounts={'local': (self.media,)}, windows=False)
+ self.assertEqual((result.files, result.sidecars), (1, len(paths) - 1))
+ self.assertEqual(self._paths(), {r'R:\media\Film\Film.mkv': 'video'})
+ self.assertTrue(all((self.media / name).is_file() for name in paths))
+
+ def test_a_declared_shorts_root_contains_independent_features(self):
+ self._layout(['Shorts/One.mp4', 'Shorts/Film/extras/Making of.mp4'])
+ result, _ = self._scan(r'R:\media\Shorts', declared_roots={'local': (r'R:\media\Shorts',)},
+ mounts={'local': (self.media / 'Shorts',)}, windows=False)
+ self.assertEqual((result.files, result.sidecars), (1, 1))
+ self.assertEqual(self._paths(), {r'R:\media\Shorts\One.mp4': 'video'})
+
+
+class LocalExtraNamingTests(unittest.TestCase):
+ def test_movie_and_episode_suffixes_and_numbered_bare_names(self):
+ examples = {
+ 'trailer.mp4': 'trailer', 'trailer2.MKV': 'trailer', 'sample1.mp4': 'sample',
+ 'Film-trailer1.mp4': 'trailer', 'Film.trailer2.mkv': 'trailer',
+ 'Film_trailer3.mov': 'trailer', 'Film trailer4.m4v': 'trailer',
+ 'Film- trailer5.mp4': 'trailer', 'Film-sample1.mp4': 'sample',
+ 'Film.sample2.mkv': 'sample', 'Film_sample3.mov': 'sample',
+ 'Film sample4.mp4': 'sample', 'Film- sample5.mp4': 'sample',
+ 'Performance Capture-behindthescenes.mkv': 'behindthescenes',
+ 'Ending-deleted.mp4': 'deleted', 'Ending-deletedscene2.mp4': 'deleted',
+ 'Special Effects-featurette.mp4': 'featurette', 'Actor-interview.mp4': 'interview',
+ 'Arrival-scene.mp4': 'scene', 'Moment-clip.mp4': 'clip',
+ 'Bonus-short.mp4': 'short', 'Bonus-extra.mp4': 'other', 'Bonus-other.mp4': 'other',
+ 'Show - S01E01 - Episode-deleted2.mkv': 'deleted', 'theme.flac': 'themeaudio',
+ }
+ for name, expected in examples.items():
+ with self.subTest(name=name):
+ self.assertEqual(catalog_rules.local_extra_type(name), expected)
+ self.assertTrue(scan.is_sidecar(name, frozenset()))
+
+ def test_exact_markers_preserve_titles_and_unsupported_files(self):
+ for name in ('Short Film.mp4', 'Trailer Park.mp4', 'Interview.mp4', 'Sample Story.mp4',
+ 'Filmtrailer.mp4', 'Film-short-story.mp4', 'Film-deleted-scenes.mp4',
+ 'Film-trailerA.mp4', 'Film-trailer.mp4.bak', 'Film-trailer.jpg',
+ 'Film-interview.mp3', 'theme.mp4', '', None):
+ with self.subTest(name=name):
+ self.assertIsNone(catalog_rules.local_extra_type(name))
+
+ def test_dedicated_directories_are_case_insensitive_and_allow_nesting(self):
+ directories = {'Trailers': 'trailer', 'Behind The Scenes': 'behindthescenes',
+ 'Deleted Scenes': 'deleted', 'Interviews': 'interview', 'Scenes': 'scene',
+ 'Samples': 'sample', 'Sample': 'sample', 'Shorts': 'short',
+ 'Featurettes': 'featurette', 'Clips': 'clip', 'Other': 'other',
+ 'Extras': 'other', 'Extra': 'other', 'Specials': 'other', 'Backdrops': 'themevideo'}
+ for directory, expected in directories.items():
+ for path in (f'R:/Movies/Film/{directory}/Clip.mp4',
+ f'R:/Movies/Film/{directory}/Disc 1/Clip.mp4'):
+ with self.subTest(path=path):
+ self.assertEqual(catalog_rules.local_extra_type('Clip.mp4', path,
+ library_roots=('R:/Movies',)), expected)
+ self.assertEqual(catalog_rules.local_extra_type('Score.opus', '/movies/Film/theme-music/Score.opus'),
+ 'themeaudio')
+ self.assertIsNone(catalog_rules.local_extra_type('Score.opus', '/movies/Film/trailers/Score.opus'))
+ self.assertEqual(catalog_rules.local_extra_type('Clip.mp4', '/movies/Film/Extras/theme-music/Clip.mp4'),
+ 'other')
+ self.assertEqual(catalog_rules.local_extra_type('Score.opus', '/movies/Film/theme-music/clips/Score.opus'),
+ 'themeaudio')
+
+ def test_longest_library_root_is_the_directory_boundary(self):
+ for path, roots in ((r'R:\Shorts\Film.mp4', ('R:/Shorts',)),
+ ('/Movies/Extras/Film.mp4', ('/Movies', '/Movies/Extras')),
+ (r'\\server\Shorts\Film.mp4', (r'\\server\Shorts',))):
+ with self.subTest(path=path):
+ self.assertIsNone(catalog_rules.local_extra_type('Film.mp4', path, library_roots=roots))
+ self.assertEqual(catalog_rules.local_extra_type('Clip.mp4', 'R:/Shorts/Film/Extras/Clip.mp4',
+ library_roots=('r:/shorts',)), 'other')
+
+ def test_sqlite_classifier_uses_configured_library_boundaries(self):
+ from peach.repository import LedgerDatabase
+ with tempfile.TemporaryDirectory() as temporary:
+ path = Path(temporary).resolve() / 'ledger.db'
+ sqlite3.connect(path).close()
+ database = LedgerDatabase(path, media_roots=('R:/Movies', 'R:/Movies/Shorts'))
+ with database.read_connection() as connection:
+ values = [connection.execute("SELECT peach_is_local_extra('Film.mp4',?)", (value,)).fetchone()[0]
+ for value in ('R:/Movies/Shorts/Film.mp4', 'R:/Movies/Shorts/Film/Extras/Film.mp4')]
+ self.assertEqual(values, [0, 1])
+
def test_the_rule_reads_names_only(self):
videos = scan.video_stems(["ABC-1.mp4", "notes.txt"])
self.assertEqual(videos, frozenset({"abc-1"}))
diff --git a/tests/test_web_library_shortcuts.py b/tests/test_web_library_shortcuts.py
index 1c67b5639..2314b9964 100644
--- a/tests/test_web_library_shortcuts.py
+++ b/tests/test_web_library_shortcuts.py
@@ -26,7 +26,7 @@ class LibraryShortcutsTests(unittest.TestCase):
CREATE TABLE entity_redirect(old_id INTEGER,target_id INTEGER);
CREATE TABLE entity_alias(entity_id INTEGER,alias TEXT);
INSERT INTO entity_alias VALUES(3,'故事');
- CREATE TABLE asset(id INTEGER PRIMARY KEY,medium TEXT,disposal TEXT,size INTEGER,snapshot_path TEXT,name TEXT);
+ CREATE TABLE asset(id INTEGER PRIMARY KEY,medium TEXT,disposal TEXT,size INTEGER,snapshot_path TEXT,name TEXT,path TEXT);
CREATE TABLE asset_entity(asset_id INTEGER,entity_id INTEGER,role TEXT);
""")
self.contract = WebContract(self.path)