From 23c8d99d33df88349ef3c086ae367c4a04c484a7 Mon Sep 17 00:00:00 2001 From: Rafael Vargas Date: Tue, 28 Dec 2021 19:15:46 -0400 Subject: [PATCH] Adding new functions to Playlist module --- vulkanbot/music/Playlist.py | 156 ++++++++++++++++++++++++++---------- 1 file changed, 113 insertions(+), 43 deletions(-) diff --git a/vulkanbot/music/Playlist.py b/vulkanbot/music/Playlist.py index d749c08..b541ad4 100644 --- a/vulkanbot/music/Playlist.py +++ b/vulkanbot/music/Playlist.py @@ -1,43 +1,108 @@ from collections import deque import random -from Song import Song +from vulkanbot.music.Song import Song +from vulkanbot.music.Downloader import Downloader class Playlist(): """Class to manage and control the songs to play and played""" def __init__(self) -> None: + self.__down = Downloader() self.__queue = deque() # Store the musics to play self.__songs_history = deque() # Store the musics played self.__name_history = deque() # Store the name of musics played - self.__loop_one = False - self.__loop_all = False + self.__looping_one = False + self.__looping_all = False self.__current = None + @property + def looping_one(self): + return self.__looping_one + + @property + def looping_all(self): + return self.__looping_all + def __len__(self): - return len(self.queue) + if self.__looping_one == True or self.__looping_all == True: + return 1 + else: + return len(self.__queue) def next_song(self): """Return the source of the next song to play""" - if self.__current == None: - print('Nenhuma música tocando') - return None - - if self.__loop_one: # Insert the current song to play again - self.__queue.appendleft(self.__current) - - if self.__loop_all: # Insert the current song in the end of queue - self.__queue.append(self.__current) - - if len(self.__queue) == 0: # If nothing no more song to play, return - return None + if self.__current == None: # If not playing + if len(self.__queue) == 0: # If nothing to play + return None + else: # If there is music to play + return self.__start() + # If playing played_song = self.__current - # Add played song name to history - self.__name_history.append(played_song.title) - return self.__queue[0] # Return the next song to play + + # Check if need to repeat the played song + if self.__looping_one: # Insert the current song to play again + self.__queue.appendleft(played_song) + + if self.__looping_all: # Insert the current song in the end of queue + self.__queue.append(played_song) + + while True: # Try to get the source of next song + if len(self.__queue) == 0: # If no more song to play, return None + return None + + # If there is more to play + # Finish download of the next song + source = self.__prepare_next(self.__queue[0]) + if source == None: # If there is a problem in the download + self.__queue.popleft() # Remove the music with problems + continue + + return source + + def get_current(self): + """Return current music embed""" + if self.__current: + return self.__current.embed() + else: + return 'Nenhuma música tocando' + + def __prepare_next(self, next_song: Song) -> str: + """Finish the download of the music and return the source""" + if next_song.source == None: # Check if source has already downloaded + url = next_song.url # Get the URL + info = self.__down.download_source(url) # Download the source + if info == None: # If there is a problem in the download + return None + + next_song.finish_down(info) # Updating the info of song + + # Att the Playlist info + self.__current = next_song # Att the current + self.__queue.popleft() # Remove the current from queue + self.__name_history.append(self.__current.title) # Add to name history + self.__songs_history.append(self.__current) # Add to song history + + return self.__current.source # Return the source of current + + def __start(self) -> None: + """Start the play of the first musics and return his source""" + # Finish download of the next song + url = self.__queue[0].url # Get the URL + info = self.__down.download_source(url) # Download the source + + self.__queue[0].finish_down(info) # Att the song + + # Att Playlist info + self.__current = self.__queue[0] # Att the current + self.__queue.popleft() # Remove the current from queue + self.__name_history.append(self.__current.title) # Add to name history + self.__songs_history.append(self.__current) # Add to song history + + return self.__current.source # Return the source of current def prev_song(self): """Return the source of the last song played @@ -49,19 +114,20 @@ class Playlist(): else: return self.__songs_history[0].source - def add_song(self, song: Song) -> None: - """Receives a song object and store to the play queue""" - if type(song) != Song: - print('Song type invalid') + def add_song(self, music: dict) -> None: + """Receives a music object and store to the play queue""" + if (not 'title' in music.keys()) or (not 'url' in music.keys()): + print('Music without necessary keys') return + song = Song(title=music['title'], url=music['url']) # Cria a musica self.__queue.append(song) - def shuffle(self): + def shuffle(self) -> None: """Shuffle the order of the songs to play""" random.shuffle(self.__queue) - def revert(self): + def revert(self) -> None: """Revert the order of the songs to play""" self.__queue.reverse() @@ -70,40 +136,44 @@ class Playlist(): self.__queue.clear() self.__songs_history.clear() - def play(self): - """Start the play of the first musics and return his source""" - if len(self.__queue) == 0: - print('Nenhuma música para tocar') - return None - - self.__current = self.__queue[0] - return self.__queue[0].source - def loop_one(self) -> str: """Try to start the loop of the current song Return: Embed descrition to show to user """ - if self.__loop_all == True: + if self.__looping_all == True: return 'Vulkan already looping one music, disable loop first' - elif self.__loop_one == True: + elif self.__looping_one == True: return "I'm already doing this, you dumb ass" else: - self.__loop_one = True + self.__looping_one = True + return 'Repeating the current song' def loop_all(self) -> str: """Try to start the loop of all songs Return: Embed descrition to show to user """ - if self.__loop_one == True: + if self.__looping_one == True: return 'Vulkan already looping one music, disable loop first' - elif self.__loop_all == True: + elif self.__looping_all == True: return "I'm already doing this, you dumb ass" else: - self.__loop_all = True + self.__looping_all = True + return 'Repeating all songs in queue' - def loop_off(self) -> None: + def loop_off(self) -> str: """Disable both types of loop""" - self.__loop_all = False - self.__loop_one = False + if self.__looping_all == False and self.__looping_one == False: + return "The loop is already off, you fucking dick head" + + self.__looping_all = False + self.__looping_one = False + return 'Loop disable' + + def queue(self) -> list: + list_songs = [] + for song in self.__queue: + title = song.title + list_songs.append(title) + return list_songs