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') == ''