-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppleMusic.py
257 lines (215 loc) · 9.53 KB
/
AppleMusic.py
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import sys
import traceback
from typing import List, Any, Union
import requests
from requests import Session, Response
from .models.CatalogSongResponse import CatalogSong, CatalogSongs
from .models.LibraryArtist import LibraryArtist, LibraryArtists
from .models.LibraryPlaylist import LibraryPlaylist, LibraryPlaylists, LibraryPlaylistTracks
from .models.LibrarySearchResponse import LibrarySearchResponse
from .models.LibrarySong import LibrarySongs
from .models.common import ResourceTypes
class Service:
def __init__(self, dev_token: str, media_token: str):
self.developer_token: str = dev_token
self.media_token: str = media_token
self.base_url: str = "https://api.music.apple.com/v1/"
self.__client = Session()
self.__configure_session()
def __configure_session(self):
self.__client.headers = {
'Authorization': 'Bearer ' + self.developer_token,
'Media-User-Token': self.media_token,
'Origin': 'https://music.apple.com'
}
def perform_request(self, url: str, query_params: dict[str, Any] = {}, post = False, post_data = {}) -> Union[None, Response]:
try:
if not post:
response = self.__client.get(url, params=query_params)
else:
response = self.__client.post(url, params=query_params, json=post_data)
except requests.RequestException as err:
print("Error performing request.")
print(err)
traceback.print_exc()
else:
if not response.ok:
response.raise_for_status()
return response
# if status == 404:
# print(f"404 Not Found: {response.request.url}")
# elif status == 401:
# print("401 Unauthorized. Please check your developer and user tokens.")
# elif status == 403:
# print("403 Forbidden. Please check your developer and user tokens.")
# else:
# print(f"HTTP Error: {status}")
# return False, response
class LibraryService(Service):
def __init__(self, dev_token: str, media_token: str):
super().__init__(dev_token, media_token)
def get_next_songs(self, url) -> LibrarySongs:
response = self.perform_request("https://api.music.apple.com" + url)
return LibrarySongs.model_validate_json(response.text)
def get_playlist(self):
pass
# Fetch one or more playlists by ids. Max fetch limit is 25
def get_playlists_by_ids(self, ids: List[str]) -> List[LibraryPlaylist]:
query_params = {
"ids": ids
}
url = f"me/library/playlists"
response = self.perform_request(url, query_params)
# Using the LibraryPlaylists model because it already has a List[Playlists] for validation,
# The next and href will always be None for this endpoint, so they are unneeded. This is also done for
# other endpoints further down in this file
data = LibraryPlaylists.model_validate_json(response.text)
return data.playlists
# A playlist can also have a MusicVideo, should rework later
def get_playlist_tracks(self, playlist_id: str, offset: int = 0, limit: int = 25) -> LibraryPlaylistTracks:
"""
Note: May result in 404 not found if the playlist is empty
@param playlist_id: Library id of playlist
@param offset: Start index of returned results
@param limit: Number of results to return (max: 100)
@return: List of Songs
"""
query_params = {
"offset": offset,
"limit": limit
}
response = self.perform_request(self.base_url + f"me/library/playlists/{playlist_id}/tracks",
query_params)
return LibraryPlaylistTracks.model_validate_json(response.text)
def get_all_playlists(self, offset: int = 1, limit: int = 10) -> LibraryPlaylists:
"""
Gets all the user's playlists in their library
@param offset: Start index of the returned results
@param limit: Number of results to return
@return: List of LibraryPlaylists
"""
query_params = {
"offset": offset,
"limit": limit
}
response = self.perform_request(self.base_url + "me/library/playlists", query_params)
return LibraryPlaylists.model_validate_json(response.text)
def get_song(self) -> CatalogSong:
pass
def get_songs_by_ids(self, track_ids: List[str]) -> LibrarySongs:
"""
Gets library information of 1 - 300 songs.
@param track_ids: List of song ids
@return: List of Songs
"""
url = self.base_url + "me/library/songs"
query_params = {"ids": track_ids}
response = self.perform_request(url, query_params)
return LibrarySongs.model_validate_json(response.text)
def get_song_relationship(self):
pass
def get_all_songs(self, offset: int = 0, limit: int = 25) -> LibrarySongs:
"""
Gets all songs in the library.
Max limit of 100.
Used get_next() to continue going through all results
@param offset:
@param limit:
@return:
"""
query_params = {
"offset": offset,
"limit": limit
}
response = super().perform_request(self.base_url + "me/library/songs", query_params)
return LibrarySongs.model_validate_json(response.text)
def get_artist(self, artist_id: str) -> LibraryArtist:
"""
Note: Due to an API limitation, it is not possible to use an artist's library id to retrieve their catalog info
Instead, you must perform a search using the artist's name
@param artist_id: Library id of artist
@return: LibraryArtist
"""
response = self.perform_request(self.base_url + f"me/library/artists/{artist_id}", {})
return LibraryArtist.model_validate_json(response.text)
def get_artist_relationship(self):
pass
def get_artists_by_ids(self, artist_ids: List[str]) -> List[LibraryArtist]:
query_params = {"ids": artist_ids}
response = self.perform_request(self.base_url + "me/library/artists", query_params)
data = LibraryArtists.model_validate_json(response.text)
return data.artists
def get_all_artists(self) -> LibraryArtists:
response = self.perform_request(self.base_url + "me/library/artists")
return LibraryArtists.model_validate_json(response.text)
def search(self, search_term: str, types: List[ResourceTypes], limit: int = 25,
offset: int = 0) -> LibrarySearchResponse:
search_term = search_term.replace(" ", "+")
query_params = {
"term": search_term,
"types": types,
"limit": limit,
"offset": offset
}
response = self.perform_request(self.base_url + "me/library/search", query_params)
print(response.json())
return LibrarySearchResponse.model_validate_json(response.text)
def add_track_to_playlist(self, playlist_id: str, tracks: List[dict]):
data = tracks
response = self.perform_request(self.base_url + f"me/library/playlists/{playlist_id}/tracks", post=True, post_data=data)
def create_playlist(self, name: str, tracks: List[dict], description: str = "") -> LibraryPlaylist:
post_data = {
"attributes": {
"name": name,
"description": description
},
"relationships": {
"tracks": tracks
}
}
response = self.perform_request(self.base_url + "me/library/playlists", post=True, post_data=post_data)
# return LibraryPlaylist.model_validate_json(response.text)
class CatalogService(Service):
def __init__(self, dev_token: str, media_token: str):
super().__init__(dev_token, media_token)
def get_song(self) -> CatalogSong:
pass
def get_songs(self) -> List[CatalogSong]:
pass
def get_songs_by_ids(self, track_ids: List[str]) -> List[CatalogSong]:
"""
Gets catalog information of 1 - 300 songs
@param track_ids: List of song ids
@return: List of Songs
"""
query_params = {"ids": track_ids}
response = self.perform_request(self.base_url + "catalog/us/songs", query_params)
data = CatalogSongs.model_validate_json(response.text)
return data.songs
def search(self, search_term: str, types: [ResourceTypes], offset: int = 0, limit: int = 25):
query_params = {
"offset": offset,
"limit": limit,
"types": types,
"term": search_term
}
response = self.perform_request(self.base_url + "catalog/us/search", query_params)
class StorefrontService:
pass
class AppleMusicClient:
def __init__(self, dev_token: str, media_token: str) -> None:
self.library = LibraryService(dev_token, media_token)
self.catalog = CatalogService(dev_token, media_token)
# self.storefront = StorefrontService(dev_token, media_token)
"""
# Should use generics (or 2 separate functions) later so this works for playlists too
def get_next(self, url: str):
Retrieves the next set of data given the "next" url from a previous request
@param url:
@return:
response = self.__perform_request("https://api.music.apple.com" + url, {})
if not success:
return None
songs = SongsList.model_validate_json(response.text)
return songs
"""