-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
340 lines (258 loc) · 11.3 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
330
331
332
333
334
335
336
337
338
339
340
import flet as ft
import yt_dlp
from yt_dlp.utils import download_range_func
import datetime
adaptiveOnOff = False
class dlLogger:
def debug(self, msg):
# For compatibility with youtube-dl, both debug and info are passed into debug
# You can distinguish them by the prefix '[debug] '
if msg.startswith('[debug] '):
pass
else:
self.info(msg)
def info(self, msg):
#print(msg)
pass
def warning(self, msg):
pass
def error(self, msg):
print(msg)
def dlHook(d):
try:
if d['status'] == 'downloading' and 'total_bytes' in d:
#print for debug
downloadSheet.downloadProgressBar.value = round((int(d['downloaded_bytes']) / int(d['total_bytes'])), 5)
downloadSheet.downloadProgressBar.update()
downloadSheet.downloadProgressLabel.value = (str(round(int(d['downloaded_bytes'])/1000000, 1)) + " MB / " + str(round(int(d['total_bytes'])/1000000, 1)) + " MB")
downloadSheet.downloadProgressLabel.update()
elif d['status'] == 'downloading' and 'fragment_count' in d:
#print for debug
downloadSheet.downloadProgressBar.value = round((int(d['fragment_index']) / int(d['fragment_count'])), 5)
downloadSheet.downloadProgressBar.update()
if 'total_bytes_estimate' in d:
# Explanation:
# total bytes * (fragment index / fragment count)
dowloadedMegabytes = round(int(d['total_bytes_estimate']) * ((int(d['fragment_index']) / int(d['fragment_count']))) / 1000000, 1)
totalMegabytes = round(int(d['total_bytes_estimate']) / 1000000, 1)
downloadSheet.downloadProgressLabel.value = f"Approx. {dowloadedMegabytes}MB / {totalMegabytes}MB "
downloadSheet.downloadProgressLabel.update()
else:
downloadSheet.downloadProgressLabel.value = f"Approx. {dowloadedMegabytes}MB / {totalMegabytes}M B"
downloadSheet.downloadProgressLabel.update()
except Exception as e:
raise Exception(e)
if d['status'] == 'finished':
print('Done downloading, now post-processing ...')
downloadSheet.title.value = "Post-rocessing"
downloadSheet.doneButton.disabled = False
downloadSheet.bs.update()
def downloadVideo():
url = mainPageControls.urlEntry.value
mainPageControls.urlEntry.value = ""
mainPageControls.urlEntry.update()
confimationSheet.bs.open = False
confimationSheet.bs.update()
downloadSheet.bs.open = True
downloadSheet.bs.update()
ydl_opts = {
'logger': dlLogger(),
'progress_hooks': [dlHook]
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info_dict = ydl.extract_info(url, download=False)
ydl.download(url)
#-----------
class downloadSheet:
def resetBs():
downloadSheet.title.value = "Downloading"
downloadSheet.downloadProgressBar.value = None
downloadSheet.downloadProgressLabel.value = "Preparing..."
downloadSheet.doneButton.disabled = True
downloadSheet.bs.open = False
downloadSheet.bs.update()
def closeBs():
downloadSheet.bs.open = False
downloadSheet.bs.update()
title = ft.Text("Downloading", size=24)
downloadProgressBar = ft.ProgressBar(width=400)
downloadProgressLabel = ft.Text("Preparing...")
doneButton = ft.TextButton("Done", disabled=True, on_click=lambda _: downloadSheet.closeBs())
bs = ft.BottomSheet(
dismissible=False,
content=ft.Container(
padding=50,
content=ft.Column(
tight=True,
controls=[
title,
downloadProgressBar,
downloadProgressLabel,
ft.Row(
alignment=ft.MainAxisAlignment.END,
tight=False,
width=400,
controls=[
doneButton
#ft.TextButton("Done", on_click=lambda _: page.close(bs)),
]
),
]
)
)
)
#--------
class confimationSheet:
def openBs():
confimationSheet.bs.open = True
ydl_opts = {
'logger': dlLogger(),
'progress_hooks': [dlHook]
}
mainPageControls.spinner.visible = True
mainPageControls.spinner.update()
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info_dict = ydl.extract_info(mainPageControls.urlEntry.value, download=False)
videoTumbnail = info_dict.get('thumbnail', None)
videoTitle = info_dict.get('title', None)
videoChannel = info_dict.get('channel', None)
videoDuration = info_dict.get('duration', None)
videoDurationMinutes = str(datetime.timedelta(seconds=int(videoDuration)))
# Lazy logic to remove the begining zeroes if the video is shorter than 10 minutes or
# 1 hour respectively
if videoDurationMinutes[0:3] == "0:0":
videoDurationMinutes = videoDurationMinutes[3:]
elif videoDurationMinutes[0:2] == "0:":
videoDurationMinutes = videoDurationMinutes[2:]
confimationSheet.Thumbnail.src = videoTumbnail
confimationSheet.videoTitleText.value = videoTitle
confimationSheet.videoTitleText.tooltip = videoTitle
confimationSheet.videoChannelText.value = videoChannel
confimationSheet.videoDurationText.value = videoDurationMinutes
mainPageControls.spinner.visible = False
mainPageControls.spinner.update()
confimationSheet.bs.update()
def closeBs():
confimationSheet.bs.open = False
confimationSheet.bs.update()
Thumbnail = ft.Image(
# gets the youtube thumbnail,
src="./generic_thumbnail.png",
height=120,
fit=ft.ImageFit.CONTAIN,
repeat=ft.ImageRepeat.NO_REPEAT,
border_radius=ft.border_radius.all(10),
)
videoTitleText = ft.Text("Title", size=24, width=340, max_lines=2, overflow=ft.TextOverflow.ELLIPSIS)
videoChannelText = ft.Text("Title", size=16, width=400, max_lines=1)
videoDurationText = ft.Text("Unknown")
cancelButton = ft.OutlinedButton("Cancel", on_click=lambda _: confimationSheet.closeBs())
downloadButton = ft.FilledButton("Download", icon="download", on_click=lambda _: downloadVideo()) #download video
bs = ft.BottomSheet(
dismissible=True,
enable_drag=True,
show_drag_handle=False,
content=ft.Container(
padding=50,
content=ft.Column(
tight=True,
spacing=38,
controls=[
ft.Row(spacing=18,
controls=[
ft.Stack(controls=[
Thumbnail,
ft.Container(margin=8, padding=2, border_radius=4, alignment=ft.alignment.bottom_right, right=0, bottom=0, bgcolor="#cc000000",
content=videoDurationText,
),
]),
ft.Column(
controls=[
videoTitleText,
videoChannelText,
]
),
]
),
ft.Row(
alignment=ft.MainAxisAlignment.END,
tight=False,
controls=[
cancelButton,
downloadButton
]
),
]
)
)
)
#----------------------
class mainPageControls:
def resetUrlEntry(e):
mainPageControls.urlEntry.error_text = None
mainPageControls.urlEntry.update()
# when the download button is clicked or if the user presses enter while in the URL entry
def downlaodAction(e):
if "youtube.com" in mainPageControls.urlEntry.value:
try:
downloadSheet.resetBs()
except AssertionError:
pass
# This happens when the thing isn't open
try:
confimationSheet.openBs()
except Exception as e:
mainPageControls.errorMsgDialog.content=ft.Text(e, selectable=True)
mainPageControls.errorMsgDialog.open = True
mainPageControls.errorMsgDialog.update()
mainPageControls.spinner.visible = False
mainPageControls.spinner.update()
else:
mainPageControls.urlEntry.error_text = "That is not a YouTube™ URL u stoopid"
mainPageControls.urlEntry.update()
def closeErrorMsgDialog(e):
mainPageControls.errorMsgDialog.open = False
mainPageControls.errorMsgDialog.update()
errorMsgDialog = ft.AlertDialog(
modal=True,
title=ft.Text("Error:"),
content=ft.Text("Details"),
actions=[
ft.TextButton("Close", on_click=closeErrorMsgDialog),
],
actions_alignment=ft.MainAxisAlignment.END,
)
spinner = ft.ProgressRing(width=20, height=20, visible=False)
urlEntry = ft.TextField(label="YouTube URL", adaptive=adaptiveOnOff, on_change=resetUrlEntry, on_submit=downlaodAction) # todo: on submit
def main(page: ft.Page):
#-----------------
# I have to add this is the page for whatever reason
page.add(downloadSheet.bs)
page.add(confimationSheet.bs)
page.add(mainPageControls.errorMsgDialog)
#==============================================
# Here is the actuall main page
page.add(
ft.Container(content=ft.Text("Downcide", size=32), padding=8),
ft.Container(content=mainPageControls.urlEntry, padding=8),
ft.Container(
alignment=ft.alignment.center,
padding=6,
content=ft.Row(
tight=True,
controls=[
ft.FilledButton(
"Download",
icon="download",
adaptive=adaptiveOnOff,
on_click=mainPageControls.downlaodAction
),
mainPageControls.spinner
]
)
)
)
ft.app(
main,
assets_dir="icons"
)