From 145beacce81f02e5c49602e3f7528c7bde203c7f Mon Sep 17 00:00:00 2001 From: Rafael Vargas Date: Tue, 28 Dec 2021 19:15:05 -0400 Subject: [PATCH] Changing the logic of a Song creation, there will be two steps --- vulkanbot/music/Song.py | 78 +++++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 41 deletions(-) diff --git a/vulkanbot/music/Song.py b/vulkanbot/music/Song.py index 4cabce5..355c69a 100644 --- a/vulkanbot/music/Song.py +++ b/vulkanbot/music/Song.py @@ -1,24 +1,25 @@ -import discord -import datetime -from config import config +from discord.embeds import Embed class Song(): """Store the usefull information about a Song""" - def __init__(self, info: dict) -> None: - if type(info) != dict: - return + def __init__(self, url: str, title: str) -> None: + """Create a song with only the URL to the youtube song""" + self.__url = url + self.__title = title + self.__info = {} - self.__usefull_keys = ['url', 'title', 'duration', + def finish_down(self, info: dict) -> None: + """Get and store the full information of the song""" + self.__usefull_keys = ['url', 'duration', 'description', 'webpage_url', 'channel', 'id', 'uploader', 'thumbnail'] self.__extract_info(info) - def __extract_info(self, info): + def __extract_info(self, info) -> None: """Extract the usefull information returned by the Downloader""" - self.__info = {} for key in self.__usefull_keys: try: self.__info[key] = info[key] @@ -26,43 +27,38 @@ class Song(): print(e) raise e + def embed(self) -> Embed: + """Configure and return the info to create the embed for this song""" + info = { + 'title': self.__title, + 'url': self.__url, + 'uploader': self.__info['uploader'] + } + + if 'thumbnail' in self.__info.keys(): + info['thumbnail'] = self.__info['thumbnail'] + + if 'duration' in self.__info.keys(): + info['duration'] = self.__info['duration'] + + return info + @property - def info(self): + def info(self) -> dict: """Return the compiled info of this song""" - return self.__info + if self.__info: + return self.__info @property - def title(self): - return self.__info['title'] + def title(self) -> str: + return self.__title @property - def source(self): + def source(self) -> str: """Return the Song Source URL to play""" - return self.__info['url'] + if 'url' in self.__info.keys(): + return self.__info['url'] - def embed(self): - """Configure and return the embed to show this song in discord chat""" - embed = discord.Embed(title='Music Playing', - description=f"[{self.__info['title']}]({self.__info['webpage_url']})", - color=config.COLOURS['blue']) - - if self.thumbnail is not None: - embed.set_thumbnail(url=self.thumbnail) - - embed.add_field(name=config.SONGINFO_UPLOADER, - value=self.__info['uploader'], - inline=False) - - if self.duration is not None: - duration = str(datetime.timedelta(seconds=self.__info['duration'])) - - embed.add_field(name=config.SONGINFO_DURATION, - value=f"{duration}", - inline=False) - - else: - embed.add_field(name=config.SONGINFO_DURATION, - value=config.SONGINFO_UNKNOWN_DURATION, - inline=False) - - return embed + @property + def url(self) -> str: + return self.__url