2024-02-12 13:20:31 +00:00
|
|
|
# plugins/youtube.py
|
|
|
|
|
|
|
|
import re
|
|
|
|
import logging
|
|
|
|
from pytube import YouTube
|
|
|
|
import simplematrixbotlib as botlib
|
|
|
|
|
|
|
|
async def handle_command(room, message, bot, PREFIX):
|
|
|
|
"""
|
|
|
|
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)
|
|
|
|
if match.is_not_from_this_bot() and re.search(r'youtube\.com/watch\?v=', message.body):
|
|
|
|
logging.info("YouTube link detected")
|
|
|
|
video_id_match = re.search(r'youtube\.com/watch\?v=([^\s]+)', message.body)
|
|
|
|
if video_id_match:
|
|
|
|
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 = video.length
|
|
|
|
views = video.views
|
|
|
|
author = video.author
|
2024-02-12 21:41:01 +00:00
|
|
|
info_message = f"""**🎬🎝 Title:** {title} | **Length**: {length} sec | **Views:** {views} | **Description:** {description}"""
|
2024-02-12 13:20:31 +00:00
|
|
|
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)}")
|
2024-02-12 21:41:01 +00:00
|
|
|
# await bot.api.send__message(room.room_id, "Error fetching YouTube video information.")
|