From 337f4abfcd3800991f90a4623be019a16fbda02b Mon Sep 17 00:00:00 2001 From: Peter van Arkel Date: Fri, 24 Mar 2023 10:04:50 +0100 Subject: [PATCH] rename cog to preview.py and add youtube preview functionality --- cogs/{gif.py => preview.py} | 37 +++++++++++++++++++++++++++++++++---- jeevesbot/env.py.dist | 2 +- jeevesbot/functions.py | 2 +- requirements.txt | 1 + 4 files changed, 36 insertions(+), 6 deletions(-) rename cogs/{gif.py => preview.py} (59%) diff --git a/cogs/gif.py b/cogs/preview.py similarity index 59% rename from cogs/gif.py rename to cogs/preview.py index 56721e3..3d58fdc 100644 --- a/cogs/gif.py +++ b/cogs/preview.py @@ -2,7 +2,9 @@ import discord from discord.ext import commands from jeevesbot import functions from logging import getLogger - +import re +import pytube +from pytube.exceptions import RegexMatchError # setup logging log = getLogger(__name__) @@ -11,11 +13,12 @@ log = getLogger(__name__) e = discord.Embed() -class Gif(commands.Cog): - """ Ensures that high-risk channels don't display embedded links, but only gifs.""" +class Preview(commands.Cog): + """ Ensures that high-risk channels don't display embedded links, but only gifs and youtube previews.""" def __init__(self, bot): self.bot = bot + self.video_id_regex = re.compile(r'(?:youtube\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/|m\.youtube\.com/(?:watch\?v=|embed/|v/))([^"&?/ ]{11})') @commands.Cog.listener() @@ -55,6 +58,32 @@ class Gif(commands.Cog): await message.channel.send(embed=embed) logline = (str(message.author) + ' requested a gif: ' + str(gif_url)) log.info(logline) + if 'https://youtu' or 'https://m.youtu' or 'https://www.youtu' in message.content(): + roles = functions.checkrole(message.author.roles) + channel = functions.checkchannel(message.channel.id) + if channel is True: + if roles is not True: + url = message.content + youtube = pytube.YouTube(url) + video_title = youtube.title + video_author = youtube.author + video_id = self.extract_video_id(url) + if video_id: + embed = discord.Embed() + embed.set_image(url=f"https://img.youtube.com/vi/{video_id}/hqdefault.jpg") + embed.set_author(name=f"{video_author}") + embed.add_field(name=f"", value=f"[{video_title}]({url})") + await message.channel.send(embed=embed) + log.info(f'User {message.author} requested preview for {url}') + + + def extract_video_id(self, url): + try: + match = self.video_id_regex.search(url) + if match: + return match.group(1) + except pytube.exceptions.RegexMatchError as e: + pass @commands.Cog.listener() @@ -63,4 +92,4 @@ class Gif(commands.Cog): async def setup(bot): - await bot.add_cog(Gif(bot)) \ No newline at end of file + await bot.add_cog(Preview(bot)) \ No newline at end of file diff --git a/jeevesbot/env.py.dist b/jeevesbot/env.py.dist index dbb6a0f..80ef7c6 100644 --- a/jeevesbot/env.py.dist +++ b/jeevesbot/env.py.dist @@ -2,7 +2,7 @@ TOKEN = 'discord-bot-token-here' ADMIN_ROLE = 'role-to-exclude-from-gifbot' -GIFCHANNELS = [add-channel-ids-for-bot-to-work-in] +PREVIEWCHANNELS = [add-channel-ids-for-bot-to-work-in] BOTCHANNEL = [add id of bot channel] FLAIRROLES = [add list of role ids to correspond to the emoji] FLAIRROLE = [add id of flair separator role] \ No newline at end of file diff --git a/jeevesbot/functions.py b/jeevesbot/functions.py index f1af938..9e0a838 100644 --- a/jeevesbot/functions.py +++ b/jeevesbot/functions.py @@ -25,7 +25,7 @@ def checkrole(roles): # check if the source channel is in the list of channels that are watched by the bot. def checkchannel(channelid): - if channelid in env.GIFCHANNELS: + if channelid in env.PREVIEWCHANNELS: return True else: return False diff --git a/requirements.txt b/requirements.txt index 4c76b47..42ef7bd 100644 --- a/requirements.txt +++ b/requirements.txt @@ -11,6 +11,7 @@ pylint gspread oauth2client Pillow +pytube # needs this version, otherwise TypeErrors will break stuff yarl==1.4.2