diff --git a/pyproject.toml b/pyproject.toml index 9a4dd58..12ad511 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -61,7 +61,13 @@ include = ["**/*.py", "**/*.pyi", "**/pyproject.toml"] convention = "google" [tool.ruff.lint.flake8-bugbear] -extend-immutable-calls = ["fastapi.Depends", "fastapi.params.Depends", "fastapi.Query", "fastapi.params.Query"] +extend-immutable-calls = [ + "fastapi.Depends", + "fastapi.params.Depends", + "fastapi.Query", + "fastapi.params.Query", + "fastapi.File" +] [tool.pdm.scripts] lint = "ruff check ." diff --git a/xiaomusic/config.py b/xiaomusic/config.py index 7a846ce..0ae8c8a 100644 --- a/xiaomusic/config.py +++ b/xiaomusic/config.py @@ -155,6 +155,9 @@ class Config: ) pull_ask_sec: int = int(os.getenv("XIAOMUSIC_PULL_ASK_SEC", "1")) crontab_json: str = os.getenv("XIAOMUSIC_CRONTAB_JSON", "") # 定时任务 + enable_yt_dlp_cookies: bool = ( + os.getenv("XIAOMUSIC_ENABLE_YT_DLP_COOKIES", "false").lower() == "true" + ) def append_keyword(self, keys, action): for key in keys.split(","): @@ -262,3 +265,10 @@ class Config: if not os.path.exists(cache_path): os.makedirs(cache_path) return cache_path + + @property + def yt_dlp_cookies_path(self): + if not os.path.exists(self.conf_path): + os.makedirs(self.conf_path) + cookies_path = os.path.join(self.conf_path, "yt-dlp-cookie.txt") + return cookies_path diff --git a/xiaomusic/httpserver.py b/xiaomusic/httpserver.py index edafc18..0ae24d2 100644 --- a/xiaomusic/httpserver.py +++ b/xiaomusic/httpserver.py @@ -13,7 +13,16 @@ from dataclasses import asdict from typing import Annotated import aiofiles -from fastapi import Depends, FastAPI, HTTPException, Query, Request, status +from fastapi import ( + Depends, + FastAPI, + File, + HTTPException, + Query, + Request, + UploadFile, + status, +) from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import StreamingResponse from fastapi.security import HTTPBasic, HTTPBasicCredentials @@ -389,15 +398,9 @@ async def downloadplaylist(data: DownloadPlayList, Verifcation=Depends(verificat log.info(f"Download completed with exit code {exit_code}") dir_path = os.path.join(config.download_path, data.dirname) - log.debug(f"Download dir_path: {exit_code}") - - if exit_code == 0: - log.info("Download was successful.") - # 执行成功的后续逻辑:文件名处理 - remove_common_prefix(dir_path) - else: - # 处理失败的情况 - log.error("Download failed.") + log.debug(f"Download dir_path: {dir_path}") + # 可能只是部分失败,都需要整理下载目录 + remove_common_prefix(dir_path) asyncio.create_task(check_download_proc()) return {"ret": "OK"} @@ -424,6 +427,18 @@ async def downloadonemusic(data: DownloadOneMusic, Verifcation=Depends(verificat return {"ret": "Failed download"} +# 上传 yt-dlp cookies +@app.post("/uploadytdlpcookie") +async def upload_yt_dlp_cookie(file: UploadFile = File(...)): + with open(config.yt_dlp_cookies_path, "wb") as buffer: + shutil.copyfileobj(file.file, buffer) + return { + "ret": "OK", + "filename": file.filename, + "file_location": config.yt_dlp_cookies_path, + } + + async def file_iterator(file_path, start, end): async with aiofiles.open(file_path, mode="rb") as file: await file.seek(start) diff --git a/xiaomusic/static/default/debug.html b/xiaomusic/static/default/debug.html index cb02326..4cdf95e 100644 --- a/xiaomusic/static/default/debug.html +++ b/xiaomusic/static/default/debug.html @@ -6,9 +6,9 @@ Debug For XiaoMusic - + - + diff --git a/xiaomusic/static/default/downloadtool.html b/xiaomusic/static/default/downloadtool.html index 022b2db..86fff2c 100644 --- a/xiaomusic/static/default/downloadtool.html +++ b/xiaomusic/static/default/downloadtool.html @@ -4,8 +4,8 @@ 歌曲下载工具 - - + + diff --git a/xiaomusic/static/default/index.html b/xiaomusic/static/default/index.html index 7fbd8a7..6f49551 100644 --- a/xiaomusic/static/default/index.html +++ b/xiaomusic/static/default/index.html @@ -4,9 +4,9 @@ 小爱音箱操控面板 - - - + + + diff --git a/xiaomusic/static/default/m3u.html b/xiaomusic/static/default/m3u.html index b45d125..19480aa 100644 --- a/xiaomusic/static/default/m3u.html +++ b/xiaomusic/static/default/m3u.html @@ -5,7 +5,7 @@ M3U to JSON Converter - + diff --git a/xiaomusic/static/default/setting.html b/xiaomusic/static/default/setting.html index c067499..6e0076a 100644 --- a/xiaomusic/static/default/setting.html +++ b/xiaomusic/static/default/setting.html @@ -4,9 +4,9 @@ 小爱音箱操控面板 - - - + + + @@ -177,6 +177,12 @@ var vConsole = new window.VConsole(); + + + @@ -198,6 +204,13 @@ var vConsole = new window.VConsole(); 下载日志文件
+
+ + + +
+
+ m3u文件转换 歌曲下载工具 diff --git a/xiaomusic/static/default/setting.js b/xiaomusic/static/default/setting.js index 2caafc0..311b5fd 100644 --- a/xiaomusic/static/default/setting.js +++ b/xiaomusic/static/default/setting.js @@ -154,4 +154,31 @@ $(function(){ }); }); + $("#upload_yt_dlp_cookie").on("click", () => { + var fileInput = document.getElementById('yt_dlp_cookies_file'); + var file = fileInput.files[0]; // 获取文件对象 + if (file) { + var formData = new FormData(); + formData.append("file", file); + $.ajax({ + url: "/uploadytdlpcookie", + type: "POST", + data: formData, + processData: false, + contentType: false, + success: function(res) { + console.log(res); + alert("上传成功"); + }, + error: function(jqXHR, textStatus, errorThrown) { + console.log(res); + alert("上传失败"); + } + }); + } else { + alert("请选择一个文件"); + } + }); + + }); diff --git a/xiaomusic/utils.py b/xiaomusic/utils.py index 64b6113..3861404 100644 --- a/xiaomusic/utils.py +++ b/xiaomusic/utils.py @@ -696,6 +696,9 @@ async def download_playlist(config, url, dirname): if config.proxy: sbp_args += ("--proxy", f"{config.proxy}") + if config.enable_yt_dlp_cookies: + sbp_args += ("--cookies", f"{config.yt_dlp_cookies_path}") + sbp_args += (url,) cmd = " ".join(sbp_args) @@ -726,6 +729,9 @@ async def download_one_music(config, url, name=""): if config.proxy: sbp_args += ("--proxy", f"{config.proxy}") + if config.enable_yt_dlp_cookies: + sbp_args += ("--cookies", f"{config.yt_dlp_cookies_path}") + sbp_args += (url,) cmd = " ".join(sbp_args) diff --git a/xiaomusic/xiaomusic.py b/xiaomusic/xiaomusic.py index eaa3867..9d16369 100644 --- a/xiaomusic/xiaomusic.py +++ b/xiaomusic/xiaomusic.py @@ -1462,6 +1462,9 @@ class XiaoMusicDevice: if self.config.proxy: sbp_args += ("--proxy", f"{self.config.proxy}") + if self.config.yt_dlp_cookies: + sbp_args += ("--cookies", f"{self.config.yt_dlp_cookies}") + cmd = " ".join(sbp_args) self.log.info(f"download cmd: {cmd}") self._download_proc = await asyncio.create_subprocess_exec(*sbp_args)