Erfan commited on
Commit ·
6f0a0db
1
Parent(s): f528143
fix: 5 yt-dlp clients + cobalt API fallback
Browse files- bot/youtube.py +84 -79
bot/youtube.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
import yt_dlp
|
| 3 |
import uuid
|
|
|
|
| 4 |
from config import DOWNLOAD_DIR, DATA_DIR
|
| 5 |
|
| 6 |
os.makedirs(DOWNLOAD_DIR, exist_ok=True)
|
|
@@ -8,6 +9,10 @@ os.makedirs(DOWNLOAD_DIR, exist_ok=True)
|
|
| 8 |
COOKIES_FILE = os.path.join(DATA_DIR, "cookies.txt")
|
| 9 |
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
def _find_downloaded_file():
|
| 12 |
for f in os.listdir(DOWNLOAD_DIR):
|
| 13 |
p = os.path.join(DOWNLOAD_DIR, f)
|
|
@@ -16,93 +21,93 @@ def _find_downloaded_file():
|
|
| 16 |
return None
|
| 17 |
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
async def get_video_info(url: str):
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 35 |
-
info = ydl.extract_info(url, download=False)
|
| 36 |
-
return {
|
| 37 |
-
"title": info.get("title", "Unknown"),
|
| 38 |
-
"duration": info.get("duration", 0),
|
| 39 |
-
"thumbnail": info.get("thumbnail", ""),
|
| 40 |
-
"url": url,
|
| 41 |
-
}
|
| 42 |
|
| 43 |
|
| 44 |
async def download_yt(url: str, quality: str = "audio"):
|
| 45 |
-
try:
|
| 46 |
-
from pytubefix import YouTube
|
| 47 |
-
|
| 48 |
-
yt = YouTube(url)
|
| 49 |
-
|
| 50 |
-
if quality == "audio":
|
| 51 |
-
stream = yt.streams.filter(only_audio=True).first()
|
| 52 |
-
elif quality == "720p":
|
| 53 |
-
stream = yt.streams.filter(res="720p").first()
|
| 54 |
-
if not stream:
|
| 55 |
-
stream = yt.streams.filter(res="480p").first()
|
| 56 |
-
if not stream:
|
| 57 |
-
stream = yt.streams.get_highest_resolution()
|
| 58 |
-
elif quality == "1080p":
|
| 59 |
-
stream = yt.streams.filter(res="1080p").first()
|
| 60 |
-
if not stream:
|
| 61 |
-
stream = yt.streams.filter(res="720p").first()
|
| 62 |
-
if not stream:
|
| 63 |
-
stream = yt.streams.get_highest_resolution()
|
| 64 |
-
else:
|
| 65 |
-
stream = yt.streams.get_highest_resolution()
|
| 66 |
-
|
| 67 |
-
if stream:
|
| 68 |
-
print(f"[youtube] pytubefix downloading: {stream}")
|
| 69 |
-
file_path = stream.download(output_path=DOWNLOAD_DIR)
|
| 70 |
-
if os.path.exists(file_path):
|
| 71 |
-
return file_path, yt.title
|
| 72 |
-
except Exception as e:
|
| 73 |
-
print(f"[youtube] pytubefix failed: {e}")
|
| 74 |
-
|
| 75 |
-
print("[youtube] Falling back to yt-dlp...")
|
| 76 |
-
return await _download_ytdlp(url, quality)
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
async def _download_ytdlp(url: str, quality: str = "audio"):
|
| 80 |
if quality == "audio":
|
| 81 |
-
|
| 82 |
else:
|
| 83 |
-
|
| 84 |
|
| 85 |
-
|
|
|
|
| 86 |
try:
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
"
|
| 91 |
-
|
| 92 |
-
"extractor_args": {"youtube": {"player_client": ["web"]}},
|
| 93 |
-
}
|
| 94 |
-
if os.path.exists(COOKIES_FILE):
|
| 95 |
-
opts["cookiefile"] = COOKIES_FILE
|
| 96 |
-
with yt_dlp.YoutubeDL(opts) as ydl:
|
| 97 |
-
info = ydl.extract_info(url, download=True)
|
| 98 |
-
file_path = ydl.prepare_filename(info)
|
| 99 |
-
if os.path.exists(file_path):
|
| 100 |
-
return file_path, info.get("title", "video")
|
| 101 |
-
found = _find_downloaded_file()
|
| 102 |
-
if found:
|
| 103 |
-
return found, info.get("title", "video")
|
| 104 |
except Exception as e:
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
|
|
|
| 108 |
return None, None
|
|
|
|
| 1 |
import os
|
| 2 |
import yt_dlp
|
| 3 |
import uuid
|
| 4 |
+
import httpx
|
| 5 |
from config import DOWNLOAD_DIR, DATA_DIR
|
| 6 |
|
| 7 |
os.makedirs(DOWNLOAD_DIR, exist_ok=True)
|
|
|
|
| 9 |
COOKIES_FILE = os.path.join(DATA_DIR, "cookies.txt")
|
| 10 |
|
| 11 |
|
| 12 |
+
def _log(msg: str):
|
| 13 |
+
print(f"[youtube] {msg}")
|
| 14 |
+
|
| 15 |
+
|
| 16 |
def _find_downloaded_file():
|
| 17 |
for f in os.listdir(DOWNLOAD_DIR):
|
| 18 |
p = os.path.join(DOWNLOAD_DIR, f)
|
|
|
|
| 21 |
return None
|
| 22 |
|
| 23 |
|
| 24 |
+
def _ydl_opts(format_spec: str, player_client: str = "web"):
|
| 25 |
+
opts = {
|
| 26 |
+
"format": format_spec,
|
| 27 |
+
"outtmpl": os.path.join(DOWNLOAD_DIR, f"{uuid.uuid4()}.%(ext)s"),
|
| 28 |
+
"quiet": True,
|
| 29 |
+
"no_warnings": True,
|
| 30 |
+
"extractor_args": {"youtube": {"player_client": [player_client]}},
|
| 31 |
+
}
|
| 32 |
+
if os.path.exists(COOKIES_FILE):
|
| 33 |
+
opts["cookiefile"] = COOKIES_FILE
|
| 34 |
+
return opts
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def _try_ytdlp(url: str, format_spec: str, player_client: str = "web"):
|
| 38 |
+
opts = _ydl_opts(format_spec, player_client)
|
| 39 |
+
with yt_dlp.YoutubeDL(opts) as ydl:
|
| 40 |
+
info = ydl.extract_info(url, download=True)
|
| 41 |
+
file_path = ydl.prepare_filename(info)
|
| 42 |
+
if os.path.exists(file_path):
|
| 43 |
+
return file_path, info.get("title", "video")
|
| 44 |
+
found = _find_downloaded_file()
|
| 45 |
+
if found:
|
| 46 |
+
return found, info.get("title", "video")
|
| 47 |
+
return None, None
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
async def _try_cobalt_api(url: str):
|
| 51 |
+
_log("Trying cobalt.tools API...")
|
| 52 |
+
async with httpx.AsyncClient(timeout=120) as client:
|
| 53 |
+
r = await client.post(
|
| 54 |
+
"https://api.cobalt.tools/api/json",
|
| 55 |
+
json={"url": url, "vCodec": "h264", "vQuality": "720"},
|
| 56 |
+
headers={"Accept": "application/json"},
|
| 57 |
+
)
|
| 58 |
+
data = r.json()
|
| 59 |
+
if data.get("url"):
|
| 60 |
+
_log(f"cobalt returned URL: {data['url']}")
|
| 61 |
+
r2 = await client.get(data["url"], follow_redirects=True, timeout=120)
|
| 62 |
+
if r2.status_code == 200:
|
| 63 |
+
ext = ".mp4"
|
| 64 |
+
filename = f"{uuid.uuid4()}{ext}"
|
| 65 |
+
filepath = os.path.join(DOWNLOAD_DIR, filename)
|
| 66 |
+
with open(filepath, "wb") as f:
|
| 67 |
+
f.write(r2.content)
|
| 68 |
+
return filepath, "YouTube Video"
|
| 69 |
+
return None, None
|
| 70 |
+
|
| 71 |
+
|
| 72 |
async def get_video_info(url: str):
|
| 73 |
+
for client in ["web", "android", "tv", "ios"]:
|
| 74 |
+
try:
|
| 75 |
+
opts = _ydl_opts("best", client)
|
| 76 |
+
with yt_dlp.YoutubeDL(opts) as ydl:
|
| 77 |
+
info = ydl.extract_info(url, download=False)
|
| 78 |
+
return {
|
| 79 |
+
"title": info.get("title", "Unknown"),
|
| 80 |
+
"duration": info.get("duration", 0),
|
| 81 |
+
"thumbnail": info.get("thumbnail", ""),
|
| 82 |
+
"url": url,
|
| 83 |
+
}
|
| 84 |
+
except Exception as e:
|
| 85 |
+
_log(f"info client={client} failed: {e}")
|
| 86 |
+
return {"title": "Unknown", "duration": 0, "thumbnail": "", "url": url}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
|
| 89 |
async def download_yt(url: str, quality: str = "audio"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
if quality == "audio":
|
| 91 |
+
fmt = "bestaudio"
|
| 92 |
else:
|
| 93 |
+
fmt = "best"
|
| 94 |
|
| 95 |
+
clients = ["web", "android", "tv", "ios", "web_creator"]
|
| 96 |
+
for client in clients:
|
| 97 |
try:
|
| 98 |
+
_log(f"Trying yt-dlp client={client} fmt={fmt}")
|
| 99 |
+
result = _try_ytdlp(url, fmt, client)
|
| 100 |
+
if result[0]:
|
| 101 |
+
_log(f"SUCCESS with client={client}")
|
| 102 |
+
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
except Exception as e:
|
| 104 |
+
_log(f"client={client} failed: {e}")
|
| 105 |
+
|
| 106 |
+
_log("All yt-dlp clients failed, trying cobalt API...")
|
| 107 |
+
cobalt_result = await _try_cobalt_api(url)
|
| 108 |
+
if cobalt_result[0]:
|
| 109 |
+
_log("SUCCESS with cobalt API")
|
| 110 |
+
return cobalt_result
|
| 111 |
|
| 112 |
+
_log("All methods failed")
|
| 113 |
return None, None
|