From 199417cacb29bde0f76ea5a1f4163cbfedf3af01 Mon Sep 17 00:00:00 2001 From: Yuukiy <76897913+Yuukiy@users.noreply.github.com> Date: Fri, 5 Apr 2024 15:44:25 +0800 Subject: [PATCH] =?UTF-8?q?Fix=20#268:=20=E4=BF=AE=E5=A4=8D=E7=B4=A7?= =?UTF-8?q?=E8=B7=9F=E5=BD=B1=E7=89=87=E7=95=AA=E5=8F=B7=E7=9A=84CDx?= =?UTF-8?q?=E8=A2=AB=E8=AF=AF=E8=AF=86=E5=88=AB=E4=B8=BA=E5=86=85=E5=B5=8C?= =?UTF-8?q?=E5=AD=97=E5=B9=95=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (cherry picked from commit 729f652f933d26947f0e81ec35f013511e1385de) --- core/avid.py | 3 ++- core/lib.py | 4 ++-- unittest/test_avid.py | 1 + unittest/test_file.py | 11 +++++++++++ unittest/test_lib.py | 3 ++- 5 files changed, 18 insertions(+), 4 deletions(-) diff --git a/core/avid.py b/core/avid.py index f48df8c35..e78be7af8 100644 --- a/core/avid.py +++ b/core/avid.py @@ -97,11 +97,12 @@ def get_id(filepath: str) -> str: return '' +CD_POSTFIX = re.compile(r'([-_]\w|cd\d)$') def get_cid(filepath: str) -> str: """尝试将给定的文件名匹配为CID(Content ID)""" basename = os.path.splitext(os.path.basename(filepath))[0] # 移除末尾可能带有的分段影片序号 - possible = re.sub(r'[-_]\w$', '', basename) + possible = CD_POSTFIX.sub('', basename) # cid只由数字、小写字母和下划线组成 match = re.match(r'^([a-z\d_]+)$', possible, re.A) if match: diff --git a/core/lib.py b/core/lib.py index ec056a35f..5577cd817 100644 --- a/core/lib.py +++ b/core/lib.py @@ -59,7 +59,7 @@ def detect_special_attr(filepath: str, avid: str = None) -> str: if postfix in ('U', 'C', 'UC'): result += postfix elif avid: - pattern_str = re.sub(r'[_-]', '[_-]*', avid) + '(UC|U|C)' + pattern_str = re.sub(r'[_-]', '[_-]*', avid) + r'(UC|U|C)\b' match = re.search(pattern_str, base, flags=re.I) if match: result += match.group(1) @@ -69,4 +69,4 @@ def detect_special_attr(filepath: str, avid: str = None) -> str: if __name__ == "__main__": - print(detect_special_attr('STARS225Uc.mp4', 'STARS-225')) + print(detect_special_attr('ipx-177cd1.mp4', 'IPX-177')) diff --git a/unittest/test_avid.py b/unittest/test_avid.py index aa51709fc..209731ef1 100644 --- a/unittest/test_avid.py +++ b/unittest/test_avid.py @@ -54,6 +54,7 @@ def test_cid_valid(): assert '1234wvr00001rp' == get_cid('1234wvr00001rp.mp4') assert '402abc_hello000089' == get_cid('402abc_hello000089.mp4') assert 'h_826zizd021' == get_cid('h_826zizd021.mp4') + assert '403abcd56789' == get_cid('403abcd56789cd1.mp4') def test_from_file(): diff --git a/unittest/test_file.py b/unittest/test_file.py index 7d8108157..cec2facc7 100644 --- a/unittest/test_file.py +++ b/unittest/test_file.py @@ -97,6 +97,17 @@ def test_scan_movies__cdx(prepare_files): assert basenames[2] == 'ABC-123.CD3.mp4' +@pytest.mark.parametrize('files', [('abc123cd1.mp4','abc123cd2.mp4')]) +def test_scan_movies__cdx_without_delimeter(prepare_files): + movies = scan_movies(tmp_folder) + assert len(movies) == 1 + assert movies[0].dvdid == 'abc-123' + assert len(movies[0].files) == 2 + basenames = [os.path.basename(i) for i in movies[0].files] + assert basenames[0] == 'abc123cd1.mp4' + assert basenames[1] == 'abc123cd2.mp4' + + # 文件夹以番号命名,分片位于文件夹内且无番号信息 @pytest.mark.parametrize('files', [('ABC-123/CD1.mp4','ABC-123/CD2 .mp4','ABC-123/CD3.mp4')]) def test_scan_movies__from_folder(prepare_files): diff --git a/unittest/test_lib.py b/unittest/test_lib.py index 1d7fb5504..53bb0ec8a 100644 --- a/unittest/test_lib.py +++ b/unittest/test_lib.py @@ -1,6 +1,5 @@ import os import sys -import random sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) from core.lib import * @@ -23,3 +22,5 @@ def test_detect_special_attr(): assert run('STARS225u.mp4', 'STARS-225') == 'U' assert run('STARS225C.mp4', 'STARS-225') == 'C' assert run('STARS225uC.mp4', 'STARS-225') == 'UC' + assert run('STARS-225CD1.mp4', 'STARS-225') == '' + assert run('stars225cd2.mp4', 'STARS-225') == ''