Changed Google-Search to Youtube (fix)
This commit is contained in:
16
Music-Bot.py
16
Music-Bot.py
@@ -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.
|
||||||
|
|||||||
Reference in New Issue
Block a user