Preps for youtube search selection

This commit is contained in:
Lukas Blacha
2023-02-10 15:18:24 +01:00
parent c3eb9a9d73
commit be7002bba5

View File

@@ -1,7 +1,7 @@
import asyncio import asyncio
import re import re
from datetime import timedelta from datetime import timedelta
from urllib.parse import quote_plus from pytube import YouTube
import aiohttp import aiohttp
import yt_dlp import yt_dlp
@@ -110,9 +110,10 @@ else:
async with aiohttp.ClientSession() as client: async with aiohttp.ClientSession() as client:
async with client.get("https://www.youtube.com/results", params=p, headers=h) as resp: async with client.get("https://www.youtube.com/results", params=p, headers=h) as resp:
dom = await resp.text() dom = await resp.text()
found = re.findall(r"watch\?v=(\S{11})", dom)[0] result = re.findall(r"watch\?v=(\S{11})", dom)[0:5]
url = f"https://www.youtube.com/watch?v={found}" print(result)
return url print("----")
return result
intents = discord.Intents.default() intents = discord.Intents.default()
@@ -242,7 +243,25 @@ else:
@bot.slash_command(name="youtube", description="Searches YouTube for a given string and plays the first result") @bot.slash_command(name="youtube", description="Searches YouTube for a given string and plays the first result")
async def youtube(ctx, *, search: str): async def youtube(ctx, *, search: str):
url = await youtube_search(search=search) results = await youtube_search(search=search)
embed = Embed(title=f"Search results for `{search}`", color=discord.Color.green())
counter = 1
for result in results:
url = f"https://youtube.com/watch?v={result}"
title = YouTube(url)
embed.add_field(name=f"{counter}", value=title.title, inline=False)
counter += 1
print(url)
await ctx.respond(f"{success}**A short selection of videos will be displayed shortly...**")
msg = await ctx.send(embed=embed)
await msg.add_reaction(discord.PartialEmoji(name='one'))
await msg.add_reaction('')
await msg.add_reaction('three')
await msg.add_reaction('four')
await msg.add_reaction('five')
"""
async with ctx.typing(): async with ctx.typing():
await ctx.respond("🤖 Your song is queued for download... please wait ", ephemeral=True) await ctx.respond("🤖 Your song is queued for download... please wait ", ephemeral=True)
try: try:
@@ -258,11 +277,12 @@ else:
await asyncio.sleep(3) await asyncio.sleep(3)
else: else:
await ctx.voice_client.disconnect() await ctx.voice_client.disconnect()
"""
@play.before_invoke @play.before_invoke
@stream.before_invoke @stream.before_invoke
@youtube.before_invoke #@youtube.before_invoke
async def ensure_voice(ctx: commands.Context): async def ensure_voice(ctx: commands.Context):
if ctx.voice_client is None: if ctx.voice_client is None:
if ctx.author.voice: if ctx.author.voice:
@@ -274,7 +294,7 @@ else:
"""
@bot.event @bot.event
async def on_application_command_error(ctx, error): async def on_application_command_error(ctx, error):
print(f"[on_application_command_error]\n{ctx.author}\n{error}") print(f"[on_application_command_error]\n{ctx.author}\n{error}")
@@ -299,5 +319,6 @@ else:
print(f"[on_command_error]\n{ctx.author}\n{error}") print(f"[on_command_error]\n{ctx.author}\n{error}")
embed = Embed(title=f"{error}", color=15158332) embed = Embed(title=f"{error}", color=15158332)
await ctx.send(embed=embed) await ctx.send(embed=embed)
"""
bot.run(token=args.token, reconnect=True) bot.run(token=args.token, reconnect=True)