Changed Google-Search to Youtube (fix)

This commit is contained in:
Lukas Blacha
2023-02-08 12:14:00 +01:00
parent 71bd1f6ae5
commit 7ffc356b1c

View File

@@ -1,6 +1,9 @@
import asyncio import asyncio
import re
from datetime import timedelta from datetime import timedelta
from urllib.parse import quote_plus from urllib.parse import quote_plus
import aiohttp
import yt_dlp import yt_dlp
import discord import discord
import logging import logging
@@ -89,7 +92,18 @@ else:
super().__init__() super().__init__()
# We need to quote the query string to make a valid url. Discord will raise an error if it isn't valid. # 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) 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 # Link buttons cannot be made with the
# decorator, so we have to manually create one. # decorator, so we have to manually create one.