-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathplex.py
More file actions
79 lines (61 loc) · 2.33 KB
/
Copy pathplex.py
File metadata and controls
79 lines (61 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
from nicegui import app
from plexapi.server import PlexServer
from plexapi.myplex import MyPlexAccount
from fastapi.responses import StreamingResponse
from starlette.background import BackgroundTask
import httpx
from config import config
from common import io_bound
def _clean_token(token: str | None, name: str) -> str:
if not isinstance(token, str) or not token.strip():
raise ValueError(f"{name} is missing")
return token.strip()
@app.on_startup
async def startup():
app.state.httpx_client = httpx.AsyncClient()
@app.on_shutdown
async def shutdown():
await app.state.httpx_client.aclose()
@app.get("/plex/{path:path}")
async def streaming(path: str) -> StreamingResponse:
"""
Forwards a request to the Plex server
"""
# https://stackoverflow.com/a/74556972/2196124
client = app.state.httpx_client
url = httpx.URL(config.server_url + "/" + path)
req = httpx.Request("GET", url, headers={"X-Plex-Token": app.storage.user.get("server_token")})
resp = await client.send(req, stream=True)
return StreamingResponse(
resp.aiter_raw(),
status_code=resp.status_code,
headers=resp.headers,
background=BackgroundTask(resp.aclose)
)
async def get_server():
token = _clean_token(app.storage.user.get("server_token"), "server token")
return await io_bound(PlexServer, config.server_url, token)
async def get_self():
token = _clean_token(app.storage.user.get("user_token"), "user token")
return await io_bound(MyPlexAccount, token=token)
async def get_server_token(user_token: str) -> str:
user_token = _clean_token(user_token, "user token")
acc = await io_bound(MyPlexAccount, token=user_token)
srv = [r for r in await io_bound(acc.resources) if r.clientIdentifier == config.server_id][0]
return _clean_token(srv.accessToken, "server token")
async def check_user_token(token: str) -> bool:
try:
token = _clean_token(token, "user token")
await io_bound(MyPlexAccount, token=token)
return True
except Exception as e:
print(e)
return False
async def check_server_token(token: str) -> bool:
try:
token = _clean_token(token, "server token")
await io_bound(PlexServer, config.server_url, token=token)
return True
except Exception as e:
print(e)
return False