From 444e697f9da0be03456a403d07e7ba51162fd741 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B6=B5=E6=9B=A6?= Date: Thu, 1 Aug 2024 15:52:20 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E9=83=A8=E5=88=86?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E8=8E=B7=E5=8F=96=E4=B8=8D=E5=88=B0=E6=92=AD?= =?UTF-8?q?=E6=94=BE=E6=97=B6=E9=95=BF=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pdm.lock | 39 ++++++++++++++++++++++++------------- test/test_music_duration.py | 32 ++++++++++++++++++++++++++++++ xiaomusic/utils.py | 9 +++------ 3 files changed, 61 insertions(+), 19 deletions(-) create mode 100644 test/test_music_duration.py diff --git a/pdm.lock b/pdm.lock index 0206858..9254daa 100644 --- a/pdm.lock +++ b/pdm.lock @@ -25,14 +25,27 @@ files = [ {file = "aiofiles-24.1.0.tar.gz", hash = "sha256:22a075c9e5a3810f0c2e48f3008c94d68c65d763b9b03857924c99e57355166c"}, ] +[[package]] +name = "aiohappyeyeballs" +version = "2.3.4" +requires_python = "<4.0,>=3.8" +summary = "Happy Eyeballs for asyncio" +groups = ["default"] +marker = "python_full_version == \"3.10.12\"" +files = [ + {file = "aiohappyeyeballs-2.3.4-py3-none-any.whl", hash = "sha256:40a16ceffcf1fc9e142fd488123b2e218abc4188cf12ac20c67200e1579baa42"}, + {file = "aiohappyeyeballs-2.3.4.tar.gz", hash = "sha256:7e1ae8399c320a8adec76f6c919ed5ceae6edd4c3672f4d9eae2b27e37c80ff6"}, +] + [[package]] name = "aiohttp" -version = "3.9.5" +version = "3.10.0" requires_python = ">=3.8" summary = "Async http client/server framework (asyncio)" groups = ["default"] marker = "python_full_version == \"3.10.12\"" dependencies = [ + "aiohappyeyeballs>=2.3.0", "aiosignal>=1.1.2", "async-timeout<5.0,>=4.0; python_version < \"3.11\"", "attrs>=17.3.0", @@ -41,8 +54,8 @@ dependencies = [ "yarl<2.0,>=1.0", ] files = [ - {file = "aiohttp-3.9.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:c26959ca7b75ff768e2776d8055bf9582a6267e24556bb7f7bd29e677932be72"}, - {file = "aiohttp-3.9.5.tar.gz", hash = "sha256:edea7d15772ceeb29db4aff55e482d4bcfb6ae160ce144f2682de02f6d693551"}, + {file = "aiohttp-3.10.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:06ef0135d7ab7fb0284342fbbf8e8ddf73b7fee8ecc55f5c3a3d0a6b765e6d8b"}, + {file = "aiohttp-3.10.0.tar.gz", hash = "sha256:e8dd7da2609303e3574c95b0ec9f1fd49647ef29b94701a2862cceae76382e1d"}, ] [[package]] @@ -1094,7 +1107,7 @@ files = [ [[package]] name = "uvicorn" -version = "0.30.3" +version = "0.30.4" requires_python = ">=3.8" summary = "The lightning-fast ASGI server." groups = ["default"] @@ -1105,13 +1118,13 @@ dependencies = [ "typing-extensions>=4.0; python_version < \"3.11\"", ] files = [ - {file = "uvicorn-0.30.3-py3-none-any.whl", hash = "sha256:94a3608da0e530cea8f69683aa4126364ac18e3826b6630d1a65f4638aade503"}, - {file = "uvicorn-0.30.3.tar.gz", hash = "sha256:0d114d0831ff1adbf231d358cbf42f17333413042552a624ea6a9b4c33dcfd81"}, + {file = "uvicorn-0.30.4-py3-none-any.whl", hash = "sha256:06b00e3087e58c6865c284143c0c42f810b32ff4f265ab19d08c566f74a08728"}, + {file = "uvicorn-0.30.4.tar.gz", hash = "sha256:00db9a9e3711a5fa59866e2b02fac69d8dc70ce0814aaec9a66d1d9e5c832a30"}, ] [[package]] name = "uvicorn" -version = "0.30.3" +version = "0.30.4" extras = ["standard"] requires_python = ">=3.8" summary = "The lightning-fast ASGI server." @@ -1122,14 +1135,14 @@ dependencies = [ "httptools>=0.5.0", "python-dotenv>=0.13", "pyyaml>=5.1", - "uvicorn==0.30.3", + "uvicorn==0.30.4", "uvloop!=0.15.0,!=0.15.1,>=0.14.0; (sys_platform != \"cygwin\" and sys_platform != \"win32\") and platform_python_implementation != \"PyPy\"", "watchfiles>=0.13", "websockets>=10.4", ] files = [ - {file = "uvicorn-0.30.3-py3-none-any.whl", hash = "sha256:94a3608da0e530cea8f69683aa4126364ac18e3826b6630d1a65f4638aade503"}, - {file = "uvicorn-0.30.3.tar.gz", hash = "sha256:0d114d0831ff1adbf231d358cbf42f17333413042552a624ea6a9b4c33dcfd81"}, + {file = "uvicorn-0.30.4-py3-none-any.whl", hash = "sha256:06b00e3087e58c6865c284143c0c42f810b32ff4f265ab19d08c566f74a08728"}, + {file = "uvicorn-0.30.4.tar.gz", hash = "sha256:00db9a9e3711a5fa59866e2b02fac69d8dc70ce0814aaec9a66d1d9e5c832a30"}, ] [[package]] @@ -1345,7 +1358,7 @@ files = [ [[package]] name = "yt-dlp" -version = "2024.7.25" +version = "2024.8.1" requires_python = ">=3.8" summary = "A feature-rich command-line audio/video downloader" groups = ["default"] @@ -1361,6 +1374,6 @@ dependencies = [ "websockets>=12.0", ] files = [ - {file = "yt_dlp-2024.7.25-py3-none-any.whl", hash = "sha256:f44b5f33776b4f718900c670fe6e4698fb6fcd426455cd837cf25a1d6d4d9560"}, - {file = "yt_dlp-2024.7.25.tar.gz", hash = "sha256:7587aa25e236cf7b14bdb9378bbffff51202d901b04202be0cf62cbb56d3b52c"}, + {file = "yt_dlp-2024.8.1-py3-none-any.whl", hash = "sha256:d0d927038e30a05f6eab26ff6189628456ea21bb159a3d9dc2e855eef2810eac"}, + {file = "yt_dlp-2024.8.1.tar.gz", hash = "sha256:4318aa523694611562f01419c8d526b662a72df34ef8ba454016b34c8366c158"}, ] diff --git a/test/test_music_duration.py b/test/test_music_duration.py new file mode 100644 index 0000000..a58dbdb --- /dev/null +++ b/test/test_music_duration.py @@ -0,0 +1,32 @@ +import math + +from xiaomusic.const import ( + SUPPORT_MUSIC_TYPE, +) +from xiaomusic.utils import ( + get_local_music_duration, + traverse_music_directory, +) + + +async def test_one_music(filename): + # 获取播放时长 + duration = await get_local_music_duration(filename) + sec = math.ceil(duration) + print(f"本地歌曲 : {filename} 的时长 {duration} {sec} 秒") + + +async def main(directory): + # 获取所有歌曲文件 + local_musics = traverse_music_directory(directory, 10, [], SUPPORT_MUSIC_TYPE) + print(local_musics) + for _, files in local_musics.items(): + for file in files: + await test_one_music(file) + + +if __name__ == "__main__": + import asyncio + + directory = "./music" # 替换为你的音乐目录路径 + asyncio.run(main(directory)) diff --git a/xiaomusic/utils.py b/xiaomusic/utils.py index 551fa8a..e1810c7 100644 --- a/xiaomusic/utils.py +++ b/xiaomusic/utils.py @@ -141,9 +141,7 @@ def _append_files_result(result, root, joinpath, files, support_extension): result[dir_name].append(os.path.join(joinpath, file)) -def traverse_music_directory( - directory, depth=10, exclude_dirs=None, support_extension=None -): +def traverse_music_directory(directory, depth, exclude_dirs, support_extension): result = {} for root, dirs, files in os.walk(directory, followlinks=True): # 忽略排除的目录 @@ -247,10 +245,9 @@ async def get_local_music_duration(filename): m = await loop.run_in_executor(None, mutagen.mp3.MP3, filename) else: m = await loop.run_in_executor(None, mutagen.File, filename) - if m and m.info: - duration = m.info.length + duration = m.info.length except Exception as e: - logging.error(f"Error getting local music duration: {e}") + logging.error(f"Error getting local music {filename} duration: {e}") return duration