Added spotify import, somewhat still in beta

This commit is contained in:
Kasper Rynning-Tønnesen
2016-08-26 15:48:02 +02:00
parent 5956090c9f
commit 13bb52cded
13 changed files with 339 additions and 23 deletions

View File

@@ -123,6 +123,89 @@ var Search = {
}
},
backgroundSearch: function(title, artist, length, totalNumber, current){
var keyword= encodeURIComponent(title + " " + artist);
var yt_url = "https://www.googleapis.com/youtube/v3/search?key="+api_key+"&videoEmbeddable=true&part=id,snippet&fields=items(id,snippet)&type=video&order=relevance&safeSearch=none&maxResults=5";
yt_url+="&q="+keyword;
var vid_url = "https://www.googleapis.com/youtube/v3/videos?part=contentDetails,snippet,id&key="+api_key+"&id=";
artist = artist.split(" ");
var temptitle = title.toLowerCase().replace(" the ", "").replace(" a ", "").replace("the ", "");
temptitle = temptitle.split(" ");
$.ajax({
type: "GET",
url: yt_url,
dataType:"jsonp",
success: function(response){
//console.log(response);
if(response.items.length > 0) {
$.each(response.items, function(i,data)
{
var acceptable_track = true;
//console.log(data.snippet.title.toLowerCase().indexOf("cover"));
$.each(artist, function(i, data_artist){
if(data.snippet.title.toLowerCase().indexOf(data_artist.toLowerCase()) == -1){
acceptable_track = false;
return false;
}
});
if(data.snippet.title.toLowerCase().indexOf("cover") == -1 && acceptable_track) {
vid_url += data.id.videoId+",";
}
});
$.ajax({
type: "GET",
url: vid_url,
dataType:"jsonp",
success: function(response){
if(response.items.length > 0) {
var matched = false;
$.each(response.items, function(i, data){
//console.log(data);
//var title = data.snippet.title;
var duration = Search.durationToSeconds(data.contentDetails.duration);
var not_matched = 0;
$.each(temptitle, function(i, data_title){
if(data.snippet.title.toLowerCase().indexOf(data_title.toLowerCase()) == -1)
not_matched += 1;
});
if(((data.snippet.title.toLowerCase() == artist.join(" ").toLowerCase() + " - " + title.toLowerCase()) ||
(data.snippet.title.toLowerCase() == artist.join(" ").toLowerCase() + "-" + title.toLowerCase()) ||
(data.snippet.title.toLowerCase() == title.toLowerCase() + " - " + artist.join(" ").toLowerCase()) ||
(data.snippet.title.toLowerCase() == title.toLowerCase() + "-" + artist.join(" ").toLowerCase())) ||
(duration == length) ||
((data.snippet.title.toLowerCase().indexOf("lyric") > -1) ||
(data.snippet.title.toLowerCase().indexOf("music video") > -1) ||
(data.snippet.title.toLowerCase().indexOf("official video"))) && not_matched != temptitle.length - 1) {
matched = true;
Search.submit(data.id,data.snippet.title, duration, true, current, totalNumber);
return false;
}
});
if(!matched){
if(localStorage.debug === "true") {
console.log("------------------------------");
console.log("NO MATCH FOR:");
console.log("Spotify title: " + title + " " + artist.join(" "));
console.log("Spotify length: " + length);
console.log("------------------------------");
}
}
}
}
});
}
}
});
if(current == totalNumber - 1){
document.getElementById("import_spotify").disabled = false;
$("#import_spotify").removeClass("hide");
$("#playlist_loader_spotify").addClass("hide");
}
},
submitAndClose: function(id,title,duration){
Search.submit(id,title, duration, false, 0, 1);
$("#results").html('');
@@ -159,6 +242,27 @@ var Search = {
});
},
importSpotifyPlaylist: function(url){
$.ajax({
url: url,
headers: {
'Authorization': 'Bearer ' + access_token_data.access_token
},
success: function(response) {
console.log(response);
$.each(response.items, function(i,data)
{
//ids+=data.contentDetails.videoId+",";
Search.backgroundSearch(data.track.name, data.track.artists.map(function(elem){return elem.name;}).join(" "), Math.floor(data.track.duration_ms/1000), response.total, i + response.offset);
});
if(response.next){
Search.importSpotifyPlaylist(response.next);
}
}
});
},
addVideos: function(ids, playlist){
var request_url="https://www.googleapis.com/youtube/v3/videos?part=contentDetails,snippet,id&key=***REMOVED***&id=";
request_url += ids;