2024-03-01 03:18:58 +00:00
|
|
|
|
"""
|
2024-03-03 23:34:39 +00:00
|
|
|
|
Plugin for providing a command to search for YouTube videos in the room.
|
2024-03-01 03:18:58 +00:00
|
|
|
|
"""
|
2024-02-14 09:56:11 +00:00
|
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
import simplematrixbotlib as botlib
|
|
|
|
|
from youtube_search import YoutubeSearch
|
|
|
|
|
|
|
|
|
|
async def handle_command(room, message, bot, PREFIX, config):
|
2024-03-03 23:34:39 +00:00
|
|
|
|
"""
|
|
|
|
|
Asynchronously handles the command to search for YouTube videos in the room.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
room (Room): The Matrix room where the command was invoked.
|
|
|
|
|
message (RoomMessage): The message object containing the command.
|
|
|
|
|
bot (MatrixBot): The Matrix bot instance.
|
|
|
|
|
PREFIX (str): The command prefix.
|
|
|
|
|
config (dict): The bot's configuration.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
None
|
|
|
|
|
"""
|
2024-02-14 09:56:11 +00:00
|
|
|
|
match = botlib.MessageMatch(room, message, bot, PREFIX)
|
|
|
|
|
if match.is_not_from_this_bot() and match.prefix() and match.command("yt"):
|
|
|
|
|
args = match.args()
|
|
|
|
|
if len(args) < 1:
|
|
|
|
|
await bot.api.send_text_message(room.room_id, "Usage: !yt <search terms>")
|
|
|
|
|
else:
|
|
|
|
|
search_terms = " ".join(args)
|
|
|
|
|
logging.info(f"Performing YouTube search for: {search_terms}")
|
2024-03-03 23:34:39 +00:00
|
|
|
|
results = YoutubeSearch(search_terms, max_results=10).to_dict()
|
2024-02-14 09:56:11 +00:00
|
|
|
|
if results:
|
|
|
|
|
output = generate_output(results)
|
|
|
|
|
await send_collapsible_message(room, bot, output)
|
|
|
|
|
else:
|
|
|
|
|
await bot.api.send_text_message(room.room_id, "No results found.")
|
|
|
|
|
|
|
|
|
|
def generate_output(results):
|
2024-03-03 23:34:39 +00:00
|
|
|
|
"""
|
|
|
|
|
Generates HTML output for displaying YouTube search results.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
results (list): A list of dictionaries containing information about YouTube videos.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
str: HTML formatted output containing YouTube search results.
|
|
|
|
|
"""
|
2024-02-14 09:56:11 +00:00
|
|
|
|
output = ""
|
|
|
|
|
for video in results:
|
|
|
|
|
output += f'<a href="https://www.youtube.com/watch?v={video["id"]}">'
|
|
|
|
|
output += f'<img src="{video["thumbnails"][0]}"></img><br>'
|
|
|
|
|
output += f'<strong>{video["title"]}</strong><br>'
|
|
|
|
|
output += f'Length: {video["duration"]} | Views: {video["views"]}<br>'
|
|
|
|
|
if video["long_desc"]:
|
|
|
|
|
output += f'Description: {video["long_desc"]}<br>'
|
|
|
|
|
output += "</a><br>"
|
|
|
|
|
return output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def send_collapsible_message(room, bot, content):
|
2024-03-03 23:34:39 +00:00
|
|
|
|
"""
|
|
|
|
|
Sends a collapsible message containing YouTube search results to the room.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
room (Room): The Matrix room where the message will be sent.
|
|
|
|
|
bot (MatrixBot): The Matrix bot instance.
|
|
|
|
|
content (str): HTML content to be included in the collapsible message.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
None
|
|
|
|
|
"""
|
2024-02-14 09:56:11 +00:00
|
|
|
|
message = f'<details><summary><strong>🍄Funguy ▶YouTube Search🍄<br>⤵︎Click Here To See Results⤵︎</strong></summary>{content}</details>'
|
|
|
|
|
await bot.api.send_markdown_message(room.room_id, message)
|