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

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -4,6 +4,9 @@ var List = {
page: 0, page: 0,
can_fit: Math.round(($("#wrapper").height()) / 71), can_fit: Math.round(($("#wrapper").height()) / 71),
element_height: (($("#wrapper").height()) / Math.round(($("#wrapper").height()) / 71)) - 25, element_height: (($("#wrapper").height()) / Math.round(($("#wrapper").height()) / 71)) - 25,
uris: [],
not_found: [],
num_songs: 0,
channel_function: function(msg) channel_function: function(msg)
{ {
@@ -381,10 +384,9 @@ var List = {
'Authorization': 'Bearer ' + access_token_data.access_token 'Authorization': 'Bearer ' + access_token_data.access_token
}, },
success: function(response){ success: function(response){
var uris = [];
var user_id = response.id; var user_id = response.id;
var num_songs = 0; $("#playlist_loader_export").removeClass("hide");
var not_found = []; $(".exported-list-container").removeClass("hide");
$.ajax({ $.ajax({
type: "POST", type: "POST",
url: "https://api.spotify.com/v1/users/" + user_id + "/playlists", url: "https://api.spotify.com/v1/users/" + user_id + "/playlists",
@@ -399,6 +401,15 @@ var List = {
success: function(response){ success: function(response){
var playlist_id = response.id; var playlist_id = response.id;
$.each(full_playlist, function(i, curr_song){ $.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 original_track = curr_song.title;
var track = (curr_song.title.toLowerCase().replace("-", " ")); var track = (curr_song.title.toLowerCase().replace("-", " "));
track = track.replace("official hd video", ""); track = track.replace("official hd video", "");
@@ -457,7 +468,13 @@ var List = {
}, },
async: true, async: true,
error: function(err){ 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){ success: function(response){
var found = false; 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(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){ if(decodeURIComponent(track).indexOf(data.artists[0].name.toLowerCase()) >= 0 && decodeURIComponent(track).indexOf(data.name.toLowerCase()) >= 0){
found = true; found = true;
uris.push(data.uri); List.uris.push(data.uri);
num_songs = num_songs + 1; //List.num_songs = List.num_songs + 1;
return false; return false;
} else { } else {
var splitted = data.name.split(" "); var splitted = data.name.split(" ");
@@ -496,31 +513,33 @@ var List = {
} }
} }
found = true; found = true;
uris.push(data.uri); List.uris.push(data.uri);
num_songs = num_songs + 1; //List.num_songs = List.num_songs + 1;
return false; return false;
} }
}); });
if(!found){ if(!found){
not_found.push(original_track); List.not_found.push(original_track);
num_songs = num_songs + 1; List.num_songs = List.num_songs + 1;
} }
if(num_songs == full_playlist.length){ if(List.num_songs + List.uris.length == full_playlist.length){
if(uris.length > 100){ if(List.uris.length > 100){
while(uris.length > 100){ while(List.uris.length > 100){
List.addToSpotifyPlaylist(uris.slice(0, 100), playlist_id, user_id); List.addToSpotifyPlaylist(List.uris.slice(0, 100), playlist_id, user_id);
if(uris.length > 200){ if(List.uris.length > 200){
uris = uris.slice(100, 200); List.uris = List.uris.slice(100, 200);
} else { } 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 { } 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>"); $(".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>"); 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("value", data);
not_added_song.find(".extra-add-text").attr("title", data); not_added_song.find(".extra-add-text").attr("title", data);
@@ -529,12 +548,7 @@ var List = {
$(".not-exported").removeClass("hide"); $(".not-exported").removeClass("hide");
} }
} }
})
}); });
}
});
}
})
}, },
addToSpotifyPlaylist: function(uris, playlist_id, user_id){ addToSpotifyPlaylist: function(uris, playlist_id, user_id){