init awal 30 lagu
This commit is contained in:
40
yt_engine.py
40
yt_engine.py
@@ -93,3 +93,43 @@ def search_and_download(query_or_url: str):
|
||||
|
||||
async def process_youtube_request(query: str):
|
||||
return await asyncio.to_thread(search_and_download, query)
|
||||
|
||||
|
||||
# ==========================================
|
||||
# FITUR TAMBAHAN: PENCARIAN LIST LAGU
|
||||
# ==========================================
|
||||
def fetch_search_results_sync(query: str, max_results: int = 30):
|
||||
"""Mengambil daftar hasil pencarian tanpa mendownload audionya"""
|
||||
ydl_opts_search = {
|
||||
'quiet': True,
|
||||
'extract_flat': True, # Mengambil metadata saja biar super cepat
|
||||
# KITA HAPUS extractor_args (player_client) di sini karena bikin search error
|
||||
}
|
||||
|
||||
if os.path.exists("cookies.txt"):
|
||||
ydl_opts_search['cookiefile'] = 'cookies.txt'
|
||||
|
||||
try:
|
||||
with yt_dlp.YoutubeDL(ydl_opts_search) as ydl:
|
||||
# Memaksa yt-dlp menggunakan mode pencarian (ytsearch) secara eksplisit
|
||||
search_query = f"ytsearch{max_results}:{query}"
|
||||
info = ydl.extract_info(search_query, download=False)
|
||||
|
||||
results = []
|
||||
if 'entries' in info:
|
||||
for entry in info['entries']:
|
||||
if entry and entry.get('id') and entry.get('title'):
|
||||
# Terkadang di hasil pencarian nama key-nya 'channel', bukan 'uploader'
|
||||
uploader_name = entry.get('uploader') or entry.get('channel') or 'Unknown'
|
||||
results.append({
|
||||
'id': entry['id'],
|
||||
'title': entry['title'],
|
||||
'uploader': uploader_name
|
||||
})
|
||||
return results
|
||||
except Exception as e:
|
||||
print(f"Search error: {e}")
|
||||
return []
|
||||
|
||||
async def search_youtube_list(query: str, max_results: int = 30):
|
||||
return await asyncio.to_thread(fetch_search_results_sync, query, max_results)
|
||||
|
||||
Reference in New Issue
Block a user