Trying to add retry-after field

This commit is contained in:
Kasper Rynning-Tønnesen
2017-01-07 17:25:20 +01:00
parent a63f74d9ac
commit 1b802a1d87
3 changed files with 153 additions and 139 deletions
+2 -2
View File
File diff suppressed because one or more lines are too long
+3 -3
View File
File diff suppressed because one or more lines are too long
+39 -25
View File
@@ -4,6 +4,9 @@ var List = {
page: 0,
can_fit: Math.round(($("#wrapper").height()) / 71),
element_height: (($("#wrapper").height()) / Math.round(($("#wrapper").height()) / 71)) - 25,
uris: [],
not_found: [],
num_songs: 0,
channel_function: function(msg)
{
@@ -381,10 +384,9 @@ var List = {
'Authorization': 'Bearer ' + access_token_data.access_token
},
success: function(response){
var uris = [];
var user_id = response.id;
var num_songs = 0;
var not_found = [];
$("#playlist_loader_export").removeClass("hide");
$(".exported-list-container").removeClass("hide");
$.ajax({
type: "POST",
url: "https://api.spotify.com/v1/users/" + user_id + "/playlists",
@@ -399,6 +401,15 @@ var List = {
success: function(response){
var playlist_id = response.id;
$.each(full_playlist, function(i, curr_song){
List.searchSpotify(curr_song, playlist_id, user_id);
});
}
});
}
})
},
searchSpotify: function(curr_song, playlist_id, user_id){
var original_track = curr_song.title;
var track = (curr_song.title.toLowerCase().replace("-", " "));
track = track.replace("official hd video", "");
@@ -457,7 +468,13 @@ var List = {
},
async: true,
error: function(err){
console.log(err);
if(err.status == 429){
var retryAfter = err.getResponseHeader("Retry-After");
retryAfter = parseInt(retryAfter, 10);
setTimeout(function(){
searchSpotify(curr_song);
}, retryAfter * 1000);
}
},
success: function(response){
var found = false;
@@ -483,8 +500,8 @@ var List = {
if(data.name.substring(data.name.length-1) == " ") data.name = data.name.substring(0,data.name.length-1);
if(decodeURIComponent(track).indexOf(data.artists[0].name.toLowerCase()) >= 0 && decodeURIComponent(track).indexOf(data.name.toLowerCase()) >= 0){
found = true;
uris.push(data.uri);
num_songs = num_songs + 1;
List.uris.push(data.uri);
//List.num_songs = List.num_songs + 1;
return false;
} else {
var splitted = data.name.split(" ");
@@ -496,31 +513,33 @@ var List = {
}
}
found = true;
uris.push(data.uri);
num_songs = num_songs + 1;
List.uris.push(data.uri);
//List.num_songs = List.num_songs + 1;
return false;
}
});
if(!found){
not_found.push(original_track);
num_songs = num_songs + 1;
List.not_found.push(original_track);
List.num_songs = List.num_songs + 1;
}
if(num_songs == full_playlist.length){
if(uris.length > 100){
while(uris.length > 100){
List.addToSpotifyPlaylist(uris.slice(0, 100), playlist_id, user_id);
if(uris.length > 200){
uris = uris.slice(100, 200);
if(List.num_songs + List.uris.length == full_playlist.length){
if(List.uris.length > 100){
while(List.uris.length > 100){
List.addToSpotifyPlaylist(List.uris.slice(0, 100), playlist_id, user_id);
if(List.uris.length > 200){
List.uris = List.uris.slice(100, 200);
} else {
uris = uris.slice(100, uris.length);
List.uris = List.uris.slice(100, List.uris.length);
}
}
List.addToSpotifyPlaylist(uris, playlist_id, user_id);
List.addToSpotifyPlaylist(List.uris, playlist_id, user_id);
$("#playlist_loader_export").addClass("hide");
} else {
List.addToSpotifyPlaylist(uris, playlist_id, user_id);
List.addToSpotifyPlaylist(List.uris, playlist_id, user_id);
$("#playlist_loader_export").addClass("hide");
}
$(".exported-list").append("<a target='_blank' class='btn light exported-playlist' href='https://open.spotify.com/user/" + user_id + "/playlist/"+ playlist_id + "'>" + chan + "</a>");
$.each(not_found, function(i, data){
$.each(List.not_found, function(i, data){
var not_added_song = $("<div>" + not_export_html + "</div>");
not_added_song.find(".extra-add-text").attr("value", data);
not_added_song.find(".extra-add-text").attr("title", data);
@@ -529,12 +548,7 @@ var List = {
$(".not-exported").removeClass("hide");
}
}
})
});
}
});
}
})
},
addToSpotifyPlaylist: function(uris, playlist_id, user_id){