-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
__main__.py
329 lines (276 loc) · 11.6 KB
/
__main__.py
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
from datetime import timedelta
import asyncio
import humanize
from fastapi import Request
from fastapi.responses import RedirectResponse, FileResponse
from nicegui import ui, app
from plexapi.collection import *
from plexapi.video import *
from plexapi.media import MediaPart, Media
from config import config
from login import check_login, logout
from plex import get_server, get_self
from locales import _
from common import io_bound
def apartial(func, *args, **kwargs):
async def handler():
return await func(*args, **kwargs)
return handler
@app.middleware("http")
async def check_auth(request: Request, call_next):
# https://github.com/zauberzeug/nicegui/blob/main/examples/authentication/main.py
if not await check_login():
if not request.url.path.startswith("/_nicegui") and request.url.path != "/login":
app.storage.user['referrer_path'] = request.url.path
return RedirectResponse('/login')
return await call_next(request)
# todo: find a way
# class CustomGZipMiddleware(GZipMiddleware):
# def __init__(
# self, *args, **kwargs
# ) -> None:
# super().__init__(*args, **kwargs)
# async def __call__(self, scope, receive, send) -> None:
# if scope["type"] == "http":
# headers = Headers(scope=scope)
# if "gzip" in headers.get("Accept-Encoding", ""):
# responder = GZipResponder(
# self.app, self.minimum_size, compresslevel=self.compresslevel
# )
# await responder(scope, receive, send)
# return
# await self.app(scope, receive, send)
del app.user_middleware[-1] # gzip removes content-length
async def header():
"""
Displays thecommon page header
"""
user = await get_self()
def logout_handler():
logout()
ui.open("/login")
with ui.row():
ui.button(_("home"), on_click=lambda: ui.open("/"))
ui.button(_("logout"), on_click=logout_handler)
ui.label(_("user", user=user.email))
@app.get("/download/{media}/{index}")
async def download(media: int, index: int):
"""
Downloads the specified media part from Plex
"""
part = (await get_server()).fetchItem(media).media[index].parts[0] # is there ever more than one part per media?
filename = os.path.basename(part.file)
return FileResponse(part.file, filename=filename, stat_result=os.stat(part.file))
@ui.page("/", title="PlexDLWeb")
async def index():
ui.add_head_html("""
<link rel="manifest" href="/plexdlweb.webmanifest">
<style>
/* for search results */
.cursor-pointer-rec, .cursor-pointer-rec * {
cursor: pointer;
}
</style>
""")
ui.add_body_html("""
<script>
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/sw.js');
}
</script>
""")
def fake_button_group():
return ui.element("div").classes(add="q-btn-group row flex-nowrap")
def fake_button(text):
with ui.element("div").classes(add="q-btn q-btn-item no-outline") as e:
with ui.element("span").classes(add="text-left items-center justify-center col row"):
ui.html(text)
return e
def fake_button_label(text):
return ui.label(text).classes(add="m-3 q-btn q-btn--flat p-0").style("min-height: 0")
def duration_to_string(ms: int):
dur = timedelta(milliseconds=ms)
if dur > timedelta(hours=1):
return f"{dur.seconds // 3600}h{(dur.seconds // 60) % 60}m"
elif dur > timedelta(minutes=5):
return f"{dur.seconds // 60}m"
else:
return f"{dur.seconds // 60}m{dur.seconds % 60}s"
def merge(query, new):
"""
Merge the previous query history with the new one
Example: merge([A,B], [C,D]) == [A,B,C,D]
Handles overlaps: merge([A,B,C], [B,D,E]) == [A,B,D,E]
"""
try:
idx = query.index(new[0])
return query[:idx] + new
except ValueError:
return [*query, *new]
@ui.refreshable
def result_list(query, results):
def refresh(query2, results2):
# don't refresh if the parameters haven't changed
if (query2, results2) == (query, results):
return
result_list.refresh(query2, results2)
if not query:
return
if not results:
ui.label(_("no_results"))
return
kinds = {
Movie: (
_("movie"),
"bg-green-300",
lambda m: (f"<span style='font-size: 70%'>{m.editionTitle}</span><br>" if m.editionTitle else "") + m.title,
lambda m: print(list(m.iterParts()))
),
Show: (
_("show"),
"bg-yellow-300",
lambda s: s.title,
lambda s: refresh([*query, s], s.seasons())
),
Season: (
_("season"),
"bg-red-300",
lambda s: f"{s.parentTitle} - {s.title}",
lambda s: refresh([*query, s], s.episodes())
),
Episode: (
_("episode"),
"bg-teal-300",
lambda
e: f"<span style='font-size: 70%'>{e.grandparentTitle} - {e.parentTitle} - {_('episode')} {e.index}</span><br>{e.title}",
lambda e: refresh(merge(query, [e.show(), sea := e.season()]), sea.episodes())
),
Collection: (
_("collection"),
"bg-violet-300",
lambda c: c.title,
lambda c: refresh([*query, c], c.items())
),
str: (
_("search_noun"),
"bg-blue-300",
lambda s: s,
lambda s: do_search(s, True)
)
}
result_as_list = app.storage.user.get("result_as_list", False)
with ui.row():
def format_change(e):
app.storage.user['result_as_list'] = e.value
result_list.refresh(query, results)
ui.label(_("display"))
ui.toggle({False: _("grid"), True: _("list")}, value=result_as_list, on_change=format_change)
with ui.row():
ui.label(_("history"))
def display_crumb(i, part):
kind, color, namer, clicked = kinds[type(part)]
async def handler():
del query[i:]
await clicked(part)
with fake_button_group().on("click", handler).classes(add="cursor-pointer-rec"):
fake_button(kind).classes(add=color)
fake_button(namer(part))
for i, part in enumerate(query):
display_crumb(i, part)
with ui.column() if result_as_list else ui.grid(columns=3):
def display_result(r):
opts = kinds.get(type(r), None)
if opts is None:
return
kind, color, namer, clicked = opts
def dl_button():
if isinstance(r, Playable):
if len(r.media) > 1:
def handler():
def part_line(i, media: Media):
part = media.parts[0]
fake_button_label(duration_to_string(part.duration)).style("text-transform: none").classes(add="text-right")
fake_button_label(f"{media.width}x{media.height}").style("text-transform: none")
fake_button_label(humanize.naturalsize(part.size)).classes(add="text-right")
ui.button(icon="download").props("flat").on("click.stop", lambda: ui.download(f"/download/{r.ratingKey}/{i}")).classes(add="px-3")
with ui.dialog() as dialog, ui.card():
with ui.grid(columns=4):
for i, media in enumerate(r.media):
part_line(i, media)
dialog.open()
else:
def handler():
ui.download(f"/download/{r.ratingKey}/0")
part = r.media[0].parts[0]
fake_button_label(duration_to_string(part.duration)).classes(add="ml-auto self-center").style(
"text-transform: none; font-size: 90%")
fake_button_label(humanize.naturalsize(part.size)).classes(add="mx-0 self-center").style(
"font-size: 90%")
ui.button(icon="download").props("flat").on("click.stop", handler).classes(add="px-3")
if result_as_list:
with fake_button_group().on("click", lambda: clicked(r)).classes(add="w-full cursor-pointer-rec"):
fake_button(kind).classes(add=color)
fake_button(namer(r))
dl_button()
else:
with ui.card().tight().on("click", lambda: clicked(r)).classes(add="cursor-pointer-rec"):
with ui.card_section().classes(add=color).classes(add="p-0 row"):
fake_button_label(kind)
dl_button()
ui.image("/plex" + r.thumb)
with ui.card_section():
ui.html("<span style='font-size: 120%'>" + namer(r) + "</span>")
for r in results:
display_result(r)
last_search = None
server = await get_server()
async def do_search(query, force=False):
nonlocal last_search
previous, last_search = last_search, query
if not force and query == previous or len(query) < 3:
return
loading.set_visibility(True)
results = await io_bound(server.search, query)
async def all_editions(x):
if isinstance(x, Movie):
return [x, *(await io_bound(x.editions))]
return [x]
result_list.refresh([query], [item for editions in (await asyncio.gather(*[all_editions(res) for res in results])) for item in editions])
loading.set_visibility(False)
debounce = None
def name_change(e):
nonlocal debounce
if debounce is not None:
debounce.deactivate()
debounce = ui.timer(0.3, apartial(do_search, e.value), once=True)
await header()
with ui.column():
ui.input(label=_("search_verb"), placeholder=_("search_placeholder"), on_change=name_change)
loading = ui.spinner(size="lg")
loading.set_visibility(False)
result_list([], [])
@app.get("/plexdlweb.webmanifest")
def manifest():
return {
"name": "PlexDLWeb",
"short_name": "Volume",
"start_url": "/",
"display": "standalone",
"background_color": "#282a2d",
"theme_color": "#000",
"icons": [
{
"src": "https://app.plex.tv/desktop/static/[email protected]",
"sizes": "512x512",
"type": "image/png"
}
]
}
@app.get("/sw.js")
def sw():
return """
self.addEventListener('install', function(event) {});
self.addEventListener('fetch', function(event) {});
self.addEventListener('activate', function(event) {});
"""
ui.run(host=config.host, port=config.port, show=False, storage_secret=config.secret)