-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotify_en_tools.js
208 lines (171 loc) · 5.79 KB
/
spotify_en_tools.js
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
function getConfig() {
return {
apiKey: "HJMRTLD9QZX2WZD3E",
spotifySpace: "spotify",
echoNestHost: "http://developer.echonest.com/"
};
}
/* Tools for making working with the Spotify and Echo Nest APIs easier */
function getSpotifyPlayButtonForPlaylist(title, playlist) {
var embed = '<iframe src="https://embed.spotify.com/?uri=spotify:track:5JunxkcjfCYcY7xJ29tLai" style="width:640px; height:520px;" frameborder="0" allowtransparency="true"></iframe>';
var tids = [];
playlist.forEach(function(song) {
var tid = fidToSpid(song.tracks[0].foreign_id);
tids.push(tid);
});
var tracks = tids.join(',');
var tembed = embed.replace('TRACKS', tracks);
tembed = tembed.replace('PREFEREDTITLE', title);
var li = $("<span>").html(tembed);
return $("<span>").html(tembed);
}
/* converts full URI to just the simple spotify id */
function fidToSpid(fid) {
var fields = fid.split(':');
return fields[fields.length - 1];
}
function getSpotifyPlayer(inPlaylist, callback) {
var curSong = 0;
var audio = null;
var player = createPlayer();
var playlist = null;
function addSpotifyInfoToPlaylist() {
var tids = [];
inPlaylist.forEach(function(song) {
var tid = fidToSpid(song.tracks[0].foreign_id);
tids.push(tid);
});
$.getJSON("https://api.spotify.com/v1/tracks/", { 'ids': tids.join(',')})
.done(function(data) {
console.log('sptracks', tids, data);
data.tracks.forEach(function(track, i) {
inPlaylist[i].spotifyTrackInfo = track;
});
console.log('inPlaylist', inPlaylist);
playlist = filterSongs(inPlaylist);
showCurSong(false);
callback(player);
})
.error( function() {
info("Whoops, had some trouble getting that playlist");
}) ;
}
function filterSongs(songs) {
var out = [];
function isGoodSong(song) {
return song.spotifyTrackInfo.preview_url != null;
}
songs.forEach(function(song) {
if (isGoodSong(song)) {
out.push(song);
}
});
return out;
}
function showSong(song, autoplay) {
$(player).find(".sp-album-art").attr('src', getBestImage(song.spotifyTrackInfo.album.images, 300).url);
$(player).find(".sp-title").text(song.title);
$(player).find(".sp-artist").text(song.artist_name);
audio.attr('src', song.spotifyTrackInfo.preview_url);
if (autoplay) {
audio.get(0).play();
}
}
function getBestImage(images, maxWidth) {
var best = images[0];
images.reverse().forEach(
function(image) {
if (image.width <= maxWidth) {
best = image;
}
}
);
return best;
}
function showCurSong(autoplay) {
showSong(playlist[curSong], autoplay);
}
function nextSong() {
if (curSong < playlist.length - 1) {
curSong++;
showCurSong(true);
} else {
}
}
function prevSong() {
if (curSong > 0) {
curSong--;
showCurSong(true);
}
}
function togglePausePlay() {
console.log('tpp', audio.get(0).paused);
if (audio.get(0).paused) {
audio.get(0).play();
} else {
audio.get(0).pause();
}
}
function createPlayer() {
var main = $("<div class='sp-player'>");
var img = $("<img class='sp-album-art'>");
var info = $("<div class='sp-info'>");
var title = $("<div class='sp-title'>");
var artist = $("<div class='sp-artist'>");
var controls = $("<div class='btn-group sp-controls'>");
var next = $('<button class="btn btn-primary btn-sm" type="button"><span class="glyphicon glyphicon-forward"></span></button>');
var prev = $('<button class="btn btn-primary btn-sm" type="button"><span class="glyphicon glyphicon-backward"></span></button>');
var pausePlay = $('<button class="btn btn-primary btn-sm" type="button"><span class="glyphicon glyphicon-play"></span></button>');
audio = $("<audio>");
audio.on('pause', function() {
var pp = pausePlay.find("span");
pp.removeClass('glyphicon-pause');
pp.addClass('glyphicon-play');
});
audio.on('play', function() {
var pp = pausePlay.find("span");
pp.addClass('glyphicon-pause');
pp.removeClass('glyphicon-play');
});
audio.on('ended', function() {
console.log('ended');
nextSong();
});
next.on('click', function() {
nextSong();
});
pausePlay.on('click', function() {
togglePausePlay();
});
prev.on('click', function() {
prevSong();
});
info.append(title);
info.append(artist);
controls.append(prev);
controls.append(pausePlay);
controls.append(next);
main.append(img);
main.append(info);
main.append(controls);
main.bind('destroyed', function() {
console.log('player destroyed');
audio.pause();
});
return main;
}
addSpotifyInfoToPlaylist();
return player;
}
// set up a handler so if an element is destroyed,
// the 'destroyed' handler is invoked.
// See // http://stackoverflow.com/questions/2200494/jquery-trigger-event-when-an-element-is-removed-from-the-dom
(function($){
$.event.special.destroyed = {
remove: function(o) {
if (o.handler) {
o.handler()
}
}
}
})(jQuery);