Changing the logic of a Song creation, there will be two steps

This commit is contained in:
Rafael Vargas
2021-12-28 19:15:05 -04:00
parent eb19a95f84
commit 145beacce8

View File

@@ -1,24 +1,25 @@
import discord from discord.embeds import Embed
import datetime
from config import config
class Song(): class Song():
"""Store the usefull information about a Song""" """Store the usefull information about a Song"""
def __init__(self, info: dict) -> None: def __init__(self, url: str, title: str) -> None:
if type(info) != dict: """Create a song with only the URL to the youtube song"""
return 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', 'description', 'webpage_url',
'channel', 'id', 'uploader', 'channel', 'id', 'uploader',
'thumbnail'] 'thumbnail']
self.__extract_info(info) self.__extract_info(info)
def __extract_info(self, info): def __extract_info(self, info) -> None:
"""Extract the usefull information returned by the Downloader""" """Extract the usefull information returned by the Downloader"""
self.__info = {}
for key in self.__usefull_keys: for key in self.__usefull_keys:
try: try:
self.__info[key] = info[key] self.__info[key] = info[key]
@@ -26,43 +27,38 @@ class Song():
print(e) print(e)
raise 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 @property
def info(self): def info(self) -> dict:
"""Return the compiled info of this song""" """Return the compiled info of this song"""
return self.__info if self.__info:
return self.__info
@property @property
def title(self): def title(self) -> str:
return self.__info['title'] return self.__title
@property @property
def source(self): def source(self) -> str:
"""Return the Song Source URL to play""" """Return the Song Source URL to play"""
return self.__info['url'] if 'url' in self.__info.keys():
return self.__info['url']
def embed(self): @property
"""Configure and return the embed to show this song in discord chat""" def url(self) -> str:
embed = discord.Embed(title='Music Playing', return self.__url
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