| """ |
| Playlist Class |
| Represents a music playlist |
| """ |
|
|
| from datetime import datetime |
|
|
| class Playlist: |
| def __init__(self, playlist_id, name): |
| self.playlist_id = playlist_id |
| self.name = name |
| self.song_ids = [] |
| self.created_date = datetime.now() |
| self.last_modified = datetime.now() |
| self.description = "" |
| self.is_public = False |
| self.play_count = 0 |
| |
| def add_song(self, song_id): |
| """ |
| Add a song to the playlist |
| """ |
| from parameters import MAX_PLAYLIST_SIZE |
| |
| if len(self.song_ids) >= MAX_PLAYLIST_SIZE: |
| return False |
| |
| if song_id not in self.song_ids: |
| self.song_ids.append(song_id) |
| self.last_modified = datetime.now() |
| return True |
| return False |
| |
| def remove_song(self, song_id): |
| """ |
| Remove a song from the playlist |
| """ |
| if song_id in self.song_ids: |
| self.song_ids.remove(song_id) |
| self.last_modified = datetime.now() |
| return True |
| return False |
| |
| def move_song(self, song_id, new_position): |
| """ |
| Move a song to a new position in the playlist |
| """ |
| if song_id in self.song_ids: |
| self.song_ids.remove(song_id) |
| if 0 <= new_position <= len(self.song_ids): |
| self.song_ids.insert(new_position, song_id) |
| self.last_modified = datetime.now() |
| return True |
| return False |
| |
| def set_description(self, description): |
| """ |
| Set playlist description |
| """ |
| self.description = description |
| |
| def set_public(self, is_public): |
| """ |
| Set playlist visibility |
| """ |
| self.is_public = is_public |
| |
| def play(self): |
| """ |
| Increment playlist play count |
| """ |
| self.play_count += 1 |
| |
| def get_song_count(self): |
| """ |
| Get number of songs in playlist |
| """ |
| return len(self.song_ids) |
| |
| def shuffle(self): |
| """ |
| Shuffle the playlist order |
| """ |
| import random |
| random.shuffle(self.song_ids) |
| self.last_modified = datetime.now() |
| |
| def clear(self): |
| """ |
| Remove all songs from playlist |
| """ |
| self.song_ids = [] |
| self.last_modified = datetime.now() |
| |
| def to_dict(self): |
| """ |
| Convert playlist to dictionary |
| """ |
| return { |
| 'playlist_id': self.playlist_id, |
| 'name': self.name, |
| 'song_count': self.get_song_count(), |
| 'created_date': str(self.created_date), |
| 'last_modified': str(self.last_modified), |
| 'is_public': self.is_public, |
| 'play_count': self.play_count |
| } |
| |
| def __str__(self): |
| return f"{self.name} - {self.get_song_count()} songs" |
|
|