-
Notifications
You must be signed in to change notification settings - Fork 0
/
playlist_creator.html
133 lines (118 loc) · 4.09 KB
/
playlist_creator.html
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
<!DOCTYPE html>
<html>
<head>
<title>Spotify Controller</title>
</head>
<body>
<h1>Spotify Controller</h1>
<script src="token.js"></script>
<script>
function add_tracks(new_playlist, new_tracks) {
return fetch(`https://api.spotify.com/v1/playlists/${new_playlist.id}/tracks`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
uris: new_tracks.slice(0, 100)
})
})
}
function create_playlist_with_tracks(user_id, source_playlist, new_tracks) {
return fetch(`https://api.spotify.com/v1/users/${user_id}/playlists`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
name: source_playlist.name + "--mix",
public: false
})
}).then(response => {
return response.json()
}).then(new_playlist => {
return add_tracks(new_playlist, new_tracks)
})
}
function get_current_user(source_playlist) {
return fetch(`https://api.spotify.com/v1/me`, {
method: 'GET',
headers: {
'Authorization': `Bearer ${token}`
},
}).then(response => {
return response.json()
}).then(data => {
return data.id
})
}
function get_tracks(source_playlist) {
return fetch(`https://api.spotify.com/v1/playlists/${source_playlist.id}/tracks`, {
method: 'GET',
headers: {
'Authorization': `Bearer ${token}`
},
}).then(response => {
return response.json()
}).then(data => {
return data.items
});
}
function load_source_playlist(playlist_id) {
return fetch(`https://api.spotify.com/v1/playlists/${playlist_id}`, {
method: 'GET',
headers: {
'Authorization': `Bearer ${token}`
},
}).then(response => {
return response.json()
})
}
function interleave_tracks(data, song_uri) {
var new_tracks = []
for(var i = 0; i < data.length; i++) {
// not sure why tracks are sometimes null
if (data[i].track != null) {
new_tracks.push(song_uri);
new_tracks.push(data[i].track.uri)
}
}
return new_tracks
}
function display(output) {
document.getElementById("output").innerHTML = output
}
function create_playlist() {
var playlist_uri = document.getElementById("playlist_uri").value
var song_uri = document.getElementById("song_uri").value
var playlist_id = playlist_uri.replace("spotify:playlist:", "")
display("Creating...")
load_source_playlist(playlist_id).then(source_playlist => {
get_tracks(source_playlist, song_uri).then(data => {
return interleave_tracks(data, song_uri)
})
.then(new_tracks => {
get_current_user(source_playlist)
.then(user_id => {
create_playlist_with_tracks(user_id, source_playlist, new_tracks)
.then(response => {
if (response.status >= 200 && response.status < 300) {
display("Success!")
}
})
})
})
});
}
</script>
<p>On spotify, choose your song and playlist. On each, right-click -> 'Share' -> 'Copy Spotify URI' and paste here.</p>
<form id="form">
Song uri: <input type="text" id="song_uri" value="spotify:track:xxxx"><br/>
Playlist uri: <input type="text" id="playlist_uri" value="spotify:playlist:xxxx"><br/>
<input type="button" onclick="create_playlist()" value="Create playlist">
</form>
<div id="output"></div>
</body>
</html>