From 1f380f60c612f01749442a5ed8d2f22ae9ae62aa Mon Sep 17 00:00:00 2001 From: mainaksamuel Date: Tue, 6 Jul 2021 19:00:11 +0300 Subject: [PATCH] Part2 - Implementation of Video Playlist --- python/src/video_player.py | 105 ++++++++++++++++++++++++++++------- python/src/video_playlist.py | 26 +++++++++ 2 files changed, 112 insertions(+), 19 deletions(-) diff --git a/python/src/video_player.py b/python/src/video_player.py index b57648d8..f4501f6f 100644 --- a/python/src/video_player.py +++ b/python/src/video_player.py @@ -2,14 +2,21 @@ from .video_library import VideoLibrary from .video import Video +from .video_playlist import Playlist + import enum class Messages(enum.Enum): - NOT_PLAYING = "No video is currently playing" - NOT_PAUSED = "Video is not paused" - NOT_EXISTS = "Video does not exist" - NOT_AVAILABLE = "No videos available" + NO_VIDEO_PLAYING = "No video is currently playing" + VIDEO_NOT_PAUSED = "Video is not paused" + VIDEO_NOT_EXISTS = "Video does not exist" + VIDEOS_NOT_AVAILABLE = "No videos available" + PLAYLIST_NAME_EXISTS = "A playlist with the same name already exists" + PLAYLIST_NOT_EXIST = "Playlist does not exist" + VIDEO_IN_PLAYLIST = "Video already added" + VIDEO_NOT_IN_PLAYLIST = "Video is not in playlist" + NO_VIDEOS_IN_PLAYLIST = "No videos here yet" class PlayingStatus(enum.Enum): @@ -25,6 +32,7 @@ def __init__(self): self._video_library = VideoLibrary() self.currently_playing = None self.playing_status = None + self.playlists = {} def number_of_videos(self): num_videos = len(self._video_library.get_all_videos()) @@ -49,7 +57,7 @@ def play_video(self, video_id): """ video = self._video_library.get_video(video_id) if not video: - print("Cannot play video:", Messages.NOT_EXISTS.value) + print("Cannot play video:", Messages.VIDEO_NOT_EXISTS.value) else: self.play_known_video(video) @@ -69,7 +77,7 @@ def play_known_video(self, video: Video): def stop_video(self): """Stops the current video.""" if self.playing_status == PlayingStatus.STOPPED or not self.currently_playing: - print("Cannot stop video:", Messages.NOT_PLAYING.value) + print("Cannot stop video:", Messages.NO_VIDEO_PLAYING.value) else: print("Stopping video:", self.currently_playing.title) self.playing_status = PlayingStatus.STOPPED @@ -82,7 +90,7 @@ def play_random_video(self): video = random.choice(self._video_library.get_all_videos()) self.play_known_video(video) except IndexError: - print(Messages.NOT_AVAILABLE.value) + print(Messages.VIDEOS_NOT_AVAILABLE.value) def pause_video(self): """Pauses the current video.""" @@ -92,7 +100,7 @@ def pause_video(self): elif self.playing_status == PlayingStatus.PAUSED: print("Video already paused:", self.currently_playing.title) else: - print("Cannot pause video:", Messages.NOT_PLAYING.value) + print("Cannot pause video:", Messages.NO_VIDEO_PLAYING.value) def continue_video(self): """Resumes playing the current video.""" @@ -100,14 +108,14 @@ def continue_video(self): print("Continuing video:", self.currently_playing.title) self.playing_status = PlayingStatus.PLAYING elif self.playing_status == PlayingStatus.STOPPED or not self.currently_playing: - print("Cannot continue video:", Messages.NOT_PLAYING.value) + print("Cannot continue video:", Messages.NO_VIDEO_PLAYING.value) else: - print("Cannot continue video:", Messages.NOT_PAUSED.value) + print("Cannot continue video:", Messages.VIDEO_NOT_PAUSED.value) def show_playing(self): """Displays video currently playing.""" if self.playing_status == PlayingStatus.STOPPED or not self.currently_playing: - print(Messages.NOT_PLAYING.value) + print(Messages.NO_VIDEO_PLAYING.value) if self.playing_status == PlayingStatus.PLAYING: print("Currently playing:", self.currently_playing.__str__()) @@ -121,7 +129,12 @@ def create_playlist(self, playlist_name): Args: playlist_name: The playlist name. """ - print("create_playlist needs implementation") + name = playlist_name.lower() + if not name in self.playlists: + self.playlists[name] = Playlist(playlist_name) + print("Successfully created new playlist:", self.playlists[name]) + else: + print("Cannot create playlist:", Messages.PLAYLIST_NAME_EXISTS.value) def add_to_playlist(self, playlist_name, video_id): """Adds a video to a playlist with a given name. @@ -130,12 +143,30 @@ def add_to_playlist(self, playlist_name, video_id): playlist_name: The playlist name. video_id: The video_id to be added. """ - print("add_to_playlist needs implementation") + video = self._video_library.get_video(video_id) + name = playlist_name.lower() + if name in self.playlists and video: + if self.playlists[name].add_video(video): + print(f"Added video to {playlist_name}:", video.title) + else: + print(f"Cannot add video to {playlist_name}:", Messages.VIDEO_IN_PLAYLIST.value ) + elif name not in self.playlists: + print(f"Cannot add video to {playlist_name}:", Messages.PLAYLIST_NOT_EXIST.value) + elif not video: + print(f"Cannot add video to {playlist_name}:", Messages.VIDEO_NOT_EXISTS.value) + else: + pass def show_all_playlists(self): """Display all playlists.""" - - print("show_all_playlists needs implementation") + if self.playlists: + playlist_names = list(self.playlists) + playlist_names.sort() + print("Showing all playlists:") + for name in playlist_names: + print(self.playlists[name]) + else: + print("No playlists exist yet") def show_playlist(self, playlist_name): """Display all videos in a playlist with a given name. @@ -143,7 +174,17 @@ def show_playlist(self, playlist_name): Args: playlist_name: The playlist name. """ - print("show_playlist needs implementation") + name = playlist_name.lower() + if name in self.playlists: + playlist = self.playlists[name] + print("Showing playlist:", playlist_name) + if len(playlist.videos) > 0: + for video in playlist.videos: + print(video) + else: + print(Messages.NO_VIDEOS_IN_PLAYLIST.value) + else: + print(f"Cannot show playlist {playlist_name}:", Messages.PLAYLIST_NOT_EXIST.value) def remove_from_playlist(self, playlist_name, video_id): """Removes a video to a playlist with a given name. @@ -152,7 +193,19 @@ def remove_from_playlist(self, playlist_name, video_id): playlist_name: The playlist name. video_id: The video_id to be removed. """ - print("remove_from_playlist needs implementation") + name = playlist_name.lower() + video = self._video_library.get_video(video_id) + if name in self.playlists: + playlist = self.playlists[name] + if video: + if self.playlists[name].remove_video(video): + print(f"Removed video from {playlist_name}:", video) + else: + print(f"Cannot remove video from {playlist}:", Messages.VIDEO_NOT_IN_PLAYLIST.value ) + else: + print(f"Cannot remove video from {playlist_name}:", Messages.VIDEO_NOT_EXISTS.value) + else: + print(f"Cannot remove video from {playlist_name}:", Messages.PLAYLIST_NOT_EXIST.value) def clear_playlist(self, playlist_name): """Removes all videos from a playlist with a given name. @@ -160,7 +213,16 @@ def clear_playlist(self, playlist_name): Args: playlist_name: The playlist name. """ - print("clears_playlist needs implementation") + name = playlist_name.lower() + if name in self.playlists: + playlist = self.playlists[name] + if len(playlist.videos) == 0: + print(Messages.NO_VIDEOS_IN_PLAYLIST.value) + else: + playlist.clear_videos() + print(f"Successfully removed all videos from {playlist_name}") + else: + print(f"Cannot clear playlist {playlist_name}:", Messages.PLAYLIST_NOT_EXIST.value) def delete_playlist(self, playlist_name): """Deletes a playlist with a given name. @@ -168,7 +230,12 @@ def delete_playlist(self, playlist_name): Args: playlist_name: The playlist name. """ - print("deletes_playlist needs implementation") + name = playlist_name.lower() + if name in self.playlists: + self.playlists.pop(name) + print("Deleted playlist:", name) + else: + print(f"Cannot delete playlist {playlist_name}:", Messages.PLAYLIST_NOT_EXIST.value) def search_videos(self, search_term): """Display all the videos whose titles contain the search_term. diff --git a/python/src/video_playlist.py b/python/src/video_playlist.py index 5836da4a..1b84b342 100644 --- a/python/src/video_playlist.py +++ b/python/src/video_playlist.py @@ -1,5 +1,31 @@ """A video playlist class.""" +from .video import Video class Playlist: """A class used to represent a Playlist.""" + + def __init__(self, playlist_name: str): + self.name = playlist_name + self.videos = [] + + def __str__(self) -> str: + return self.name + + def add_video(self, video: Video) -> bool: + """Adds a video to the playlist""" + if video in self.videos: + return False + self.videos.append(video) + return True + + def remove_video(self, video: Video) -> bool: + """Removes a video from the playlist""" + if video in self.videos: + self.videos.remove(video) + return True + return False + + def clear_videos(self): + """Removes all videos from playlist""" + self.videos.clear()