From 803acf514bac8485f8bfed45dd36cb0ceae76f02 Mon Sep 17 00:00:00 2001 From: Hash Borgir Date: Fri, 1 Mar 2024 09:20:44 -0700 Subject: [PATCH] switched to using pytubefix --- plugins/youtube-preview.py | 56 ++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 24 deletions(-) diff --git a/plugins/youtube-preview.py b/plugins/youtube-preview.py index 878b199..ab0bdd6 100644 --- a/plugins/youtube-preview.py +++ b/plugins/youtube-preview.py @@ -6,25 +6,31 @@ This plugin provides a command to fetch YouTube video information from links. import re import logging -from pytube import YouTube +from pytubefix import YouTube import simplematrixbotlib as botlib +import asyncio def seconds_to_minutes_seconds(seconds): minutes = seconds // 60 seconds %= 60 return f"{minutes:02d}:{seconds:02d}" +async def fetch_youtube_info(youtube_url): + try: + video = YouTube(youtube_url) + title = video.title + description = video.description + length = seconds_to_minutes_seconds(video.length) + views = video.views + author = video.author + description_with_breaks = description.replace('\n', '
') + info_message = f"""🎬🎝 Title: {title} | Length: {length} minutes | Views: {views}\n
⤵︎Click Here For Description⤵︎{description_with_breaks}
""" + return info_message + except Exception as e: + logging.error(f"Error fetching YouTube video information: {str(e)}") + return None + async def handle_command(room, message, bot, prefix, config): - """ - Function to handle YouTube video information from links. - - Args: - room (Room): The Matrix room where the command was invoked. - message (RoomMessage): The message object containing the command. - - Returns: - None - """ match = botlib.MessageMatch(room, message, bot, prefix) if match.is_not_from_this_bot() and re.search(r'youtube\.com/watch\?v=', message.body): logging.info("YouTube link detected") @@ -33,16 +39,18 @@ async def handle_command(room, message, bot, prefix, config): video_id = video_id_match.group(1) youtube_url = f"https://www.youtube.com/watch?v={video_id}" logging.info(f"Fetching information for YouTube video: {youtube_url}") - try: - video = YouTube(youtube_url) - title = video.title - description = video.description - length = seconds_to_minutes_seconds(video.length) - views = video.views - author = video.author - info_message = f"""**🎬🎝 Title:** {title} | **Length**: {length} minutes| **Views:** {views} | **Description:** {description}""" - await bot.api.send_markdown_message(room.room_id, info_message) - logging.info("Sent YouTube video information to the room") - except Exception as e: - logging.error(f"Error fetching YouTube video information: {str(e)}") - # await bot.api.send__message(room.room_id, "Error fetching YouTube video information.") + retry_count = 3 + while retry_count > 0: + info_message = await fetch_youtube_info(youtube_url) + if info_message: + await bot.api.send_markdown_message(room.room_id, info_message) + logging.info("Sent YouTube video information to the room") + break + else: + logging.info("Retrying...") + retry_count -= 1 + await asyncio.sleep(1) # wait for 1 second before retrying + else: + logging.error("Failed to fetch YouTube video information after retries") + +