diff --git a/test/test_remove_common_prefix.py b/test/test_remove_common_prefix.py index 33f177ba56..c956d9d8c5 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 2bb997a4eb..346f5177c4 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)