v2
This commit is contained in:
43
yt_engine.py
43
yt_engine.py
@@ -106,3 +106,46 @@ async def process_youtube_request(query: str):
|
||||
saat menunggu proses download selesai.
|
||||
"""
|
||||
return await asyncio.to_thread(search_and_download, query)
|
||||
|
||||
# ==========================================
|
||||
# FITUR TAMBAHAN: YOUTUBE MIX RECOMMENDATION
|
||||
# ==========================================
|
||||
def fetch_recommendations_sync(video_id: str, limit: int = 3):
|
||||
"""
|
||||
Mengambil rekomendasi lagu dari YouTube Mix algoritma (Vibes/Genre serupa)
|
||||
"""
|
||||
# Gunakan settingan ringan karena kita cuma mau ambil teks judul, BUKAN download
|
||||
ydl_opts_recs = {
|
||||
'quiet': True,
|
||||
'extract_flat': True,
|
||||
'playlistend': limit + 1, # +1 karena urutan pertama biasanya lagu aslinya
|
||||
'extractor_args': {'youtube': ['player_client=android,ios']},
|
||||
}
|
||||
|
||||
# Sisipkan cookie jika ada
|
||||
if os.path.exists("cookies.txt"):
|
||||
ydl_opts_recs['cookiefile'] = 'cookies.txt'
|
||||
|
||||
try:
|
||||
with yt_dlp.YoutubeDL(ydl_opts_recs) as ydl:
|
||||
# RD = Playlist YouTube Mix khusus untuk video tersebut
|
||||
mix_url = f"https://www.youtube.com/watch?v={video_id}&list=RD{video_id}"
|
||||
info = ydl.extract_info(mix_url, download=False)
|
||||
|
||||
recs = []
|
||||
if 'entries' in info:
|
||||
# Loop dan ambil lagunya (Lewati index 0 karena itu lagu yg sedang diputar)
|
||||
for entry in info['entries'][1:]:
|
||||
if entry and entry.get('id') and entry.get('title'):
|
||||
recs.append({
|
||||
'id': entry['id'],
|
||||
'title': entry['title']
|
||||
})
|
||||
return recs
|
||||
except Exception as e:
|
||||
print(f"Gagal mengambil rekomendasi: {e}")
|
||||
return []
|
||||
|
||||
async def get_recommendations(video_id: str):
|
||||
"""Bungkus Async agar bot tidak hang"""
|
||||
return await asyncio.to_thread(fetch_recommendations_sync, video_id)
|
||||
|
||||
Reference in New Issue
Block a user