From f7286269af4046d2f79b062f9b88fea736aefd14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B6=B5=E6=9B=A6?= Date: Wed, 25 Dec 2024 16:20:06 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=AD=8C=E6=9B=B2?= =?UTF-8?q?=E6=89=B9=E9=87=8F=E9=87=8D=E5=91=BD=E5=90=8D=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/test_remove_common_prefix.py | 33 +++++++++++++++++++++++++------ xiaomusic/utils.py | 9 ++++++--- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/test/test_remove_common_prefix.py b/test/test_remove_common_prefix.py index 33f177b..c956d9d 100644 --- a/test/test_remove_common_prefix.py +++ b/test/test_remove_common_prefix.py @@ -1,8 +1,29 @@ -from xiaomusic.utils import ( - remove_common_prefix, -) +import re + + +def removepre(filename): + match = re.search(r"^(\d+)\s+\d*(.+?)\.(.*$)", filename.strip()) + new_filename = filename + if match: + num = match.group(1) + name = match.group(2).replace(".", " ").strip() + suffix = match.group(3) + # print(name) + # print(num) + # print(suffix) + new_filename = f"{num}.{name}.{suffix}" + + print(filename, "=>", new_filename) + if __name__ == "__main__": - remove_common_prefix( - "./tmp/【无损音质】2024年9月酷狗热歌榜TOP100合集(只选热歌最高的)首首王炸,分P合集!" - ) + removepre(" 17 《白色风车》.mp3") + removepre(" 17 《白色风车》.mp3") + removepre(" 17 17 《白色风车》.mp3") + removepre(" 17 17 《白色风车》.mp3") + + removepre(" 18 风车.mp3") + removepre(" 18 色风车.mp3") + removepre(" 18 18 你好.mp3") + removepre(" 18 18 我好.mp3") + removepre("09 009. 梁静茹-亲亲.mp3") diff --git a/xiaomusic/utils.py b/xiaomusic/utils.py index 2bb997a..346f517 100644 --- a/xiaomusic/utils.py +++ b/xiaomusic/utils.py @@ -965,7 +965,7 @@ def remove_common_prefix(directory): log.info(f'Common prefix identified: "{common_prefix}"') - pattern = re.compile(r"(\d+)[\t  ]*\1") + pattern = re.compile(r"^(\d+)\s+\d*(.+?)\.(.*$)") for filename in files: if filename == common_prefix: continue @@ -973,9 +973,12 @@ def remove_common_prefix(directory): if filename.startswith(common_prefix): # 构造新的文件名 new_filename = filename[len(common_prefix) :] - match = pattern.match(new_filename) + match = pattern.search(new_filename.strip()) if match: - new_filename = match.group(1) + new_filename[match.end() :] + num = match.group(1) + name = match.group(2).replace(".", " ").strip() + suffix = match.group(3) + new_filename = f"{num}.{name}.{suffix}" # 生成完整的文件路径 old_file_path = os.path.join(directory, filename) new_file_path = os.path.join(directory, new_filename)