From aa4223b4137f7deb9de5f4954951f8c4bc3ec267 Mon Sep 17 00:00:00 2001 From: Koha9 <36852125+Koha9@users.noreply.github.com> Date: Fri, 20 Jun 2025 21:54:55 +0900 Subject: [PATCH 1/3] Refactor Plex interaction into separate module --- README.md | 4 ++++ app/config.json | 6 +++++- app/main.py | 30 +++++++++++++++++++++++++----- app/utils/plex_client.py | 16 ++++++++++++++++ requirements.txt | 1 + 5 files changed, 51 insertions(+), 6 deletions(-) create mode 100644 app/utils/plex_client.py diff --git a/README.md b/README.md index 3a198d6..2e6b20c 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,10 @@ PlexPlaylistSync 是一个用于同步 Plex 播放列表和本地 `.m3u`/`.m3u8` - **低功耗运行**: 适合 NAS 或服务器 24 小时运行,优化资源使用,减少能耗。 - **Docker 部署**: 可通过 Docker 轻松部署和运行。 +### Token 登录 + +首次登录时使用用户名和密码连接 Plex 服务器,成功后程序会将获得的 `token` 保存在配置文件中,后续通信仅使用该 `token`,不再保存明文密码。 + ## 安装 首先安装依赖: diff --git a/app/config.json b/app/config.json index 8047ddd..97f718a 100644 --- a/app/config.json +++ b/app/config.json @@ -1,3 +1,7 @@ { - "theme": "light" + "theme": "light", + "token": "", + "server_url": "", + "server_port": "", + "library": "" } \ No newline at end of file diff --git a/app/main.py b/app/main.py index 61f5edf..c0754f4 100644 --- a/app/main.py +++ b/app/main.py @@ -4,6 +4,7 @@ from fastapi import FastAPI, Request, Form from fastapi.responses import HTMLResponse, RedirectResponse from fastapi.templating import Jinja2Templates from fastapi.staticfiles import StaticFiles +from app.utils.plex_client import connect_plex app = FastAPI() templates = Jinja2Templates(directory=os.path.join(os.path.dirname(__file__), "templates")) @@ -36,11 +37,30 @@ async def login( ): config = load_config() theme = config.get("theme", "auto") - # demo:假装连接成功 - if user == "admin": - return templates.TemplateResponse("login.html", {"request": request, "message": "连接成功", "success": True, "theme": theme, "path": "/login"}) - else: - return templates.TemplateResponse("login.html", {"request": request, "message": "连接失败:用户名错误", "success": False, "theme": theme, "path": "/login"}) + try: + connect_plex(config, user, pw, url, port, library) + save_config(config) + return templates.TemplateResponse( + "login.html", + { + "request": request, + "message": "连接成功", + "success": True, + "theme": theme, + "path": "/login", + }, + ) + except Exception as e: + return templates.TemplateResponse( + "login.html", + { + "request": request, + "message": f"连接失败:{str(e)}", + "success": False, + "theme": theme, + "path": "/login", + }, + ) @app.get("/playlist", response_class=HTMLResponse) async def get_playlist(request: Request): diff --git a/app/utils/plex_client.py b/app/utils/plex_client.py new file mode 100644 index 0000000..a3fa1cb --- /dev/null +++ b/app/utils/plex_client.py @@ -0,0 +1,16 @@ +from plexapi.myplex import MyPlexAccount +from plexapi.server import PlexServer + + +def connect_plex(config, username, password, url, port, library): + """Return a connected PlexServer instance and update config with token and server info.""" + token = config.get("token") + if not token: + account = MyPlexAccount(username, password) + token = account.authenticationToken + config["token"] = token + + base_url = f"http://{url}:{port}" + server = PlexServer(base_url, token) + config.update({"server_url": url, "server_port": port, "library": library}) + return server diff --git a/requirements.txt b/requirements.txt index 24a1b62..8222198 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,4 @@ fastapi uvicorn[standard] jinja2 python-multipart +plexapi From 75a413667d5940ca207398130c14c9a87616e1a9 Mon Sep 17 00:00:00 2001 From: Koha9 <36852125+Koha9@users.noreply.github.com> Date: Fri, 20 Jun 2025 22:27:01 +0900 Subject: [PATCH 2/3] Set default Plex port --- README.md | 1 + app/config.json | 2 +- app/main.py | 2 +- app/templates/login.html | 2 +- app/utils/plex_client.py | 2 +- 5 files changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 2e6b20c..503ffaf 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ PlexPlaylistSync 是一个用于同步 Plex 播放列表和本地 `.m3u`/`.m3u8` ### Token 登录 首次登录时使用用户名和密码连接 Plex 服务器,成功后程序会将获得的 `token` 保存在配置文件中,后续通信仅使用该 `token`,不再保存明文密码。 +默认情况下 Plex 服务器使用 `32400` 端口,可在未修改服务器端口时直接使用该默认值。 ## 安装 diff --git a/app/config.json b/app/config.json index 97f718a..a2822f8 100644 --- a/app/config.json +++ b/app/config.json @@ -2,6 +2,6 @@ "theme": "light", "token": "", "server_url": "", - "server_port": "", + "server_port": "32400", "library": "" } \ No newline at end of file diff --git a/app/main.py b/app/main.py index c0754f4..3019a28 100644 --- a/app/main.py +++ b/app/main.py @@ -32,7 +32,7 @@ async def login( user: str = Form(...), pw: str = Form(...), url: str = Form(...), - port: str = Form(...), + port: str = Form("32400"), library: str = Form(...) ): config = load_config() diff --git a/app/templates/login.html b/app/templates/login.html index 171d23a..bf2738f 100644 --- a/app/templates/login.html +++ b/app/templates/login.html @@ -19,7 +19,7 @@