-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpytube_fetch.py
More file actions
227 lines (186 loc) · 5.52 KB
/
pytube_fetch.py
File metadata and controls
227 lines (186 loc) · 5.52 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
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
from pytube import YouTube
import httpx
import time
import os
import aiofiles
import asyncio
import functools
down_ses = httpx.AsyncClient(timeout=77 * 60, verify=False)
def debug(text, filename: str = ""):
with open(f"debug{filename}.txt", "w", encoding="utf8")as f:
f.write(str(text))
def get_data_of_video(link):
yt = YouTube(link)
streams = yt.streams
title = yt.title
thumb = yt.thumbnail_url
reses = tuple(i.resolution for i in streams.filter(progressive=True))
audio = streams.get_audio_only()
return {
"title": title,
"thumbnail": thumb,
"resolutions": reses,
"video_id": get_the_video_id(link)
}
# def find_download_link(data:tuple or list):
# try:
# link, quality = data
# except:
# link = data[0]
# quality = None
#
# yt = YouTube(link)
# streams = yt.streams
# title = yt.title
# thumb = yt.thumbnail_url
#
# if quality:
# video = streams.filter(progressive=True, resolution=quality)[0]
# else:
# video = streams.filter(progressive=True)[-1]
#
# return {
# "url": video.url,
# "title": title,
# "thumbnail": thumb,
# "format": "mp4",
# "video":video
# }
temp = {}
def on_progress(
vid,
chunk,
bytes_remaining,
start,
message=None,
loop=None,
stat=None):
total = vid.filesize
dnd = total - bytes_remaining
pct = dnd / total * 100
x = 1024**2
if stat:
current = f"{stat[0]} out of {stat[1]}"
else:
current = ""
a = f"""
<b>Downloaded {pct:.2f} %
{dnd/x:.2f} MB of {total/x:.2f} MB @ {dnd/x/(time.time()-start):.2f} MB/s
{current}
</b>
""".strip()
if not message:
print(a)
else:
try:
if message:
if time.time() - temp[message.id] >= 2:
loop.run_until_complete(message.edit_text(a))
temp[message.id] = time.time()
print(time.time())
except RuntimeError as tf:
pass
except Exception as e:
print(e)
def download_pytube(data):
link = data.get("link")
quality = data.get("quality")
message = data.get("message")
filename = data.get("filename")
loop = data.get("loop")
isaudio = data.get("audio")
stat = data.get("stat")
ft = time.time()
prog = functools.partial(
on_progress,
start=ft,
message=message,
loop=loop,
stat=stat)
yt = YouTube(link, on_progress_callback=prog)
streams = yt.streams
title = yt.title
thumb = yt.thumbnail_url
if quality:
video = streams.filter(progressive=True, resolution=quality)[0]
else:
video = streams.filter(progressive=True)[-1]
audio = streams.get_audio_only()
st = time.time()
if message:
temp[message.id] = st
if isaudio:
audio.download(filename=filename)
else:
video.download(filename=filename)
del temp[message.id]
return {
"ok": True,
"message": " File has been downloaded",
"filename": filename,
"time_taken": int(
time.time() - ft),
"title": title}
d = {
"link": "http://www.youtube.com/watch?v=jZURhuJjMqs",
"quality": "720p",
"filename": "abc.mp4"}
# download_pytube(d)
def get_the_video_id(url: str):
url = url.replace('https://', '').replace('http://', '').split('/')[1]
if '&list' in url:
url = url[url.find("v=") + 2: url.find('&list')]
return url
elif "watch?v=" in url:
url = url[url.find("v=") + 2:]
return url
return url
# download_pytube(d)
# get_data_of_video("http://www.youtube.com/watch?v=OlV2vgkopBA")
async def async_download(url, name="10110", fo="", m=None):
try:
async with down_ses.stream("GET", url) as r:
if not fo:
fo = r.headers["content-type"].split('/')[1]
for x in " ~`|√π♪÷×{}£$℅^°=[]\\<>,.@#৳%&*-+()!\"':;/?":
name = name.replace(x, '_')
name = name.replace('___', '_').replace('__', '_')
filename = f"{name}.{fo}"
total = int(r.headers["content-length"])
st = time.time()
ft = time.time()
async with aiofiles.open(filename, "wb") as file:
async for chunk in r.aiter_bytes():
await file.write(chunk)
dnd = r.num_bytes_downloaded
cu = time.time()
if cu - st >= 2:
j = 1000**2
a = f"<b>Downloaded {dnd/j:.2f} MB of {total/j:.2f} MB @ {dnd/j/(cu-ft):.2f} MB/s</b>"
if not m:
print(a)
if m:
await m.edit_text(a)
st = time.time()
return {
"ok": True,
"message": " File has been downloaded",
"filename": filename,
"size": total,
"time_taken": int(
time.time() - ft)}
except httpx.RemoteProtocolError as e:
return {
"ok": False,
"message": "Server closed connection unexpectedly",
"error": str(e)}
except Exception as e:
return {"ok": False, "message": "Some error occurred", "error": str(e)}
async def main():
a = find_download_link("https://youtu.be/v3uPtNMXsvI")
x = await async_download(a.get("url"), a.get("title"), "mp4")
print(x)
if __name__ == "__main__":
# asyncio.run(main())
# os.system("pip freeze > requirements.txt")
pass