|
| 1 | +import re |
| 2 | + |
| 3 | +from discord.ext.commands import Cog |
| 4 | +from discord.commands import slash_command, SlashCommandGroup, option |
| 5 | +from discord.embeds import Embed |
| 6 | +import aiohttp |
| 7 | + |
| 8 | +from bs4 import BeautifulSoup |
| 9 | + |
| 10 | +from lib.bot import Bot |
| 11 | +from lib.context import CustomContext |
| 12 | + |
| 13 | + |
| 14 | +class Youtube(Cog): |
| 15 | + def __init__(self, bot): |
| 16 | + self.bot: Bot = bot |
| 17 | + |
| 18 | + youtube = SlashCommandGroup('youtube') |
| 19 | + |
| 20 | + def check_channel(self, handle: str, message: str, guild_id: str): |
| 21 | + async with aiohttp.ClientSession() as session: |
| 22 | + res = await session.get(f"https://youtube.com/@{handle}/videos") |
| 23 | + html = await res.text() |
| 24 | + try: |
| 25 | + latest_video_url = "https://www.youtube.com/watch?v=" + re.search('(?<="videoId":").*?(?=")', |
| 26 | + html).group() |
| 27 | + except AttributeError: |
| 28 | + latest_video_url = "" |
| 29 | + |
| 30 | + |
| 31 | + @youtube.command(name='notify', description='Get a ping when this user uploads') |
| 32 | + @option('handle', |
| 33 | + str, |
| 34 | + description='Youtube handle', |
| 35 | + required=True) |
| 36 | + @option('message', |
| 37 | + str, |
| 38 | + description='Message to send when video is uploaded', |
| 39 | + required=False) |
| 40 | + @option('channel', |
| 41 | + str, |
| 42 | + description='Channel to send announcement', |
| 43 | + required=False) |
| 44 | + async def youtube_notify(self, ctx: CustomContext, handle: str, message: str | None): |
| 45 | + async with aiohttp.ClientSession() as session: |
| 46 | + res = await session.get(f"https://youtube.com/@{handle}/videos") |
| 47 | + if res.status == 404: |
| 48 | + return await ctx.respond(embed=Embed(title=':x: | Channel not found', colour=0xff0000)) |
| 49 | + elif res.status not in range(200, 300): |
| 50 | + return await ctx.respond(embed=Embed(title=':x: | Something went wrong', colour=0xff0000)) |
| 51 | + |
| 52 | + user = self.bot.db.record("""SELECT * FROM youtube WHERE handle = ? AND guild_id = ?""", handle, ctx.guild_id) |
| 53 | + if user: |
| 54 | + return await ctx.respond(embed=Embed(title=':x: | Already watching for new videos', colour=0xff0000)) |
| 55 | + |
| 56 | + res = await session.get(f"https://youtube.com/@{handle}/videos") |
| 57 | + html = await res.text() |
| 58 | + |
| 59 | + try: |
| 60 | + latest_video_url = "https://www.youtube.com/watch?v=" + re.search('(?<="videoId":").*?(?=")', html).group() |
| 61 | + except AttributeError: |
| 62 | + latest_video_url = "" |
| 63 | + self.bot.db.execute("""INSERT INTO youtube VALUES (?,?,?,?)""", handle, ctx.guild_id, message if message else f"@{handle} has uploaded a video", latest_video_url) |
| 64 | + await ctx.respond(embed=Embed(title=f'✅ | Waiting for @{handle} to post more videos')) |
| 65 | + self.bot.tasks.add_job(id=f"{handle}|{ctx.guild_id}", args=(handle, message, ctx.guild_id,), trigger='interval', minutes=1) |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | +def setup(bot): |
| 76 | + bot.add_cog(Youtube(bot)) |
0 commit comments