fixed youtube's api interaction, fixed discord connection handshake errors

This commit is contained in:
2025-11-27 12:46:46 +00:00
parent 669e9c19fc
commit c7e033acb6
6 changed files with 217 additions and 79 deletions

View File

@@ -3,11 +3,23 @@
import yt_dlp as ytdlp
import spotipy
# Updated yt-dlp options to handle current YouTube changes
ydl_opts = {
'format': 'bestaudio/best',
'quiet': True,
'default_search': 'ytsearch',
'ignoreerrors': True,
'format': 'bestaudio/best',
'quiet': True,
'no_warnings': False, # Show warnings for debugging
'default_search': 'ytsearch',
'ignoreerrors': True,
'source_address': '0.0.0.0', # Bind to IPv4 to avoid IPv6 issues
'extract_flat': False,
'nocheckcertificate': True,
# Add extractor args to handle YouTube's new requirements
'extractor_args': {
'youtube': {
'player_skip': ['webpage', 'configs'],
'player_client': ['android', 'web'],
}
},
}
async def main(url, sp):
@@ -45,16 +57,28 @@ async def search_song(search):
with ytdlp.YoutubeDL(ydl_opts) as ydl:
try:
info = ydl.extract_info(f"ytsearch1:{search}", download=False)
except:
except Exception as e:
print(f"Error searching for '{search}': {e}")
return []
if info is None:
return []
info = info['entries'][0] # Get audio stream URL
data = {'url': info['url'],
'title': info['title'],
'thumbnail': info['thumbnail'],
'duration': info['duration']} # Grab data
if 'entries' not in info or len(info['entries']) == 0:
return []
info = info['entries'][0] # Get first search result
# Get the best audio stream URL
if 'url' not in info:
print(f"No URL found for: {search}")
return []
data = {
'url': info['url'],
'title': info.get('title', 'Unknown'),
'thumbnail': info.get('thumbnail', ''),
'duration': info.get('duration', 0)
}
return [data]
@@ -123,15 +147,29 @@ async def song_download(url):
with ytdlp.YoutubeDL(ydl_opts) as ydl:
try:
info = ydl.extract_info(url, download=False)
except:
except Exception as e:
print(f"Error downloading '{url}': {e}")
return []
if info is None:
return []
data = {'url': info['url'],
'title': info['title'],
'thumbnail': info['thumbnail'],
'duration': info['duration']} # Grab data
# Handle both direct videos and playlists with single entry
if 'entries' in info:
if len(info['entries']) == 0:
return []
info = info['entries'][0]
if 'url' not in info:
print(f"No URL found for: {url}")
return []
data = {
'url': info['url'],
'title': info.get('title', 'Unknown'),
'thumbnail': info.get('thumbnail', ''),
'duration': info.get('duration', 0)
}
return [data]
@@ -139,19 +177,26 @@ async def playlist_download(url):
with ytdlp.YoutubeDL(ydl_opts) as ydl:
try:
info = ydl.extract_info(url, download=False)
except:
except Exception as e:
print(f"Error downloading playlist '{url}': {e}")
return []
if info is None:
return []
info = info['entries'] # Grabbing all songs in playlist
info = info['entries'] # Grabbing all songs in playlist
urls = []
for song in info:
data = {'url': song['url'],
'title': song['title'],
'thumbnail': song['thumbnail'],
'duration': song['duration']} # Grab data
if song is None or 'url' not in song:
continue
data = {
'url': song['url'],
'title': song.get('title', 'Unknown'),
'thumbnail': song.get('thumbnail', ''),
'duration': song.get('duration', 0)
}
urls.append(data)
return urls