支持手动token认证
This commit is contained in:
+37
-12
@@ -1,17 +1,15 @@
|
||||
from plexapi.myplex import MyPlexAccount
|
||||
from plexapi.server import PlexServer
|
||||
from urllib.parse import urlparse
|
||||
from app.utils.common import str_is_empty
|
||||
|
||||
|
||||
def connect_plex(config, username, password, url, port="32400"):
|
||||
"""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
|
||||
|
||||
parsed = urlparse(url)
|
||||
def build_plex_url(scheme, url, port="32400"):
|
||||
"""Build a full Plex URL from scheme, url, and port."""
|
||||
# 如果url不以http://或https://开头,则添加scheme
|
||||
full_url = url
|
||||
if not full_url.startswith("http://") and not full_url.startswith("https://"):
|
||||
full_url = f"{scheme}://{url}"
|
||||
parsed = urlparse(full_url)
|
||||
|
||||
if parsed.scheme in ("http", "https"):
|
||||
netloc = parsed.netloc or parsed.path
|
||||
@@ -20,6 +18,33 @@ def connect_plex(config, username, password, url, port="32400"):
|
||||
base_url = f"{parsed.scheme}://{netloc}"
|
||||
else:
|
||||
base_url = f"http://{url}:{port}"
|
||||
return base_url
|
||||
|
||||
def connect_plex(username, password, token, scheme, url, port="32400"):
|
||||
"""Return a connected PlexServer instance and update config with token and server info."""
|
||||
# 如果token存在且不为空,则使用token连接
|
||||
if not str_is_empty(token):
|
||||
return connect_plex_with_token(token, scheme, url, port)
|
||||
else:
|
||||
return connect_plex_with_pw(username, password, scheme, url, port)
|
||||
|
||||
|
||||
def connect_plex_with_pw(username, password, scheme, url, port="32400"):
|
||||
"""Return a connected PlexServer instance and update config with token and server info."""
|
||||
# url 初始化
|
||||
base_url = build_plex_url(scheme, url, port)
|
||||
# account 初始化
|
||||
account = MyPlexAccount(username, password)
|
||||
# token 获取
|
||||
token = account.authenticationToken
|
||||
|
||||
server = PlexServer(base_url, token)
|
||||
config.update({"server_url": url, "server_port": port})
|
||||
return server
|
||||
return server, token
|
||||
|
||||
def connect_plex_with_token(token, scheme, url, port="32400"):
|
||||
"""Return a connected PlexServer instance using a token."""
|
||||
# URL 初始化
|
||||
base_url = build_plex_url(scheme, url, port)
|
||||
|
||||
server = PlexServer(base_url, token)
|
||||
return server, token
|
||||
Reference in New Issue
Block a user