Added exporting to spotify from zöff playlist

This commit is contained in:
Kasper Rynning-Tønnesen
2017-01-07 17:02:01 +01:00
parent 62390e168c
commit a63f74d9ac
9 changed files with 270 additions and 33 deletions

View File

@@ -8,7 +8,7 @@ window.addEventListener("load", function(){
if(query.spotify){
client_id = "b934ecdd173648f5bcd38738af529d58";
response = "token";
scope = "playlist-read-private playlist-read-collaborative user-read-private";
scope = "playlist-read-private playlist-read-collaborative user-read-private playlist-modify-public";
state = query.nonce;
window.location.href = "https://accounts.spotify.com/authorize?client_id=" + client_id + "&scope=" + scope + "&show_dialog=false&response_type=" + response + "&redirect_uri=" + redirect + "&state=" + state;

View File

@@ -8,17 +8,17 @@ var Crypt = {
try{
conf_arr = Crypt.decrypt(Crypt.getCookie("_opt"), "_opt");
}catch(err){
conf_arr = Crypt.decrypt(Crypt.create_cookie("_opt"), "_opt");
}
try{
Crypt.conf_pass = Crypt.decrypt(Crypt.getCookie(chan.toLowerCase()), chan.toLowerCase());
}catch(err){
Crypt.conf_pass = Crypt.decrypt(Crypt.create_cookie(chan.toLowerCase()), chan.toLowerCase());
}
Hostcontroller.change_enabled(conf_arr.remote);
if(conf_arr.width != 100) Player.set_width(conf_arr.width);
if(conf_arr.name !== undefined && conf_arr.name !== "") Chat.namechange(conf_arr.name);
}catch(err){
conf_arr = Crypt.decrypt(Crypt.create_cookie("_opt"), "_opt");
}
try{
Crypt.conf_pass = Crypt.decrypt(Crypt.getCookie(chan.toLowerCase()), chan.toLowerCase());
}catch(err){
Crypt.conf_pass = Crypt.decrypt(Crypt.create_cookie(chan.toLowerCase()), chan.toLowerCase());
}
Hostcontroller.change_enabled(conf_arr.remote);
if(conf_arr.width != 100) Player.set_width(conf_arr.width);
if(conf_arr.name !== undefined && conf_arr.name !== "") Chat.namechange(conf_arr.name);
},
decrypt: function(cookie, name){

View File

@@ -85,7 +85,6 @@ var List = {
} else if(embed) {
List.can_fit = Math.round(($("#wrapper").height()) / 91) + 1;
List.element_height = (($("#wrapper").height()) / List.can_fit)-4;
console.log("anal")
}else {
List.can_fit = Math.round(($(window).height() - $(".tabs").height() - $("header").height() -66) / 71)+1;
List.element_height = (($(window).height() - $(".tabs").height() - $("header").height() -66) / List.can_fit)-6;
@@ -374,6 +373,187 @@ var List = {
return true;
},
exportToSpotify: function(){
$.ajax({
type: "GET",
url: "https://api.spotify.com/v1/me",
headers: {
'Authorization': 'Bearer ' + access_token_data.access_token
},
success: function(response){
var uris = [];
var user_id = response.id;
var num_songs = 0;
var not_found = [];
$.ajax({
type: "POST",
url: "https://api.spotify.com/v1/users/" + user_id + "/playlists",
headers: {
'Authorization': 'Bearer ' + access_token_data.access_token,
'Content-Type': 'application/json'
},
data: JSON.stringify({
name: chan.toLowerCase() + " - Zöff",
public: true
}),
success: function(response){
var playlist_id = response.id;
$.each(full_playlist, function(i, curr_song){
var original_track = curr_song.title;
var track = (curr_song.title.toLowerCase().replace("-", " "));
track = track.replace("official hd video", "");
track = track.replace("unofficial video", "");
track = track.replace("studio footage", "");
track = track.replace("great song", "");
track = track.replace("-", " ");
track = track.replace("-", " ");
track = track.replace(" hq ", " ");
track = track.replace("lyric video", "");
track = track.replace("lyrics video", "");
track = track.replace("album version", "");
track = track.replace("drive original movie soundtrack", "");
track = track.replace("original movie soundtrack", "");
track = track.replace("live sessions", "");
track = track.replace("audio only", "");
track = track.replace("audio", "");
track = track.replace("(new)", "");
track = track.replace(" by ", " ");
track = track.replace(" vs ", " ");
track = track.replace("(full)", " ");
track = track.replace("with lyrics", "");
track = track.replace("lyrics", "");
track = track.replace("w/", "");
track = track.replace("w/", "");
track = track.replace("official video", "");
track = track.replace("official music video", "");
track = track.replace("music video", "");
track = track.replace("musicvideo", "");
track = track.replace("original video", "");
track = track.replace("full version", "");
track = track.replace("full song", "");
track = track.replace("(official)", "");
track = track.replace("official", "");
track = track.replace("(original)", "");
track = track.replace("(", " ");
track = track.replace(")", " ");
track = track.replace("feat.", " ");
track = track.replace("feat", " ");
track = track.replace("ft.", " ");
track = track.replace("[", " ");
track = track.replace("]", " ");
track = track.replace(" free ", "");
track = track.replace(" hd ", "");
track = track.replace("original mix", " ");
track = track.replace("radio edit", " ");
track = track.replace("pop version", " ");
track = track.replace(" ", " ").replace(" ", " ").replace(" ", " ").replace(" ", " ").replace(" ", " ").replace(" ", " ");
track = encodeURIComponent(track);
$.ajax({
type: "GET",
url: "https://api.spotify.com/v1/search?q=" + track + "&type=track",
headers: {
'Authorization': 'Bearer ' + access_token_data.access_token
},
async: true,
error: function(err){
console.log(err);
},
success: function(response){
var found = false;
$.each(response.tracks.items, function(i, data){
data.name = data.name.toLowerCase();
data.name = data.name.replace("(", " ");
data.name = data.name.replace(")", " ");
data.name = data.name.replace("[", " ");
data.name = data.name.replace("]", " ");
data.name = data.name.replace("-", " ");
data.name = data.name.replace("original mix", " ");
data.name = data.name.replace("album version", " ");
data.name = data.name.replace("abum version", " ");
data.name = data.name.replace("feat.", " ");
data.artists[0].name = data.artists[0].name.replace("feat.", " ");
data.artists[0].name = data.artists[0].name.replace("feat", " ");
data.name = data.name.replace("feat", " ");
data.name = data.name.replace("ft.", " ");
data.name = data.name.replace("radio edit", " ");
data.name = data.name.replace("pop version", " ");
data.name = data.name.replace(" ", " ").replace(" ", " ").replace(" ", " ").replace(" ", " ").replace(" ", " ").replace(" ", " ").replace(" ", " ").replace(" ", " ");
data.artists[0].name = data.artists[0].name.replace(" ", " ").replace(" ", " ").replace(" ", " ").replace(" ", " ").replace(" ", " ").replace(" ", " ").replace(" ", " ").replace(" ", " ");
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;
return false;
} else {
var splitted = data.name.split(" ");
for(var i = 0; i < splitted.length; i++){
if((splitted[i] == "and" && track.indexOf("&") >= 0) || (splitted[i] == "&" && track.indexOf("and") >= 0)){
continue;
} else if(track.indexOf(splitted[i]) < 0){
return true;
}
}
found = true;
uris.push(data.uri);
num_songs = num_songs + 1;
return false;
}
});
if(!found){
not_found.push(original_track);
num_songs = 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);
} else {
uris = uris.slice(100, uris.length);
}
}
List.addToSpotifyPlaylist(uris, playlist_id, user_id);
} else {
List.addToSpotifyPlaylist(uris, playlist_id, user_id);
}
$(".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){
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);
$(".not-exported-container").append(not_added_song.html());
})
$(".not-exported").removeClass("hide");
}
}
})
});
}
});
}
})
},
addToSpotifyPlaylist: function(uris, playlist_id, user_id){
$.ajax({
type: "POST",
url: "https://api.spotify.com/v1/users/" + user_id + "/playlists/" + playlist_id + "/tracks",
headers: {
'Authorization': 'Bearer ' + access_token_data.access_token,
'Content-Type': 'application/json'
},
data: JSON.stringify({
uris: uris
}),
success: function(response){
Helper.log("Added songs");
}
})
},
exportToYoutube: function(){
var request_url = "https://www.googleapis.com/youtube/v3/playlists?part=snippet";
$(".exported-list-container").removeClass("hide");
@@ -412,18 +592,28 @@ var List = {
}
}
}),
success: function(response){
Helper.log("Added video: " + data.id + " to playlist id " + playlist_id);
error: function(code){
if(number_added == full_playlist.length - 1){
Helper.log("All videoes added!");
Helper.log("url: https://www.youtube.com/playlist?list=" + playlist_id);
$(".exported-list").append("<h5>Exported list</h5>");
$(".exported-list").append("<a target='_blank' class='btn light exported-playlist' href='https://www.youtube.com/playlist?list=" + playlist_id + "'>" + chan + "</a>");
$("#playlist_loader_export").addClass("hide");
}
number_added += 1;
},
success: function(response){
Helper.log("Added video: " + data.id + " to playlist id " + playlist_id);
if(number_added == full_playlist.length - 1){
Helper.log("All videoes added!");
Helper.log("url: https://www.youtube.com/playlist?list=" + playlist_id);
$(".exported-list").append("<a target='_blank' class='btn light exported-playlist' href='https://www.youtube.com/playlist?list=" + playlist_id + "'>" + chan + "</a>");
$("#playlist_loader_export").addClass("hide");
//$(".youtube_export_button").removeClass("hide");
}
number_added += 1;
}
});
});
},

View File

@@ -31,6 +31,7 @@ var blinking = false;
var access_token_data = {};
var spotify_authenticated = false;
var not_import_html = "";
var not_export_html = "";
var embed_height = 300;
var embed_width = 600;
var embed_autoplay = "&autoplay";
@@ -159,7 +160,9 @@ function init(){
result_html = $("#temp-results-container");
empty_results_html = $("#empty-results-container").html();
not_import_html = $(".not-imported-container").html();
not_export_html = $(".not-exported-container").html();
$(".not-imported-container").empty();
$(".not-exported-container").empty();
$(".video-container").resizable({
start: function(event, ui) {
@@ -574,6 +577,10 @@ $(document).on("click", ".modal-close", function(e){
e.preventDefault();
});
$(document).on("click", ".not-exported-container .not-exported-element #extra-export-container-text .extra-add-text", function(){
this.select();
})
$(document).on("click", ".next_page", function(e){
e.preventDefault();
List.dynamicContentPage(1);
@@ -686,9 +693,28 @@ $(document).on("click", "#listExport", function(e){
}
});
$(document).on("click", "#listExportSpotify", function(e){
$(document).on("click", ".export-spotify-auth", function(e){
e.preventDefault();
Helper.log(full_playlist);
var nonce = randomString(29);
window.callback = function(data) {
access_token_data = data;
if(access_token_data.state == nonce){
spotify_authenticated = true;
spotify_is_authenticated(true);
setTimeout(function(){
spotify_authenticated = false;
access_token_data = {};
spotify_is_authenticated(false);
}, access_token_data.expires_in * 1000);
List.exportToSpotify();
} else {
access_token_data = {};
console.error("States doesn't match");
}
spotify_window.close();
window.callback = "";
};
spotify_window = window.open("/o_callback#spotify=true&nonce=" + nonce, "", "width=600, height=600");
});
$(document).on("submit", "#listImport", function(e){