From 7ffc356b1cf3e5f2dd8f36b79a68837681b22b4f Mon Sep 17 00:00:00 2001 From: Lukas Blacha Date: Wed, 8 Feb 2023 12:14:00 +0100 Subject: [PATCH] Changed Google-Search to Youtube (fix) --- Music-Bot.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/Music-Bot.py b/Music-Bot.py index fd396a2..c75f66d 100644 --- a/Music-Bot.py +++ b/Music-Bot.py @@ -1,6 +1,9 @@ import asyncio +import re from datetime import timedelta from urllib.parse import quote_plus + +import aiohttp import yt_dlp import discord import logging @@ -89,7 +92,18 @@ else: super().__init__() # We need to quote the query string to make a valid url. Discord will raise an error if it isn't valid. query = quote_plus(query) - url = f"https://www.youtube.com/results?search_query={query}" + #url = f"https://www.youtube.com/results?search_query={query}" + + p = {"search_query": query} + # 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://www.youtube.com/results", params=p, headers=h) as resp: + dom = await resp.text() + # open("debug.html", "w").write(dom) + found = re.findall(r'href"\/watch\?v=([a-zA-Z0-9_-]{11})', dom) + url = f"https://youtu.be/{found[0]}" + # Link buttons cannot be made with the # decorator, so we have to manually create one.