@@ -18,6 +18,32 @@ class TrackItem:
18
18
Type: int
19
19
"""
20
20
21
+ @dataclass (frozen = True )
22
+ class TrackList :
23
+ """Track data list"""
24
+
25
+ tracks : list [TrackItem ]
26
+ """List of tracks
27
+
28
+ Type: list[TrackItem]
29
+ """
30
+
31
+ def __init__ (self ):
32
+ super ().__init__ ()
33
+
34
+ def filter (self , name : str ) -> TrackItem | None :
35
+ """Filters the tracks
36
+
37
+ Returns: TrackItem | None
38
+ """
39
+
40
+ data = [x for x in self .tracks if x .get ('name' ).lower () == name .lower ()]
41
+
42
+ if len (data ) == 0 :
43
+ return None
44
+
45
+ return TrackItem (data [0 ].get ('name' ), data [0 ].get ('plays' ))
46
+
21
47
@dataclass (frozen = True )
22
48
class ArtistItem :
23
49
"""Gets the artist data"""
@@ -38,7 +64,7 @@ class ArtistList:
38
64
"""Artist data list"""
39
65
40
66
artists : list [ArtistItem ]
41
- """The name of the artist
67
+ """List of artists
42
68
43
69
Type: list[ArtistItem]
44
70
"""
@@ -56,8 +82,8 @@ def filter(self, name: str) -> ArtistItem | None:
56
82
57
83
if len (data ) == 0 :
58
84
return None
59
-
60
- return ArtistItem (data [0 ].get ('name' ), [TrackItem (x , y ) for x , y in data [0 ].get ('tracks' ).items ()])
85
+
86
+ return ArtistItem (data [0 ].get ('name' ), TrackList ( [TrackItem (x , y ) for x , y in data [0 ].get ('tracks' ).items ()]) )
61
87
62
88
63
89
@dataclass (frozen = True )
@@ -69,25 +95,25 @@ class MusicResponse:
69
95
70
96
Type: int
71
97
"""
72
- artists : list [ ArtistList ]
98
+ artists : ArtistList
73
99
"""Returns the list of artists
74
100
75
- Type: list[ ArtistList]
101
+ Type: ArtistList
76
102
"""
77
103
78
104
79
105
class Music :
80
106
"""Music class for requesting Music data"""
81
107
82
108
@staticmethod
83
- async def get_top_ten (id : str ):
109
+ async def get_top_ten (id : str | int ):
84
110
"""Gets the top 10 music data request
85
111
86
112
Returns: MusicResponse
87
113
"""
88
114
response = await Request (
89
115
headers = {"Content-Type" : "application/json" },
90
- params = {"user_id" : id }
116
+ params = {"user_id" : str ( id ) }
91
117
).GET (
92
118
"/music"
93
119
)
@@ -103,7 +129,9 @@ async def get_top_ten(id: str):
103
129
ArtistList (
104
130
[ArtistItem (
105
131
x .get ('name' ),
106
- [TrackItem (x , y ) for x , y in x .get ('tracks' ).items ()]
132
+ TrackList (
133
+ [TrackItem (x , y ) for x , y in x .get ('tracks' ).items ()]
134
+ )
107
135
) for x in json_response .get ('artists' )]
108
136
)
109
137
)
0 commit comments