Skip to content

Commit

Permalink
fix: 修复歌曲批量重命名的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
hanxi committed Dec 25, 2024
1 parent d98652f commit f728626
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
33 changes: 27 additions & 6 deletions test/test_remove_common_prefix.py
Original file line number Diff line number Diff line change
@@ -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")
9 changes: 6 additions & 3 deletions xiaomusic/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -965,17 +965,20 @@ 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
# 检查文件名是否以共同前缀开头
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)
Expand Down

0 comments on commit f728626

Please sign in to comment.