Skip to content

Commit

Permalink
refactor: 优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
hanxi committed Jul 19, 2024
1 parent 7744a75 commit 7877775
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 33 deletions.
6 changes: 5 additions & 1 deletion xiaomusic/httpserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ def reset_http_server():

# 更新 music 链接
app.router.routes = [route for route in app.router.routes if route.path != "/music"]
app.mount("/music", StaticFiles(directory=config.music_path,follow_symlink=True), name="music")
app.mount(
"/music",
StaticFiles(directory=config.music_path, follow_symlink=True),
name="music",
)


def HttpInit(_xiaomusic):
Expand Down
26 changes: 11 additions & 15 deletions xiaomusic/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@
import os
import random
import re
import shutil
import string
import tempfile
from collections.abc import AsyncIterator
from http.cookies import SimpleCookie
from urllib.parse import urlparse
from mutagen.id3 import ID3
from mutagen.mp3 import MP3
import os
import shutil

import aiohttp
import mutagen
from mutagen.id3 import ID3
from mutagen.mp3 import MP3
from requests.utils import cookiejar_from_dict

from xiaomusic.const import SUPPORT_MUSIC_TYPE
Expand Down Expand Up @@ -300,20 +299,21 @@ def parse_str_to_dict(s, d1=",", d2=":"):

return result


# remove mp3 file id3 tag and padding to reduce delay
def no_padding(info):
# this will remove all padding
return 0

def remove_id3_tags(filename,music_path):

file_path = music_path + "/" + filename
def remove_id3_tags(file_path):
audio = MP3(file_path, ID3=ID3)
change = False

# 检查是否存在ID3 v2.3或v2.4标签
if audio.tags and (audio.tags.version == (2, 3, 0) or audio.tags.version == (2, 4, 0)):

# 检查是否存在ID3 v2.3或v2.4标签
if audio.tags and (
audio.tags.version == (2, 3, 0) or audio.tags.version == (2, 4, 0)
):
# 构造新文件的路径
new_file_path = file_path + ".bak"

Expand All @@ -325,14 +325,10 @@ def remove_id3_tags(filename,music_path):

# 删除padding
audio.save(padding=no_padding)

# 保存修改后的文件
audio.save()

change = True

return filename,change




return change
34 changes: 17 additions & 17 deletions xiaomusic/xiaomusic.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,11 @@
fuzzyfinder,
get_local_music_duration,
get_web_music_duration,
is_mp3,
parse_cookie_string,
parse_str_to_dict,
traverse_music_directory,
remove_id3_tags,
is_mp3

traverse_music_directory,
)


Expand Down Expand Up @@ -371,21 +370,22 @@ def get_music_url(self, name):
self.log.debug(f"get_music_url web music. name:{name}, url:{url}")
return url

filename = self.get_filename(name).replace("\\", "/")
filename = self.get_filename(name)
# 移除MP3 ID3 v2标签和填充,减少播放前延迟
if self.remove_id3tag and is_mp3(filename):
self.log.info(f"remove_id3tag:{self.remove_id3tag}, is_mp3:True ")
change = remove_id3_tags(filename)
if change:
self.log.info("ID3 tag removed, orgin mp3 file saved as bak")
else:
self.log.info("No ID3 tag remove needed")

filename = filename.replace("\\", "/")
if filename.startswith(self.config.music_path):
filename = filename[len(self.config.music_path) :]
if filename.startswith("/"):
filename = filename[1:]
self.log.debug(f"get_music_url local music. name:{name}, filename:{filename}")

#移除MP3 ID3 v2标签和填充,减少播放前延迟
if self.remove_id3tag and is_mp3(f"{self.music_path}/{filename}"):
self.log.info(f"remove_id3tag:{self.remove_id3tag}, is_mp3:True ")
filename,change = remove_id3_tags(filename,self.music_path)
if change:
self.log.info(f"ID3 tag removed, orgin mp3 file saved as bak")
else:
self.log.info(f"No ID3 tag remove needed")
encoded_name = urllib.parse.quote(filename)
return f"http://{self.hostname}:{self.public_port}/music/{encoded_name}"

Expand Down Expand Up @@ -418,12 +418,12 @@ def _gen_all_music_list(self):
all_music_by_dir[dir_name][name] = True
self.log.debug(f"_gen_all_music_list {name}:{dir_name}:{file}")

#self.log.debug(self.all_music)
# self.log.debug(self.all_music)

self.music_list = {}
for dir_name, musics in all_music_by_dir.items():
self.music_list[dir_name] = list(musics.keys())
#self.log.debug("dir_name:%s, list:%s", dir_name, self.music_list[dir_name])
# self.log.debug("dir_name:%s, list:%s", dir_name, self.music_list[dir_name])

try:
self._append_music_list()
Expand Down Expand Up @@ -471,8 +471,8 @@ def _append_music_list(self):
self.music_list[list_name] = one_music_list
if self._all_radio:
self.music_list["所有电台"] = list(self._all_radio.keys())
#self.log.debug(self.all_music)
#self.log.debug(self.music_list)
# self.log.debug(self.all_music)
# self.log.debug(self.music_list)
except Exception as e:
self.log.exception(f"Execption {e}")

Expand Down

0 comments on commit 7877775

Please sign in to comment.