-
Notifications
You must be signed in to change notification settings - Fork 0
/
main old.py
209 lines (156 loc) · 6.76 KB
/
main old.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
import flet as ft
import yt_dlp
from yt_dlp.utils import download_range_func
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
'''
print(int(round((int(d['downloaded_bytes']) / int(d['total_bytes'])) * 100, 0)), end="\r")
print((int(d['downloaded_bytes']) / int(d['total_bytes'])))
print(round((int(d['downloaded_bytes']) / int(d['total_bytes'])), 1))
'''
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
print(int(round((int(d['fragment_index']) / int(d['fragment_count'])) * 100, 2)), end="\r")
downloadSheet.downloadProgressBar.value = round((int(d['fragment_index']) / int(d['fragment_count'])), 5)
downloadSheet.downloadProgressBar.update()
if 'total_bytes_estimate' in d:
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()
#-----------
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)),
]
),
]
)
)
)
#--------
def main(page: ft.Page):
def downLoadButtonClicked(e):
try:
downloadSheet.resetBs()
except AssertionError:
pass
# This happens when the thing isn't open
try:
page.open(downloadSheet.bs)
page.update()
ydl_opts = {
'logger': dlLogger(),
'progress_hooks': [dlHook]
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info_dict = ydl.extract_info(urlEntry.value, download=False)
videoTitle = info_dict.get('title', None)
videoChannel = info_dict.get('channel', None)
videoTumbnail = info_dict.get('thumbnail', None)
print(videoTitle)
print(videoChannel)
print(videoTumbnail)
ydl.download(urlEntry.value)
except Exception as e:
downloadSheet.resetBs()
if ("not a valid URL" in str(e)):
#page.snack_bar = ft.SnackBar(ft.Text(f"Error: {e}"))
page.snack_bar = ft.SnackBar("Your URL appears to invalid")
page.snack_bar.open = True
page.update()
else:
print(e)
errorMsgDialog.content=ft.Text(e, selectable=True)
page.open(errorMsgDialog)
#--------------
def closeDialog(e):
page.close(errorMsgDialog)
errorMsgDialog = ft.AlertDialog(
modal=True,
title=ft.Text("Error:"),
content=ft.Text("Details"),
actions=[
ft.TextButton("Close", on_click=closeDialog),
],
actions_alignment=ft.MainAxisAlignment.END,
)
#==============================================
# Here is the actuall main page
urlEntry = ft.TextField(label="YouTube URL", adaptive=adaptiveOnOff)
page.add(
ft.Container(content=ft.Text("Downcide", size=32), padding=8),
ft.Container(content=urlEntry, padding=8),
ft.Container(
content=ft.FilledButton(
"Download",
icon="download",
adaptive=adaptiveOnOff,
on_click=downLoadButtonClicked
),
alignment=ft.alignment.center,
padding=6,
)
)
ft.app(main)