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

@@ -114,12 +114,19 @@
display:none;
}
.not-imported{
.not-imported, .not-exported{
border-top: 1px solid lightgrey;
margin-top: 15px;
color:black;
}
.not-exported-element{
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-between;
}
.not-imported-element{
height:60px;
}
@@ -131,6 +138,10 @@
line-height: 30px;
}
.not-exported-container .not-imported-element .extra-add-text{
width: 215px;
}
.extra-button{
margin: 0 4px !important;
padding: 0px !important;

View File

@@ -1 +1 @@
!function(){function e(e){var t,a=e.substring(1).split("&"),n={};for(var o in a)t=a[o].split("="),2==t.length&&(n[t[0]]=t[1]);return n}window.addEventListener("load",function(){var t,a,n,o=e(window.location.hash),i=window.location.protocol+"//"+window.location.hostname+"/o_callback";if(o.spotify)t="b934ecdd173648f5bcd38738af529d58",a="token",n="playlist-read-private playlist-read-collaborative user-read-private",state=o.nonce,window.location.href="https://accounts.spotify.com/authorize?client_id="+t+"&scope="+n+"&show_dialog=false&response_type="+a+"&redirect_uri="+i+"&state="+state;else if(o.youtube)t="944988770273-butsmlr1aotlsskk8lmgvh0etqqekigf.apps.googleusercontent.com",a="token",n="https://www.googleapis.com/auth/youtube",state=o.nonce,window.location.href="https://accounts.google.com/o/oauth2/v2/auth?client_id="+t+"&response_type="+a+"&state="+state+"&redirect_uri="+i+"&scope="+n;else{var s=e(window.location.hash);window.opener.callback(s)}})}();
!function(){function e(e){var t,a=e.substring(1).split("&"),n={};for(var o in a)t=a[o].split("="),2==t.length&&(n[t[0]]=t[1]);return n}window.addEventListener("load",function(){var t,a,n,o=e(window.location.hash),i=window.location.protocol+"//"+window.location.hostname+"/o_callback";if(o.spotify)t="b934ecdd173648f5bcd38738af529d58",a="token",n="playlist-read-private playlist-read-collaborative user-read-private playlist-modify-public",state=o.nonce,window.location.href="https://accounts.spotify.com/authorize?client_id="+t+"&scope="+n+"&show_dialog=false&response_type="+a+"&redirect_uri="+i+"&state="+state;else if(o.youtube)t="944988770273-butsmlr1aotlsskk8lmgvh0etqqekigf.apps.googleusercontent.com",a="token",n="https://www.googleapis.com/auth/youtube",state=o.nonce,window.location.href="https://accounts.google.com/o/oauth2/v2/auth?client_id="+t+"&response_type="+a+"&state="+state+"&redirect_uri="+i+"&scope="+n;else{var s=e(window.location.hash);window.opener.callback(s)}})}();

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

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){

View File

@@ -301,13 +301,13 @@
<div class="input-field field-settings">
</div>
</li>
<!--<li class="white-bg">
<li class="white-bg">
<div class="input-field field-settings spotify_unauthenticated export-buttons">
<a class="modal-trigger waves-effect green lighten btn export-spotify-auth" id="listExportSpotify" title="Export to Spotify">
Spotify
<a class="modal-trigger waves-effect green lighten btn export-spotify-auth" title="Export Spotify playlist">
<img src="/public/images/spotify.png" class="left spotify_logo" alt="Spotify Logo" />Spotify
</a>
</div>
</li>-->
</li>
<li class="exported-list-container white-bg hide">
<div class="valign playlist_loader_padding">
<div id="playlist_loader_export" class="preloader-wrapper small active hide">
@@ -360,11 +360,21 @@
</div>
</div>
</div>
<ul class="input-field field-settings white-bg">
<li class="white-bg exported-list white-bg">
</li>
</ul>
</li>
<li class="white-bg">
<div class="exported-list input-field field-settings export-buttons">
</div>
<li class="not-exported white-bg hide">
<div class="center-align">Not exported songs</div>
<ul class="input-field field-settings not-exported-container">
<li class="white-bg not-exported-element">
<div id="extra-export-container-text">
<input class="extra-add-text truncate" readonly />
</div>
<a href="#" class="waves-effect red lighten btn right extra-button extra-button-delete">X</a>
</li>
</ul>
</li>
</li>
</ul>
</div>