PlexPlaylistSync/app/main.py
2025-07-08 19:17:18 +09:00

132 lines
4.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
from app.utils.config import load_config, save_config, get_server_settings
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"))
# mount static files
# 这里的路径是相对于 main.py 文件所在的目录
app.mount("/static", StaticFiles(directory=os.path.join(os.path.dirname(__file__), "static")), name="static")
# 显示主页
@app.get("/", response_class=HTMLResponse)
async def home(request: Request):
config = load_config()
theme = config.get("theme", "auto")
token,scheme, host, port = get_server_settings(config)
return templates.TemplateResponse(
"login.html",
{
"request": request,
"theme": theme,
"path": "/login",
"scheme": scheme,
"token": token,
"server_url": host,
"port": port,
},
)
# 登录页面和处理
@app.get("/login", response_class=HTMLResponse)
async def login_page(request: Request):
config = load_config()
theme = config.get("theme", "auto")
token, scheme, host, port = get_server_settings(config)
return templates.TemplateResponse(
"login.html",
{
"request": request,
"theme": theme,
"path": "/login",
"token": token,
"scheme": scheme,
"server_url": host,
"port": port,
},
)
@app.post("/login", response_class=HTMLResponse)
async def login(
request: Request,
user: str = Form(...),
pw: str = Form(...),
token: str = Form(...),
scheme: str = Form("https"),
url: str = Form(...),
port: str = Form("32400"),
):
config = load_config()
theme = config.get("theme", "auto")
# 尝试连接到 Plex 服务器
try:
# 优先使用 token 连接,如果 token 为空则使用用户名和密码连接
_, token_success = connect_plex(user, pw, token, scheme, url, port)
# 成功连接后保存配置到配置文件
config.update({
"server_url": url,
"server_scheme": scheme,
"server_port": port,
"token": token_success,
})
save_config(config)
token, scheme, host, port = get_server_settings(config)
return templates.TemplateResponse(
"login.html",
{
"request": request,
"message": "连接成功",
"success": True,
"theme": theme,
"path": "/login",
"token": token,
"scheme": scheme,
"server_url": host,
"port": port,
},
)
except Exception as e:
return templates.TemplateResponse(
"login.html",
{
"request": request,
"message": f"连接失败:{str(e)}",
"success": False,
"theme": theme,
"path": "/login",
"scheme": scheme,
"server_url": url,
"port": port,
},
)
@app.get("/playlist", response_class=HTMLResponse)
async def get_playlist(request: Request):
config = load_config()
theme = config.get("theme", "auto")
return templates.TemplateResponse("playlist.html", {"request": request, "theme": theme, "path": "/playlist"})
@app.post("/playlist", response_class=HTMLResponse)
async def set_playlist(request: Request, address: str = Form(...), interval: str = Form(...)):
config = load_config()
theme = config.get("theme", "auto")
# demo返回提交的设置
return templates.TemplateResponse("playlist.html", {
"request": request,
"message": f"设置成功:地址 {address},间隔 {interval} 分钟",
"theme": theme,
"path": "/playlist"
})
@app.post("/set-theme")
async def set_theme(theme: str = Form(...)):
config = load_config()
config["theme"] = theme
save_config(config)
return RedirectResponse("/login", status_code=303)