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 @@