1
1
from .requests import Request
2
2
from .exceptions import UserNotFound
3
3
4
+ from dataclasses import dataclass , field
5
+
6
+ @dataclass (frozen = True )
4
7
class TrackItem :
5
8
"""Gets the track data"""
6
- def __init__ (self , name : str , plays : int ):
7
- self ._name = name
8
- self ._plays = plays
9
-
10
- def __str__ (self ):
11
- return f"<TrackItem name={ self ._name } tracks={ self ._tracks } >"
12
-
13
- @property
14
- def name (self ) -> str :
15
- """The name of the track
9
+
10
+ name : str
11
+ """The name of the track
16
12
17
- Type: str
18
- """
19
- return self ._name
20
-
21
- @property
22
- def plays (self ) -> int :
23
- """How many times it was played
13
+ Type: str
14
+ """
15
+ plays : int
16
+ """How many times it was played
24
17
25
- Type: int
26
- """
27
- return self . _plays
18
+ Type: int
19
+ """
20
+
28
21
22
+ @dataclass (frozen = True )
29
23
class ArtistListItem :
30
24
"""Artist data list"""
31
- def __init__ (self , artist : list ):
32
- self ._artist = artist
33
-
34
- def __str__ (self ):
35
- return f"<ArtistListItem name={ self .name } tracks={ self .tracks } >"
36
-
37
- @property
38
- def name (self ) -> str :
39
- """The name of the artist
25
+
26
+ name : str
27
+ """The name of the artist
40
28
41
- Type: str
42
- """
43
- return self ._artist .get ('name' )
44
-
45
- @property
46
- def tracks (self ) -> list [TrackItem ]:
47
- """Amount of tracks
29
+ Type: str
30
+ """
31
+ tracks : list [TrackItem ]
32
+ """Amount of tracks
48
33
49
- Type: list[TrackItem]
50
- """
51
- return [ TrackItem ( x , y ) for x , y in self . _artist . get ( 'tracks' ). items ()]
34
+ Type: list[TrackItem]
35
+ """
36
+
52
37
38
+ @dataclass (frozen = True )
53
39
class MusicResponse :
54
40
"""Music data response from user ID"""
55
- def __init__ (self , response : dict ):
56
- self ._response = response
57
-
58
- def __str__ (self ):
59
- return f"<MusicResponse id={ self .id } artists={ self .artists } >"
60
41
61
- @property
62
- def id (self ) -> int :
63
- """The User ID it returns
42
+ id : int
43
+ """The User ID it returns
64
44
65
- Type: int
66
- """
67
- return self ._response .get ('_id' )
68
-
69
- @property
70
- def artists (self ) -> list [ArtistListItem ]:
71
- """Returns the list of artists
45
+ Type: int
46
+ """
47
+ artists : list [ArtistListItem ]
48
+ """Returns the list of artists
72
49
73
- Type: list[ArtistListItem]
74
- """
75
- return [ArtistListItem (x ) for x in self ._response .get ('artists' )]
50
+ Type: list[ArtistListItem]
51
+ """
76
52
77
53
78
54
class Music :
@@ -95,4 +71,12 @@ async def get_top_ten(id: str):
95
71
raise UserNotFound ()
96
72
97
73
else :
98
- return MusicResponse (await response .json ())
74
+ json_response = await response .json ()
75
+
76
+ return MusicResponse (
77
+ json_response .get ('_id' ),
78
+ [ArtistListItem (
79
+ x .get ('name' ),
80
+ [TrackItem (x , y ) for x , y in x .get ('tracks' ).items ()]
81
+ ) for x in json_response .get ('artists' )]
82
+ )
0 commit comments