File size: 2,177 Bytes
e830c88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""
Song Class
Represents a music track
"""

class Song:
    def __init__(self, song_id, title, artist, album):
        self.song_id = song_id
        self.title = title
        self.artist = artist
        self.album = album
        self.duration = 0
        self.genre = "Unknown"
        self.year = None
        self.play_count = 0
        self.rating = 0.0
        self.favorite = False
        self.file_path = ""
    
    def set_duration(self, seconds):
        """
        Set song duration in seconds
        """
        if seconds > 0:
            self.duration = seconds
            return True
        return False
    
    def set_genre(self, genre):
        """
        Set music genre
        """
        self.genre = genre
    
    def set_year(self, year):
        """
        Set release year
        """
        if 1900 <= year <= 2100:
            self.year = year
            return True
        return False
    
    def play(self):
        """
        Increment play count
        """
        self.play_count += 1
    
    def rate(self, rating):
        """
        Rate the song (1-5 stars)
        """
        if 1 <= rating <= 5:
            self.rating = rating
            return True
        return False
    
    def toggle_favorite(self):
        """
        Toggle favorite status
        """
        self.favorite = not self.favorite
    
    def get_duration_string(self):
        """
        Get duration as MM:SS string
        """
        minutes = self.duration // 60
        seconds = self.duration % 60
        return f"{minutes}:{seconds:02d}"
    
    def to_dict(self):
        """
        Convert song to dictionary
        """
        return {
            'song_id': self.song_id,
            'title': self.title,
            'artist': self.artist,
            'album': self.album,
            'duration': self.get_duration_string(),
            'genre': self.genre,
            'year': self.year,
            'play_count': self.play_count,
            'rating': self.rating,
            'favorite': self.favorite
        }
    
    def __str__(self):
        return f"{self.artist} - {self.title} ({self.get_duration_string()})"