Skip to content

Commit 449bda5

Browse files
committed
changed classes
1 parent 1461278 commit 449bda5

File tree

3 files changed

+45
-61
lines changed

3 files changed

+45
-61
lines changed

ChillBot/base_urls.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
BaseAPI = "https://api.chillbot.cloud"
1+
base_api = "https://api.chillbot.cloud"

ChillBot/music.py

+42-58
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,54 @@
11
from .requests import Request
22
from .exceptions import UserNotFound
33

4+
from dataclasses import dataclass, field
5+
6+
@dataclass(frozen=True)
47
class TrackItem:
58
"""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
1612
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
2417
25-
Type: int
26-
"""
27-
return self._plays
18+
Type: int
19+
"""
20+
2821

22+
@dataclass(frozen=True)
2923
class ArtistListItem:
3024
"""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
4028
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
4833
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+
5237

38+
@dataclass(frozen=True)
5339
class MusicResponse:
5440
"""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}>"
6041

61-
@property
62-
def id(self) -> int:
63-
"""The User ID it returns
42+
id: int
43+
"""The User ID it returns
6444
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
7249
73-
Type: list[ArtistListItem]
74-
"""
75-
return [ArtistListItem(x) for x in self._response.get('artists')]
50+
Type: list[ArtistListItem]
51+
"""
7652

7753

7854
class Music:
@@ -95,4 +71,12 @@ async def get_top_ten(id: str):
9571
raise UserNotFound()
9672

9773
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+
)

ChillBot/requests.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import aiohttp
2-
from .base_urls import BaseAPI
2+
from .base_urls import base_api
33

44
class Request:
55
def __init__(self, headers: dict[str, str], params: dict[str, str]):
@@ -11,7 +11,7 @@ def __init__(self, headers: dict[str, str], params: dict[str, str]):
1111
)
1212

1313
async def GET(self, endpoint: str):
14-
response: aiohttp.ClientSession = await self._client.get(BaseAPI + endpoint, params=self._params)
14+
response: aiohttp.ClientSession = await self._client.get(base_api + endpoint, params=self._params)
1515

1616
await self._client.close()
1717
return response

0 commit comments

Comments
 (0)