Spaces:
Paused
Paused
taslim19
commited on
Commit
·
7041448
1
Parent(s):
1122d52
feat: patch /play to use Bitflow API for YouTube, configurable API key via env
Browse files- DragMusic/platforms/Youtube.py +21 -2
- DragMusic/plugins/play/play.py +44 -13
- config.py +5 -0
DragMusic/platforms/Youtube.py
CHANGED
|
@@ -7,7 +7,9 @@ import yt_dlp
|
|
| 7 |
from pyrogram.enums import MessageEntityType
|
| 8 |
from pyrogram.types import Message
|
| 9 |
from youtubesearchpython.__future__ import VideosSearch
|
| 10 |
-
|
|
|
|
|
|
|
| 11 |
from DragMusic.utils.database import is_on_off
|
| 12 |
from DragMusic.utils.formatters import time_to_seconds
|
| 13 |
|
|
@@ -350,4 +352,21 @@ class YouTubeAPI:
|
|
| 350 |
else:
|
| 351 |
direct = True
|
| 352 |
downloaded_file = await loop.run_in_executor(None, audio_dl)
|
| 353 |
-
return downloaded_file, direct
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
|
|
|
|
| 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(timeout=150) as client:
|
| 371 |
+
response = await client.get(api_url, params=params)
|
| 372 |
+
return response.json()
|
DragMusic/plugins/play/play.py
CHANGED
|
@@ -24,7 +24,7 @@ from DragMusic.utils.inline import (
|
|
| 24 |
)
|
| 25 |
from DragMusic.utils.logger import play_logs
|
| 26 |
from DragMusic.utils.stream.stream import stream
|
| 27 |
-
from config import BANNED_USERS, lyrical
|
| 28 |
|
| 29 |
|
| 30 |
@app.on_message(
|
|
@@ -179,15 +179,32 @@ async def play_commnd(
|
|
| 179 |
cap = _["play_9"]
|
| 180 |
else:
|
| 181 |
try:
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
elif await Spotify.valid(url):
|
| 192 |
spotify = True
|
| 193 |
if not config.SPOTIFY_CLIENT_ID and not config.SPOTIFY_CLIENT_SECRET:
|
|
@@ -335,9 +352,23 @@ async def play_commnd(
|
|
| 335 |
if "-v" in query:
|
| 336 |
query = query.replace("-v", "")
|
| 337 |
try:
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 341 |
streamtype = "youtube"
|
| 342 |
if str(playmode) == "Direct":
|
| 343 |
if not plist_type:
|
|
|
|
| 24 |
)
|
| 25 |
from DragMusic.utils.logger import play_logs
|
| 26 |
from DragMusic.utils.stream.stream import stream
|
| 27 |
+
from config import BANNED_USERS, lyrical, BITFLOW_API_KEY
|
| 28 |
|
| 29 |
|
| 30 |
@app.on_message(
|
|
|
|
| 179 |
cap = _["play_9"]
|
| 180 |
else:
|
| 181 |
try:
|
| 182 |
+
bitflow_result = await YouTube.bitflow_video(url, api_key=BITFLOW_API_KEY)
|
| 183 |
+
if bitflow_result and bitflow_result.get("status") == "success":
|
| 184 |
+
details = {
|
| 185 |
+
"title": bitflow_result.get("title"),
|
| 186 |
+
"duration_min": bitflow_result.get("duration"),
|
| 187 |
+
"thumb": bitflow_result.get("thumbnail"),
|
| 188 |
+
"link": bitflow_result.get("url"),
|
| 189 |
+
}
|
| 190 |
+
track_id = bitflow_result.get("id")
|
| 191 |
+
streamtype = "youtube"
|
| 192 |
+
img = details["thumb"]
|
| 193 |
+
cap = _["play_10"].format(details["title"], details["duration_min"])
|
| 194 |
+
else:
|
| 195 |
+
details, track_id = await YouTube.track(url)
|
| 196 |
+
streamtype = "youtube"
|
| 197 |
+
img = details["thumb"]
|
| 198 |
+
cap = _["play_10"].format(details["title"], details["duration_min"])
|
| 199 |
+
except Exception as e:
|
| 200 |
+
print(f"Bitflow API error: {e}")
|
| 201 |
+
try:
|
| 202 |
+
details, track_id = await YouTube.track(url)
|
| 203 |
+
streamtype = "youtube"
|
| 204 |
+
img = details["thumb"]
|
| 205 |
+
cap = _["play_10"].format(details["title"], details["duration_min"])
|
| 206 |
+
except:
|
| 207 |
+
return await mystic.edit_text(_["play_3"])
|
| 208 |
elif await Spotify.valid(url):
|
| 209 |
spotify = True
|
| 210 |
if not config.SPOTIFY_CLIENT_ID and not config.SPOTIFY_CLIENT_SECRET:
|
|
|
|
| 352 |
if "-v" in query:
|
| 353 |
query = query.replace("-v", "")
|
| 354 |
try:
|
| 355 |
+
bitflow_result = await YouTube.bitflow_video(query, api_key=BITFLOW_API_KEY)
|
| 356 |
+
if bitflow_result and bitflow_result.get("status") == "success":
|
| 357 |
+
details = {
|
| 358 |
+
"title": bitflow_result.get("title"),
|
| 359 |
+
"duration_min": bitflow_result.get("duration"),
|
| 360 |
+
"thumb": bitflow_result.get("thumbnail"),
|
| 361 |
+
"link": bitflow_result.get("url"),
|
| 362 |
+
}
|
| 363 |
+
track_id = bitflow_result.get("id")
|
| 364 |
+
else:
|
| 365 |
+
details, track_id = await YouTube.track(query)
|
| 366 |
+
except Exception as e:
|
| 367 |
+
print(f"Bitflow API error: {e}")
|
| 368 |
+
try:
|
| 369 |
+
details, track_id = await YouTube.track(query)
|
| 370 |
+
except:
|
| 371 |
+
return await mystic.edit_text(_["play_3"])
|
| 372 |
streamtype = "youtube"
|
| 373 |
if str(playmode) == "Direct":
|
| 374 |
if not plist_type:
|
config.py
CHANGED
|
@@ -31,6 +31,8 @@ LOGGER_ID = int(getenv("LOGGER_ID", ))
|
|
| 31 |
# Get this value from @FallenxBot on Telegram by /id
|
| 32 |
OWNER_ID = int(getenv("OWNER_ID", ))
|
| 33 |
|
|
|
|
|
|
|
| 34 |
## Fill these variables if you're deploying on heroku.
|
| 35 |
# Your heroku app name
|
| 36 |
HEROKU_APP_NAME = getenv("HEROKU_APP_NAME")
|
|
@@ -98,6 +100,9 @@ SPOTIFY_ALBUM_IMG_URL = "https://te.legra.ph/file/b35fd1dfca73b950b1b05.jpg"
|
|
| 98 |
SPOTIFY_PLAYLIST_IMG_URL = "https://te.legra.ph/file/95b3ca7993bbfaf993dcb.jpg"
|
| 99 |
|
| 100 |
|
|
|
|
|
|
|
|
|
|
| 101 |
def time_to_seconds(time):
|
| 102 |
stringt = str(time)
|
| 103 |
return sum(int(x) * 60**i for i, x in enumerate(reversed(stringt.split(":"))))
|
|
|
|
| 31 |
# Get this value from @FallenxBot on Telegram by /id
|
| 32 |
OWNER_ID = int(getenv("OWNER_ID", ))
|
| 33 |
|
| 34 |
+
BITFLOW_API_KEY = getenv("BITFLOW_API_KEY", None) # Bitflow API key for YouTube downloads
|
| 35 |
+
|
| 36 |
## Fill these variables if you're deploying on heroku.
|
| 37 |
# Your heroku app name
|
| 38 |
HEROKU_APP_NAME = getenv("HEROKU_APP_NAME")
|
|
|
|
| 100 |
SPOTIFY_PLAYLIST_IMG_URL = "https://te.legra.ph/file/95b3ca7993bbfaf993dcb.jpg"
|
| 101 |
|
| 102 |
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
|
| 106 |
def time_to_seconds(time):
|
| 107 |
stringt = str(time)
|
| 108 |
return sum(int(x) * 60**i for i, x in enumerate(reversed(stringt.split(":"))))
|