Update plugins, refactor proxy plugin, yt preview now gets lyrics
This commit is contained in:
@@ -6,8 +6,13 @@ Plugin for providing a command to fetch YouTube video information from links.
|
||||
import re
|
||||
import logging
|
||||
import asyncio
|
||||
import aiohttp
|
||||
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/{}"
|
||||
|
||||
|
||||
def seconds_to_minutes_seconds(seconds):
|
||||
"""
|
||||
@@ -23,6 +28,28 @@ def seconds_to_minutes_seconds(seconds):
|
||||
seconds %= 60
|
||||
return f"{minutes:02d}:{seconds:02d}"
|
||||
|
||||
|
||||
async def fetch_lyrics(artist):
|
||||
"""
|
||||
Asynchronously fetches lyrics for a song from the Lyricist API.
|
||||
|
||||
Args:
|
||||
artist (str): The name of the artist.
|
||||
|
||||
Returns:
|
||||
str: Lyrics of the song.
|
||||
None if an error occurs during fetching.
|
||||
"""
|
||||
try:
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.get(LYRICIST_API_URL.format(artist)) as response:
|
||||
data = await response.json()
|
||||
return data.get("lyrics")
|
||||
except Exception as e:
|
||||
logging.error(f"Error fetching lyrics: {str(e)}")
|
||||
return None
|
||||
|
||||
|
||||
async def fetch_youtube_info(youtube_url):
|
||||
"""
|
||||
Asynchronously fetches information about a YouTube video.
|
||||
@@ -37,17 +64,27 @@ async def fetch_youtube_info(youtube_url):
|
||||
try:
|
||||
video = YouTube(youtube_url)
|
||||
title = video.title
|
||||
artist, song = get_artist_title(title)
|
||||
|
||||
description = video.description
|
||||
length = seconds_to_minutes_seconds(video.length)
|
||||
views = video.views
|
||||
author = video.author
|
||||
description_with_breaks = description.replace('\n', '<br>')
|
||||
info_message = f"""<strong>🎬🎝 Title:</strong> {title} | <strong>Length</strong>: {length} minutes | <strong>Views</strong>: {views}\n<details><summary><strong>⤵︎Click Here For Description⤵︎</strong></summary>{description_with_breaks}</details>"""
|
||||
|
||||
# Fetching lyrics
|
||||
lyrics = await fetch_lyrics(artist)
|
||||
lyrics = lyrics.replace('\n', "<br>")
|
||||
|
||||
info_message = f"""<strong>🎬🎝 Title:</strong> {title} | <strong>Length</strong>: {length} minutes | <strong>Views</strong>: {views}\n<details><summary><strong>⤵︎Description⤵︎</strong></summary>{description_with_breaks}</details>"""
|
||||
if lyrics:
|
||||
info_message += f"<br><details><summary><strong>🎵 Lyrics:</strong></summary><br>{lyrics}</details>"
|
||||
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):
|
||||
"""
|
||||
Asynchronously handles the command to fetch YouTube video information.
|
||||
|
Reference in New Issue
Block a user