-
Notifications
You must be signed in to change notification settings - Fork 0
/
apple_music.py
53 lines (43 loc) · 1.49 KB
/
apple_music.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
import json
import requests
def add_track_to_apple_music_playlist(uri: str, telegram_user_id: str) -> bool:
f = open('users/%s.json' % telegram_user_id, 'r')
current_user_data = json.load(f)
playlist_id = current_user_data['user_apple_music_playlist_id']
url = "https://api.music.apple.com/v1/me/library/playlists/%s/tracks" % playlist_id
song_data = {
"data": [
{
"id": uri,
"type": "songs"
}
]
}
response = requests.post(
url,
data=json.dumps(song_data),
headers={
'Authorization': 'Bearer %s' % current_user_data['apple_music_credentials']['developer_token'],
'Music-User-Token': current_user_data['apple_music_credentials']['music_user_token']
}
)
if response.status_code == 204:
return True
return False
def create_playlist_for_user_in_apple_music(music_user_token: str, developer_token: str) -> str:
playlist_data = {
"attributes": {
"name": "Music Collector Playlist",
"description": "Here are all the track your friends sent you :)"
}
}
response = requests.post(
"https://api.music.apple.com/v1/me/library/playlists",
data=json.dumps(playlist_data),
headers={
'Authorization': 'Bearer %s' % developer_token,
'Music-User-Token': music_user_token
}
)
if response.status_code == 201:
return response.json()['data'][0]['id']