| """ |
| Music Library Manager |
| Manage songs and playlists |
| """ |
|
|
| from song import Song |
| from playlist import Playlist |
|
|
| class MusicLibrary: |
| def __init__(self): |
| self.songs = {} |
| self.playlists = {} |
| self.next_song_id = 1 |
| self.next_playlist_id = 1 |
| |
| def add_song(self, title, artist, album): |
| """ |
| Add a song to the library |
| """ |
| song_id = f"SONG{self.next_song_id:05d}" |
| self.next_song_id += 1 |
| |
| song = Song(song_id, title, artist, album) |
| self.songs[song_id] = song |
| |
| return song |
| |
| def get_song(self, song_id): |
| """ |
| Get song by ID |
| """ |
| return self.songs.get(song_id) |
| |
| def delete_song(self, song_id): |
| """ |
| Delete a song from library |
| """ |
| if song_id in self.songs: |
| del self.songs[song_id] |
| return True |
| return False |
| |
| def create_playlist(self, name): |
| """ |
| Create a new playlist |
| """ |
| playlist_id = f"PL{self.next_playlist_id:04d}" |
| self.next_playlist_id += 1 |
| |
| playlist = Playlist(playlist_id, name) |
| self.playlists[playlist_id] = playlist |
| |
| return playlist |
| |
| def get_playlist(self, playlist_id): |
| """ |
| Get playlist by ID |
| """ |
| return self.playlists.get(playlist_id) |
| |
| def delete_playlist(self, playlist_id): |
| """ |
| Delete a playlist |
| """ |
| if playlist_id in self.playlists: |
| del self.playlists[playlist_id] |
| return True |
| return False |
| |
| def search_songs(self, query): |
| """ |
| Search songs by title, artist, or album |
| """ |
| query_lower = query.lower() |
| results = [] |
| |
| for song in self.songs.values(): |
| if (query_lower in song.title.lower() or |
| query_lower in song.artist.lower() or |
| query_lower in song.album.lower()): |
| results.append(song) |
| |
| return results |
| |
| def get_songs_by_artist(self, artist): |
| """ |
| Get all songs by an artist |
| """ |
| return [s for s in self.songs.values() if s.artist == artist] |
| |
| def get_songs_by_album(self, album): |
| """ |
| Get all songs in an album |
| """ |
| return [s for s in self.songs.values() if s.album == album] |
| |
| def get_songs_by_genre(self, genre): |
| """ |
| Get songs by genre |
| """ |
| return [s for s in self.songs.values() if s.genre == genre] |
| |
| def get_favorite_songs(self): |
| """ |
| Get all favorite songs |
| """ |
| return [s for s in self.songs.values() if s.favorite] |
| |
| def get_most_played_songs(self, limit=10): |
| """ |
| Get most played songs |
| """ |
| sorted_songs = sorted(self.songs.values(), key=lambda s: s.play_count, reverse=True) |
| return sorted_songs[:limit] |
| |
| def get_playlist_songs(self, playlist_id): |
| """ |
| Get all songs in a playlist |
| """ |
| playlist = self.get_playlist(playlist_id) |
| if not playlist: |
| return [] |
| |
| songs = [] |
| for song_id in playlist.song_ids: |
| song = self.get_song(song_id) |
| if song: |
| songs.append(song) |
| |
| return songs |
| |
| def calculate_total_duration(self): |
| """ |
| Calculate total duration of all songs |
| """ |
| return sum(s.duration for s in self.songs.values()) |
| |
| def get_library_statistics(self): |
| """ |
| Get library statistics |
| """ |
| total_songs = len(self.songs) |
| total_playlists = len(self.playlists) |
| total_duration = self.calculate_total_duration() |
| total_plays = sum(s.play_count for s in self.songs.values()) |
| |
| |
| artists = set(s.artist for s in self.songs.values()) |
| albums = set(s.album for s in self.songs.values()) |
| |
| stats = { |
| 'total_songs': total_songs, |
| 'total_playlists': total_playlists, |
| 'total_artists': len(artists), |
| 'total_albums': len(albums), |
| 'total_duration_seconds': total_duration, |
| 'total_duration_hours': round(total_duration / 3600, 2), |
| 'total_plays': total_plays, |
| 'favorite_count': len(self.get_favorite_songs()) |
| } |
| |
| return stats |
|
|