From 5d746027e2409db23d0b22f5337d70b9ef8d4ca9 Mon Sep 17 00:00:00 2001 From: Hash Borgir Date: Sun, 10 Mar 2024 05:58:21 -0600 Subject: [PATCH] unload plugin loadplugin.py. youtube-preview now gets lyrics. --- plugins/loadplugin.py | 88 ++++++++++++++++++++++++++++---------- plugins/youtube-preview.py | 10 +++-- requirements.txt | 2 +- 3 files changed, 73 insertions(+), 27 deletions(-) diff --git a/plugins/loadplugin.py b/plugins/loadplugin.py index 49a69a2..fdaf95f 100644 --- a/plugins/loadplugin.py +++ b/plugins/loadplugin.py @@ -6,6 +6,7 @@ import os import logging import importlib import simplematrixbotlib as botlib +import sys # Import sys module for unloading plugins # Dictionary to store loaded plugins PLUGINS = {} @@ -32,9 +33,33 @@ async def load_plugin(plugin_name): logging.error(f"Error loading plugin {plugin_name}: {e}") return False +async def unload_plugin(plugin_name): + """ + Asynchronously unloads a plugin. + + Args: + plugin_name (str): The name of the plugin to unload. + + Returns: + bool: True if the plugin is unloaded successfully, False otherwise. + """ + try: + if plugin_name in PLUGINS: + del PLUGINS[plugin_name] # Remove the plugin from the PLUGINS dictionary + del sys.modules[f"plugins.{plugin_name}"] # Unload the plugin module from sys.modules + logging.info(f"Unloaded plugin: {plugin_name}") + return True + else: + logging.warning(f"Plugin '{plugin_name}' is not loaded") + return False + except Exception as e: + # Log an error if the plugin fails to unload + logging.error(f"Error unloading plugin {plugin_name}: {e}") + return False + async def handle_command(room, message, bot, prefix, config): """ - Asynchronously handles the command to load a plugin. + Asynchronously handles the command to load or unload a plugin. Args: room (Room): The Matrix room where the command was invoked. @@ -47,25 +72,44 @@ async def handle_command(room, message, bot, prefix, config): None """ match = botlib.MessageMatch(room, message, bot, prefix) - if match.is_not_from_this_bot() and match.prefix() and match.command("load"): - # Check if the sender is the admin - if str(message.sender) == config.admin_user: - args = match.args() - if len(args) != 1: - # Send usage message if the command format is incorrect - await bot.api.send_text_message(room.room_id, "Usage: !load ") - else: - plugin_name = args[0] - # Check if the plugin is not already loaded - if plugin_name not in PLUGINS: - # Load the plugin - success = await load_plugin(plugin_name) - if success: - await bot.api.send_text_message(room.room_id, f"Plugin '{plugin_name}' loaded successfully") - else: - await bot.api.send_text_message(room.room_id, f"Error loading plugin '{plugin_name}'") + if match.is_not_from_this_bot() and match.prefix(): + command = match.command() + if command == "load": + if str(message.sender) == config.admin_user: + args = match.args() + if len(args) != 1: + # Send usage message if the command format is incorrect + await bot.api.send_text_message(room.room_id, "Usage: !load ") else: - await bot.api.send_text_message(room.room_id, f"Plugin '{plugin_name}' is already loaded") - else: - # Send unauthorized message if the sender is not the admin - await bot.api.send_text_message(room.room_id, "You are not authorized to load plugins.") + plugin_name = args[0] + # Check if the plugin is not already loaded + if plugin_name not in PLUGINS: + # Load the plugin + success = await load_plugin(plugin_name) + if success: + await bot.api.send_text_message(room.room_id, f"Plugin '{plugin_name}' loaded successfully") + else: + await bot.api.send_text_message(room.room_id, f"Error loading plugin '{plugin_name}'") + else: + await bot.api.send_text_message(room.room_id, f"Plugin '{plugin_name}' is already loaded") + else: + # Send unauthorized message if the sender is not the admin + await bot.api.send_text_message(room.room_id, "You are not authorized to load plugins.") + elif command == "unload": + if str(message.sender) == config.admin_user: + args = match.args() + if len(args) != 1: + # Send usage message if the command format is incorrect + await bot.api.send_text_message(room.room_id, "Usage: !unload ") + else: + plugin_name = args[0] + # Unload the plugin + success = await unload_plugin(plugin_name) + if success: + await bot.api.send_text_message(room.room_id, f"Plugin '{plugin_name}' unloaded successfully") + else: + await bot.api.send_text_message(room.room_id, f"Error unloading plugin '{plugin_name}'") + else: + # Send unauthorized message if the sender is not the admin + await bot.api.send_text_message(room.room_id, "You are not authorized to unload plugins.") + diff --git a/plugins/youtube-preview.py b/plugins/youtube-preview.py index 477ed63..1cac89a 100644 --- a/plugins/youtube-preview.py +++ b/plugins/youtube-preview.py @@ -11,7 +11,7 @@ from pytubefix import YouTube import simplematrixbotlib as botlib from youtube_title_parse import get_artist_title -LYRICIST_API_URL = "https://lyrist.vercel.app/api/song/{}" +LYRICIST_API_URL = "https://lyrist.vercel.app/api/{}" def seconds_to_minutes_seconds(seconds): @@ -29,11 +29,12 @@ def seconds_to_minutes_seconds(seconds): return f"{minutes:02d}:{seconds:02d}" -async def fetch_lyrics(artist): +async def fetch_lyrics(song, artist): """ Asynchronously fetches lyrics for a song from the Lyricist API. Args: + song (str): The name of the song. artist (str): The name of the artist. Returns: @@ -42,7 +43,7 @@ async def fetch_lyrics(artist): """ try: async with aiohttp.ClientSession() as session: - async with session.get(LYRICIST_API_URL.format(artist)) as response: + async with session.get(LYRICIST_API_URL.format(song, artist)) as response: data = await response.json() return data.get("lyrics") except Exception as e: @@ -50,6 +51,7 @@ async def fetch_lyrics(artist): return None + async def fetch_youtube_info(youtube_url): """ Asynchronously fetches information about a YouTube video. @@ -73,7 +75,7 @@ async def fetch_youtube_info(youtube_url): description_with_breaks = description.replace('\n', '
') # Fetching lyrics - lyrics = await fetch_lyrics(artist) + lyrics = await fetch_lyrics(song, artist) lyrics = lyrics.replace('\n', "
") info_message = f"""🎬🎝 Title: {title} | Length: {length} minutes | Views: {views}\n
⤵︎Description⤵︎{description_with_breaks}
""" diff --git a/requirements.txt b/requirements.txt index 269f3ee..8c8a64c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,5 +8,5 @@ watchdog emoji python-slugify youtube_title_parse - +dnspython