Spaces:
Paused
Paused
Update DragMusic/platforms/Youtube.py
Browse files- DragMusic/platforms/Youtube.py +75 -54
DragMusic/platforms/Youtube.py
CHANGED
@@ -3,24 +3,14 @@ import os
|
|
3 |
import re
|
4 |
import json
|
5 |
import httpx # Make sure to import httpx
|
6 |
-
import
|
7 |
import yt_dlp
|
8 |
-
from typing import Union
|
9 |
from pyrogram.enums import MessageEntityType
|
10 |
from pyrogram.types import Message
|
11 |
from youtubesearchpython.__future__ import VideosSearch
|
12 |
from DragMusic.utils.database import is_on_off
|
13 |
from DragMusic.utils.formatters import time_to_seconds
|
14 |
-
|
15 |
-
from pyrogram.types import Message
|
16 |
-
from pyrogram.enums import MessageEntityType
|
17 |
-
from youtubesearchpython.__future__ import VideosSearch
|
18 |
-
|
19 |
-
|
20 |
-
def time_to_seconds(time):
|
21 |
-
stringt = str(time)
|
22 |
-
return sum(int(x) * 60**i for i, x in enumerate(reversed(stringt.split(":"))))
|
23 |
-
|
24 |
|
25 |
async def shell_cmd(cmd):
|
26 |
proc = await asyncio.create_subprocess_shell(
|
@@ -36,22 +26,6 @@ async def shell_cmd(cmd):
|
|
36 |
return errorz.decode("utf-8")
|
37 |
return out.decode("utf-8")
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
async def get_stream_url(query, video=False):
|
42 |
-
api_url = "http://46.250.243.87:1470/youtube"
|
43 |
-
api_key = "1a873582a7c83342f961cc0a177b2b26"
|
44 |
-
|
45 |
-
async with httpx.AsyncClient(timeout=60) as client:
|
46 |
-
params = {"query": query, "video": video, "api_key": api_key}
|
47 |
-
response = await client.get(api_url, params=params)
|
48 |
-
if response.status_code != 200:
|
49 |
-
return ""
|
50 |
-
info = response.json()
|
51 |
-
return info.get("stream_url")
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
class YouTubeAPI:
|
56 |
def __init__(self):
|
57 |
self.base = "https://www.youtube.com/watch?v="
|
@@ -144,9 +118,20 @@ class YouTubeAPI:
|
|
144 |
link = self.base + link
|
145 |
if "&" in link:
|
146 |
link = link.split("&")[0]
|
147 |
-
|
148 |
-
|
149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
150 |
|
151 |
async def playlist(self, link, limit, user_id, videoid: Union[bool, str] = None):
|
152 |
if videoid:
|
@@ -240,6 +225,22 @@ class YouTubeAPI:
|
|
240 |
thumbnail = result[query_type]["thumbnails"][0]["url"].split("?")[0]
|
241 |
return title, duration_min, thumbnail, vidid
|
242 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
243 |
async def download(
|
244 |
self,
|
245 |
link: str,
|
@@ -253,45 +254,64 @@ class YouTubeAPI:
|
|
253 |
) -> str:
|
254 |
if videoid:
|
255 |
link = self.base + link
|
|
|
|
|
256 |
loop = asyncio.get_running_loop()
|
257 |
-
|
258 |
-
def audio_dl():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
259 |
ydl_optssx = {
|
260 |
"format": "bestaudio/best",
|
261 |
-
"outtmpl":
|
262 |
"geo_bypass": True,
|
263 |
"nocheckcertificate": True,
|
264 |
"quiet": True,
|
265 |
"no_warnings": True,
|
266 |
}
|
267 |
x = yt_dlp.YoutubeDL(ydl_optssx)
|
268 |
-
info = x.extract_info(link, False)
|
269 |
-
xyz = os.path.join("downloads", f"{info['id']}.{info['ext']}")
|
270 |
if os.path.exists(xyz):
|
271 |
return xyz
|
272 |
-
x.download([
|
273 |
return xyz
|
274 |
-
|
275 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
276 |
ydl_optssx = {
|
277 |
"format": "(bestvideo[height<=?720][width<=?1280][ext=mp4])+(bestaudio[ext=m4a])",
|
278 |
-
"outtmpl":
|
279 |
"geo_bypass": True,
|
280 |
"nocheckcertificate": True,
|
281 |
"quiet": True,
|
282 |
"no_warnings": True,
|
283 |
}
|
284 |
x = yt_dlp.YoutubeDL(ydl_optssx)
|
285 |
-
info = x.extract_info(link, False)
|
286 |
-
xyz = os.path.join("downloads", f"{info['id']}.{info['ext']}")
|
287 |
if os.path.exists(xyz):
|
288 |
return xyz
|
289 |
-
x.download([
|
290 |
return xyz
|
291 |
-
|
292 |
def song_video_dl():
|
|
|
|
|
293 |
formats = f"{format_id}+140"
|
294 |
-
fpath = f"downloads/{title}"
|
295 |
ydl_optssx = {
|
296 |
"format": formats,
|
297 |
"outtmpl": fpath,
|
@@ -304,9 +324,9 @@ class YouTubeAPI:
|
|
304 |
}
|
305 |
x = yt_dlp.YoutubeDL(ydl_optssx)
|
306 |
x.download([link])
|
307 |
-
|
308 |
def song_audio_dl():
|
309 |
-
|
|
|
310 |
ydl_optssx = {
|
311 |
"format": format_id,
|
312 |
"outtmpl": fpath,
|
@@ -325,19 +345,20 @@ class YouTubeAPI:
|
|
325 |
}
|
326 |
x = yt_dlp.YoutubeDL(ydl_optssx)
|
327 |
x.download([link])
|
328 |
-
|
329 |
if songvideo:
|
330 |
await loop.run_in_executor(None, song_video_dl)
|
331 |
-
|
|
|
332 |
return fpath
|
333 |
elif songaudio:
|
334 |
await loop.run_in_executor(None, song_audio_dl)
|
335 |
-
|
|
|
336 |
return fpath
|
337 |
elif video:
|
338 |
-
|
339 |
-
|
340 |
else:
|
341 |
-
direct =
|
342 |
-
downloaded_file = await
|
343 |
return downloaded_file, direct
|
|
|
3 |
import re
|
4 |
import json
|
5 |
import httpx # Make sure to import httpx
|
6 |
+
from typing import Union
|
7 |
import yt_dlp
|
|
|
8 |
from pyrogram.enums import MessageEntityType
|
9 |
from pyrogram.types import Message
|
10 |
from youtubesearchpython.__future__ import VideosSearch
|
11 |
from DragMusic.utils.database import is_on_off
|
12 |
from DragMusic.utils.formatters import time_to_seconds
|
13 |
+
import tempfile
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
async def shell_cmd(cmd):
|
16 |
proc = await asyncio.create_subprocess_shell(
|
|
|
26 |
return errorz.decode("utf-8")
|
27 |
return out.decode("utf-8")
|
28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
class YouTubeAPI:
|
30 |
def __init__(self):
|
31 |
self.base = "https://www.youtube.com/watch?v="
|
|
|
118 |
link = self.base + link
|
119 |
if "&" in link:
|
120 |
link = link.split("&")[0]
|
121 |
+
proc = await asyncio.create_subprocess_exec(
|
122 |
+
"yt-dlp",
|
123 |
+
"-g",
|
124 |
+
"-f",
|
125 |
+
"best[height<=?720][width<=?1280]",
|
126 |
+
f"{link}",
|
127 |
+
stdout=asyncio.subprocess.PIPE,
|
128 |
+
stderr=asyncio.subprocess.PIPE,
|
129 |
+
)
|
130 |
+
stdout, stderr = await proc.communicate()
|
131 |
+
if stdout:
|
132 |
+
return 1, stdout.decode().split("\n")[0]
|
133 |
+
else:
|
134 |
+
return 0, stderr.decode()
|
135 |
|
136 |
async def playlist(self, link, limit, user_id, videoid: Union[bool, str] = None):
|
137 |
if videoid:
|
|
|
225 |
thumbnail = result[query_type]["thumbnails"][0]["url"].split("?")[0]
|
226 |
return title, duration_min, thumbnail, vidid
|
227 |
|
228 |
+
async def get_video_info_from_bitflow(self, url: str, video: bool):
|
229 |
+
api_url = "https://bitflow.in/api/youtube"
|
230 |
+
params = {
|
231 |
+
"query": url,
|
232 |
+
"format": "video" if video else "audio",
|
233 |
+
"download": True,
|
234 |
+
"api_key": "1spiderkey2"
|
235 |
+
}
|
236 |
+
|
237 |
+
async with httpx.AsyncClient() as client:
|
238 |
+
response = await client.get(api_url, params=params, timeout=150)
|
239 |
+
if response.status_code == 200:
|
240 |
+
return response.json()
|
241 |
+
else:
|
242 |
+
return {"status": "error", "message": "Failed to fetch data from Bitflow API."}
|
243 |
+
|
244 |
async def download(
|
245 |
self,
|
246 |
link: str,
|
|
|
254 |
) -> str:
|
255 |
if videoid:
|
256 |
link = self.base + link
|
257 |
+
if "&" in link:
|
258 |
+
link = link.split("&")[0]
|
259 |
loop = asyncio.get_running_loop()
|
260 |
+
bitflow_info = await self.get_video_info_from_bitflow(link, video)
|
261 |
+
def audio_dl(bitflow_info):
|
262 |
+
temp_dir = tempfile.gettempdir()
|
263 |
+
xyz = os.path.join(temp_dir, f"{bitflow_info['videoid']}.{bitflow_info['ext']}")
|
264 |
+
url = bitflow_info['url']
|
265 |
+
# If the url is not a YouTube link, download directly
|
266 |
+
if not (url.startswith('http') and ('youtube.com' in url or 'youtu.be' in url)):
|
267 |
+
import httpx
|
268 |
+
with httpx.Client() as client:
|
269 |
+
r = client.get(url)
|
270 |
+
with open(xyz, "wb") as f:
|
271 |
+
f.write(r.content)
|
272 |
+
return xyz
|
273 |
ydl_optssx = {
|
274 |
"format": "bestaudio/best",
|
275 |
+
"outtmpl": xyz,
|
276 |
"geo_bypass": True,
|
277 |
"nocheckcertificate": True,
|
278 |
"quiet": True,
|
279 |
"no_warnings": True,
|
280 |
}
|
281 |
x = yt_dlp.YoutubeDL(ydl_optssx)
|
|
|
|
|
282 |
if os.path.exists(xyz):
|
283 |
return xyz
|
284 |
+
x.download([url])
|
285 |
return xyz
|
286 |
+
def video_dl(bitflow_info):
|
287 |
+
temp_dir = tempfile.gettempdir()
|
288 |
+
xyz = os.path.join(temp_dir, f"{bitflow_info['videoid']}.{bitflow_info['ext']}")
|
289 |
+
url = bitflow_info['url']
|
290 |
+
# If the url is not a YouTube link, download directly
|
291 |
+
if not (url.startswith('http') and ('youtube.com' in url or 'youtu.be' in url)):
|
292 |
+
import httpx
|
293 |
+
with httpx.Client() as client:
|
294 |
+
r = client.get(url)
|
295 |
+
with open(xyz, "wb") as f:
|
296 |
+
f.write(r.content)
|
297 |
+
return xyz
|
298 |
ydl_optssx = {
|
299 |
"format": "(bestvideo[height<=?720][width<=?1280][ext=mp4])+(bestaudio[ext=m4a])",
|
300 |
+
"outtmpl": xyz,
|
301 |
"geo_bypass": True,
|
302 |
"nocheckcertificate": True,
|
303 |
"quiet": True,
|
304 |
"no_warnings": True,
|
305 |
}
|
306 |
x = yt_dlp.YoutubeDL(ydl_optssx)
|
|
|
|
|
307 |
if os.path.exists(xyz):
|
308 |
return xyz
|
309 |
+
x.download([url])
|
310 |
return xyz
|
|
|
311 |
def song_video_dl():
|
312 |
+
temp_dir = tempfile.gettempdir()
|
313 |
+
fpath = os.path.join(temp_dir, f"{title}")
|
314 |
formats = f"{format_id}+140"
|
|
|
315 |
ydl_optssx = {
|
316 |
"format": formats,
|
317 |
"outtmpl": fpath,
|
|
|
324 |
}
|
325 |
x = yt_dlp.YoutubeDL(ydl_optssx)
|
326 |
x.download([link])
|
|
|
327 |
def song_audio_dl():
|
328 |
+
temp_dir = tempfile.gettempdir()
|
329 |
+
fpath = os.path.join(temp_dir, f"{title}.%(ext)s")
|
330 |
ydl_optssx = {
|
331 |
"format": format_id,
|
332 |
"outtmpl": fpath,
|
|
|
345 |
}
|
346 |
x = yt_dlp.YoutubeDL(ydl_optssx)
|
347 |
x.download([link])
|
|
|
348 |
if songvideo:
|
349 |
await loop.run_in_executor(None, song_video_dl)
|
350 |
+
temp_dir = tempfile.gettempdir()
|
351 |
+
fpath = os.path.join(temp_dir, f"{title}.mp4")
|
352 |
return fpath
|
353 |
elif songaudio:
|
354 |
await loop.run_in_executor(None, song_audio_dl)
|
355 |
+
temp_dir = tempfile.gettempdir()
|
356 |
+
fpath = os.path.join(temp_dir, f"{title}.mp3")
|
357 |
return fpath
|
358 |
elif video:
|
359 |
+
direct = True
|
360 |
+
downloaded_file = await loop.run_in_executor(None, video_dl, bitflow_info)
|
361 |
else:
|
362 |
+
direct = True
|
363 |
+
downloaded_file = await loop.run_in_executor(None, audio_dl, bitflow_info)
|
364 |
return downloaded_file, direct
|