From e1f1fedfdf72dbc5be6ff3974c3f7697c5b94e57 Mon Sep 17 00:00:00 2001 From: Lukas Blacha Date: Wed, 8 Feb 2023 12:54:36 +0100 Subject: [PATCH] Added youtube_search method --- Music-Bot.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/Music-Bot.py b/Music-Bot.py index b21b0c3..1967cd6 100644 --- a/Music-Bot.py +++ b/Music-Bot.py @@ -94,11 +94,24 @@ else: async with aiohttp.ClientSession() as client: async with client.get("https://www.youtube.com/results", params=p, headers=h) as resp: dom = await resp.text() - open("debug.html", "w").write(dom) + open("yt_debug.html", "w").write(dom) found = re.findall(r"watch\?v=(\S{11})", dom)[0] url = f"https://www.youtube.com/watch?v={found}" return url + async def soundcloud_search(search: str): + p = {"search_query": search} + # Spoof a user agent header or the request will immediately fail + h = {"User-Agent": "Mozilla/5.0"} + async with aiohttp.ClientSession() as client: + async with client.get("https://soundcloud.com/search", params=p, headers=h) as resp: + dom = await resp.text() + open("sc_debug.html", "w").write(dom) + found = re.findall(r"(\S{11})", dom)[0] + url = f"https://soundcloud.com/{found}" + print(url) + return url + intents = discord.Intents.default() intents.message_content = True @@ -232,6 +245,12 @@ else: await ctx.send(f"{success}Now playing: `{player.title}`\nRequested by {ctx.author.mention}") + @bot.slash_command() + async def soundcloud(ctx, *, search: str): + url = await soundcloud_search(search=search) + await ctx.respond(url) + + @play.before_invoke @stream.before_invoke @youtube.before_invoke