Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 86 additions & 19 deletions python/src/video_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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())
Expand All @@ -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)

Expand All @@ -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
Expand All @@ -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."""
Expand All @@ -92,22 +100,22 @@ 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."""
if self.playing_status == PlayingStatus.PAUSED:
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__())
Expand All @@ -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.
Expand All @@ -130,20 +143,48 @@ 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.

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.
Expand All @@ -152,23 +193,49 @@ 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.

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.

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.
Expand Down
26 changes: 26 additions & 0 deletions python/src/video_playlist.py
Original file line number Diff line number Diff line change
@@ -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()