diff --git a/youtube/info.json b/youtube/info.json new file mode 100644 index 0000000..8dc1ce7 --- /dev/null +++ b/youtube/info.json @@ -0,0 +1,7 @@ +{ + "AUTHOR" : "Paddolicious#8880 and aikaterna#1393", + "NAME" : "youtube", + "SHORT" : "Look up videos on YouTube.", + "DESCRIPTION" : "Look up videos on YouTube.", + "TAGS": ["youtube"] +} diff --git a/youtube/youtube.py b/youtube/youtube.py new file mode 100644 index 0000000..ed18ba6 --- /dev/null +++ b/youtube/youtube.py @@ -0,0 +1,41 @@ +from discord.ext import commands +import aiohttp +import re + + +class YouTube: + """Le YouTube Cog""" + def __init__(self, bot): + self.bot = bot + self.session = aiohttp.ClientSession() + + async def _youtube_results(self, query: str): + try: + headers = {"user-agent": "Red-cog/2.0"} + async with self.session.get("https://www.youtube.com/results", params={"search_query": query}, headers=headers) as r: + result = await r.text() + yt_find = re.findall(r"{\"videoId\":\"(.{11})", result) + url_list = [] + for track in yt_find: + url = "https://www.youtube.com/watch?v={}".format(track) + if url not in url_list: + url_list.append(url) + + except Exception as e: + url_list = ["Something went terribly wrong! [{}]".format(e)] + + return url_list + + @commands.command() + async def youtube(self, ctx, *, query: str): + """Search on Youtube.""" + result = await self._youtube_results(query) + if result: + await self.bot.say(result[0]) + else: + await self.bot.say("Nothing found. Try again later.") + + +def setup(bot): + n = YouTube(bot) + bot.add_cog(n)