Delete DragMusic/platforms/Youtube.py
Browse files- DragMusic/platforms/Youtube.py +0 -372
DragMusic/platforms/Youtube.py
DELETED
@@ -1,372 +0,0 @@
|
|
1 |
-
import asyncio
|
2 |
-
import os
|
3 |
-
import re
|
4 |
-
from typing import Union
|
5 |
-
|
6 |
-
import yt_dlp
|
7 |
-
from pyrogram.enums import MessageEntityType
|
8 |
-
from pyrogram.types import Message
|
9 |
-
from youtubesearchpython.__future__ import VideosSearch
|
10 |
-
import httpx
|
11 |
-
import config
|
12 |
-
import httpx
|
13 |
-
from DragMusic.utils.database import is_on_off
|
14 |
-
from DragMusic.utils.formatters import time_to_seconds
|
15 |
-
|
16 |
-
|
17 |
-
async def shell_cmd(cmd):
|
18 |
-
proc = await asyncio.create_subprocess_shell(
|
19 |
-
cmd,
|
20 |
-
stdout=asyncio.subprocess.PIPE,
|
21 |
-
stderr=asyncio.subprocess.PIPE,
|
22 |
-
)
|
23 |
-
out, errorz = await proc.communicate()
|
24 |
-
if errorz:
|
25 |
-
if "unavailable videos are hidden" in (errorz.decode("utf-8")).lower():
|
26 |
-
return out.decode("utf-8")
|
27 |
-
else:
|
28 |
-
return errorz.decode("utf-8")
|
29 |
-
return out.decode("utf-8")
|
30 |
-
|
31 |
-
|
32 |
-
class YouTubeAPI:
|
33 |
-
def __init__(self):
|
34 |
-
self.base = "https://www.youtube.com/watch?v="
|
35 |
-
self.regex = r"(?:youtube\.com|youtu\.be)"
|
36 |
-
self.status = "https://www.youtube.com/oembed?url="
|
37 |
-
self.listbase = "https://youtube.com/playlist?list="
|
38 |
-
self.reg = re.compile(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])")
|
39 |
-
|
40 |
-
async def exists(self, link: str, videoid: Union[bool, str] = None):
|
41 |
-
if videoid:
|
42 |
-
link = self.base + link
|
43 |
-
if re.search(self.regex, link):
|
44 |
-
return True
|
45 |
-
else:
|
46 |
-
return False
|
47 |
-
|
48 |
-
async def url(self, message_1: Message) -> Union[str, None]:
|
49 |
-
messages = [message_1]
|
50 |
-
if message_1.reply_to_message:
|
51 |
-
messages.append(message_1.reply_to_message)
|
52 |
-
text = ""
|
53 |
-
offset = None
|
54 |
-
length = None
|
55 |
-
for message in messages:
|
56 |
-
if offset:
|
57 |
-
break
|
58 |
-
if message.entities:
|
59 |
-
for entity in message.entities:
|
60 |
-
if entity.type == MessageEntityType.URL:
|
61 |
-
text = message.text or message.caption
|
62 |
-
offset, length = entity.offset, entity.length
|
63 |
-
break
|
64 |
-
elif message.caption_entities:
|
65 |
-
for entity in message.caption_entities:
|
66 |
-
if entity.type == MessageEntityType.TEXT_LINK:
|
67 |
-
return entity.url
|
68 |
-
if offset in (None,):
|
69 |
-
return None
|
70 |
-
return text[offset : offset + length]
|
71 |
-
|
72 |
-
async def details(self, link: str, videoid: Union[bool, str] = None):
|
73 |
-
if videoid:
|
74 |
-
link = self.base + link
|
75 |
-
if "&" in link:
|
76 |
-
link = link.split("&")[0]
|
77 |
-
results = VideosSearch(link, limit=1)
|
78 |
-
for result in (await results.next())["result"]:
|
79 |
-
title = result["title"]
|
80 |
-
duration_min = result["duration"]
|
81 |
-
thumbnail = result["thumbnails"][0]["url"].split("?")[0]
|
82 |
-
vidid = result["id"]
|
83 |
-
if str(duration_min) == "None":
|
84 |
-
duration_sec = 0
|
85 |
-
else:
|
86 |
-
duration_sec = int(time_to_seconds(duration_min))
|
87 |
-
return title, duration_min, duration_sec, thumbnail, vidid
|
88 |
-
|
89 |
-
async def title(self, link: str, videoid: Union[bool, str] = None):
|
90 |
-
if videoid:
|
91 |
-
link = self.base + link
|
92 |
-
if "&" in link:
|
93 |
-
link = link.split("&")[0]
|
94 |
-
results = VideosSearch(link, limit=1)
|
95 |
-
for result in (await results.next())["result"]:
|
96 |
-
title = result["title"]
|
97 |
-
return title
|
98 |
-
|
99 |
-
async def duration(self, link: str, videoid: Union[bool, str] = None):
|
100 |
-
if videoid:
|
101 |
-
link = self.base + link
|
102 |
-
if "&" in link:
|
103 |
-
link = link.split("&")[0]
|
104 |
-
results = VideosSearch(link, limit=1)
|
105 |
-
for result in (await results.next())["result"]:
|
106 |
-
duration = result["duration"]
|
107 |
-
return duration
|
108 |
-
|
109 |
-
async def thumbnail(self, link: str, videoid: Union[bool, str] = None):
|
110 |
-
if videoid:
|
111 |
-
link = self.base + link
|
112 |
-
if "&" in link:
|
113 |
-
link = link.split("&")[0]
|
114 |
-
results = VideosSearch(link, limit=1)
|
115 |
-
for result in (await results.next())["result"]:
|
116 |
-
thumbnail = result["thumbnails"][0]["url"].split("?")[0]
|
117 |
-
return thumbnail
|
118 |
-
|
119 |
-
async def video(self, link: str, videoid: Union[bool, str] = None):
|
120 |
-
if videoid:
|
121 |
-
link = self.base + link
|
122 |
-
if "&" in link:
|
123 |
-
link = link.split("&")[0]
|
124 |
-
proc = await asyncio.create_subprocess_exec(
|
125 |
-
"yt-dlp",
|
126 |
-
"--cookies",
|
127 |
-
"cookies.txt",
|
128 |
-
"-g",
|
129 |
-
"-f",
|
130 |
-
"best[height<=?720][width<=?1280]",
|
131 |
-
f"{link}",
|
132 |
-
stdout=asyncio.subprocess.PIPE,
|
133 |
-
stderr=asyncio.subprocess.PIPE,
|
134 |
-
)
|
135 |
-
stdout, stderr = await proc.communicate()
|
136 |
-
if stdout:
|
137 |
-
return 1, stdout.decode().split("\n")[0]
|
138 |
-
else:
|
139 |
-
return 0, stderr.decode()
|
140 |
-
|
141 |
-
async def playlist(self, link, limit, user_id, videoid: Union[bool, str] = None):
|
142 |
-
if videoid:
|
143 |
-
link = self.listbase + link
|
144 |
-
if "&" in link:
|
145 |
-
link = link.split("&")[0]
|
146 |
-
playlist = await shell_cmd(
|
147 |
-
f"yt-dlp --cookies cookies.txt -i --get-id --flat-playlist --playlist-end {limit} --skip-download {link}"
|
148 |
-
)
|
149 |
-
try:
|
150 |
-
result = playlist.split("\n")
|
151 |
-
for key in result:
|
152 |
-
if key == "":
|
153 |
-
result.remove(key)
|
154 |
-
except:
|
155 |
-
result = []
|
156 |
-
return result
|
157 |
-
|
158 |
-
async def track(self, link: str, videoid: Union[bool, str] = None):
|
159 |
-
if videoid:
|
160 |
-
link = self.base + link
|
161 |
-
if "&" in link:
|
162 |
-
link = link.split("&")[0]
|
163 |
-
results = VideosSearch(link, limit=1)
|
164 |
-
for result in (await results.next())["result"]:
|
165 |
-
title = result["title"]
|
166 |
-
duration_min = result["duration"]
|
167 |
-
vidid = result["id"]
|
168 |
-
yturl = result["link"]
|
169 |
-
thumbnail = result["thumbnails"][0]["url"].split("?")[0]
|
170 |
-
track_details = {
|
171 |
-
"title": title,
|
172 |
-
"link": yturl,
|
173 |
-
"vidid": vidid,
|
174 |
-
"duration_min": duration_min,
|
175 |
-
"thumb": thumbnail,
|
176 |
-
}
|
177 |
-
return track_details, vidid
|
178 |
-
|
179 |
-
async def formats(self, link: str, videoid: Union[bool, str] = None):
|
180 |
-
if videoid:
|
181 |
-
link = self.base + link
|
182 |
-
if "&" in link:
|
183 |
-
link = link.split("&")[0]
|
184 |
-
ytdl_opts = {"cookiefile": "cookies.txt", "quiet": True}
|
185 |
-
ydl = yt_dlp.YoutubeDL(ytdl_opts)
|
186 |
-
with ydl:
|
187 |
-
formats_available = []
|
188 |
-
r = ydl.extract_info(link, download=False)
|
189 |
-
for format in r["formats"]:
|
190 |
-
try:
|
191 |
-
str(format["format"])
|
192 |
-
except:
|
193 |
-
continue
|
194 |
-
if not "dash" in str(format["format"]).lower():
|
195 |
-
try:
|
196 |
-
format["format"]
|
197 |
-
format["filesize"]
|
198 |
-
format["format_id"]
|
199 |
-
format["ext"]
|
200 |
-
format["format_note"]
|
201 |
-
except:
|
202 |
-
continue
|
203 |
-
formats_available.append(
|
204 |
-
{
|
205 |
-
"format": format["format"],
|
206 |
-
"filesize": format["filesize"],
|
207 |
-
"format_id": format["format_id"],
|
208 |
-
"ext": format["ext"],
|
209 |
-
"format_note": format["format_note"],
|
210 |
-
"yturl": link,
|
211 |
-
}
|
212 |
-
)
|
213 |
-
return formats_available, link
|
214 |
-
|
215 |
-
async def slider(
|
216 |
-
self,
|
217 |
-
link: str,
|
218 |
-
query_type: int,
|
219 |
-
videoid: Union[bool, str] = None,
|
220 |
-
):
|
221 |
-
if videoid:
|
222 |
-
link = self.base + link
|
223 |
-
if "&" in link:
|
224 |
-
link = link.split("&")[0]
|
225 |
-
a = VideosSearch(link, limit=10)
|
226 |
-
result = (await a.next()).get("result")
|
227 |
-
title = result[query_type]["title"]
|
228 |
-
duration_min = result[query_type]["duration"]
|
229 |
-
vidid = result[query_type]["id"]
|
230 |
-
thumbnail = result[query_type]["thumbnails"][0]["url"].split("?")[0]
|
231 |
-
return title, duration_min, thumbnail, vidid
|
232 |
-
|
233 |
-
async def download(
|
234 |
-
self,
|
235 |
-
link: str,
|
236 |
-
mystic,
|
237 |
-
video: Union[bool, str] = None,
|
238 |
-
videoid: Union[bool, str] = None,
|
239 |
-
songaudio: Union[bool, str] = None,
|
240 |
-
songvideo: Union[bool, str] = None,
|
241 |
-
format_id: Union[bool, str] = None,
|
242 |
-
title: Union[bool, str] = None,
|
243 |
-
) -> str:
|
244 |
-
if videoid:
|
245 |
-
link = self.base + link
|
246 |
-
loop = asyncio.get_running_loop()
|
247 |
-
|
248 |
-
def audio_dl():
|
249 |
-
ydl_optssx = {
|
250 |
-
"cookiefile": "cookies.txt",
|
251 |
-
"format": "bestaudio/best",
|
252 |
-
"outtmpl": "downloads/%(id)s.%(ext)s",
|
253 |
-
"geo_bypass": True,
|
254 |
-
"nocheckcertificate": True,
|
255 |
-
"quiet": True,
|
256 |
-
"no_warnings": True,
|
257 |
-
}
|
258 |
-
x = yt_dlp.YoutubeDL(ydl_optssx)
|
259 |
-
info = x.extract_info(link, False)
|
260 |
-
xyz = os.path.join("downloads", f"{info['id']}.{info['ext']}")
|
261 |
-
if os.path.exists(xyz):
|
262 |
-
return xyz
|
263 |
-
x.download([link])
|
264 |
-
return xyz
|
265 |
-
|
266 |
-
def video_dl():
|
267 |
-
ydl_optssx = {
|
268 |
-
"format": "(bestvideo[height<=?720][width<=?1280][ext=mp4])+(bestaudio[ext=m4a])",
|
269 |
-
"outtmpl": "downloads/%(id)s.%(ext)s",
|
270 |
-
"geo_bypass": True,
|
271 |
-
"nocheckcertificate": True,
|
272 |
-
"quiet": True,
|
273 |
-
"no_warnings": True,
|
274 |
-
}
|
275 |
-
x = yt_dlp.YoutubeDL(ydl_optssx)
|
276 |
-
info = x.extract_info(link, False)
|
277 |
-
xyz = os.path.join("downloads", f"{info['id']}.{info['ext']}")
|
278 |
-
if os.path.exists(xyz):
|
279 |
-
return xyz
|
280 |
-
x.download([link])
|
281 |
-
return xyz
|
282 |
-
|
283 |
-
def song_video_dl():
|
284 |
-
formats = f"{format_id}+140"
|
285 |
-
fpath = f"downloads/{title}"
|
286 |
-
ydl_optssx = {
|
287 |
-
"cookiefile": "cookies.txt",
|
288 |
-
"format": formats,
|
289 |
-
"outtmpl": fpath,
|
290 |
-
"geo_bypass": True,
|
291 |
-
"nocheckcertificate": True,
|
292 |
-
"quiet": True,
|
293 |
-
"no_warnings": True,
|
294 |
-
"prefer_ffmpeg": True,
|
295 |
-
"merge_output_format": "mp4",
|
296 |
-
}
|
297 |
-
x = yt_dlp.YoutubeDL(ydl_optssx)
|
298 |
-
x.download([link])
|
299 |
-
|
300 |
-
def song_audio_dl():
|
301 |
-
fpath = f"downloads/{title}.%(ext)s"
|
302 |
-
ydl_optssx = {
|
303 |
-
"cookiefile": "cookies.txt",
|
304 |
-
"format": format_id,
|
305 |
-
"outtmpl": fpath,
|
306 |
-
"geo_bypass": True,
|
307 |
-
"nocheckcertificate": True,
|
308 |
-
"quiet": True,
|
309 |
-
"no_warnings": True,
|
310 |
-
"prefer_ffmpeg": True,
|
311 |
-
"postprocessors": [
|
312 |
-
{
|
313 |
-
"key": "FFmpegExtractAudio",
|
314 |
-
"preferredcodec": "mp3",
|
315 |
-
"preferredquality": "192",
|
316 |
-
}
|
317 |
-
],
|
318 |
-
}
|
319 |
-
x = yt_dlp.YoutubeDL(ydl_optssx)
|
320 |
-
x.download([link])
|
321 |
-
|
322 |
-
if songvideo:
|
323 |
-
await loop.run_in_executor(None, song_video_dl)
|
324 |
-
fpath = f"downloads/{title}.mp4"
|
325 |
-
return fpath
|
326 |
-
elif songaudio:
|
327 |
-
await loop.run_in_executor(None, song_audio_dl)
|
328 |
-
fpath = f"downloads/{title}.mp3"
|
329 |
-
return fpath
|
330 |
-
elif video:
|
331 |
-
if await is_on_off(1):
|
332 |
-
direct = True
|
333 |
-
downloaded_file = await loop.run_in_executor(None, video_dl)
|
334 |
-
else:
|
335 |
-
proc = await asyncio.create_subprocess_exec(
|
336 |
-
"yt-dlp",
|
337 |
-
"--cookies",
|
338 |
-
"cookies.txt",
|
339 |
-
"-g",
|
340 |
-
"-f",
|
341 |
-
"best[height<=?720][width<=?1280]",
|
342 |
-
f"{link}",
|
343 |
-
stdout=asyncio.subprocess.PIPE,
|
344 |
-
stderr=asyncio.subprocess.PIPE,
|
345 |
-
)
|
346 |
-
stdout, stderr = await proc.communicate()
|
347 |
-
if stdout:
|
348 |
-
downloaded_file = stdout.decode().split("\n")[0]
|
349 |
-
direct = None
|
350 |
-
else:
|
351 |
-
return
|
352 |
-
else:
|
353 |
-
direct = True
|
354 |
-
downloaded_file = await loop.run_in_executor(None, audio_dl)
|
355 |
-
return downloaded_file, direct
|
356 |
-
|
357 |
-
async def bitflow_video(self, query: str, api_key: str = None):
|
358 |
-
"""
|
359 |
-
Fetch video info from Bitflow API.
|
360 |
-
Args:
|
361 |
-
query (str): The search query or YouTube URL.
|
362 |
-
api_key (str, optional): Bitflow API key. Defaults to config.BITFLOW_API_KEY.
|
363 |
-
Returns:
|
364 |
-
dict: The JSON result from the Bitflow API.
|
365 |
-
"""
|
366 |
-
api_url = "https://bitflow.in/api/youtube"
|
367 |
-
if api_key is None:
|
368 |
-
api_key = config.BITFLOW_API_KEY
|
369 |
-
params = {"query": query, "format": "video", "api_key": api_key}
|
370 |
-
async with httpx.AsyncClient(tim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