Better YouTube importing, and auth-form prettier

This commit is contained in:
Kasper Rynning-Tønnesen
2018-03-22 15:53:55 +01:00
parent f518a06ed2
commit 9ccdcf7aec
5 changed files with 100 additions and 144 deletions

View File

@@ -29,9 +29,6 @@
adminpass: Base64(PASSWORD), adminpass: Base64(PASSWORD),
duration: VIDEO_DURATION, duration: VIDEO_DURATION,
list: channel_name, list: channel_name,
playlist: true_if_importing_playlist,
num: current_number_of_sending_songs,
total: total_number_of_sending_songs,
pass: Base64(channel_pass) pass: Base64(channel_pass)
} }

View File

@@ -293,9 +293,7 @@ function add_function(arr, coll, guid, offline, socket) {
if(typeof(arr.id) != "string" || typeof(arr.start) != "number" || if(typeof(arr.id) != "string" || typeof(arr.start) != "number" ||
typeof(arr.end) != "number" || typeof(arr.title) != "string" || typeof(arr.end) != "number" || typeof(arr.title) != "string" ||
typeof(arr.list) != "string" || typeof(arr.duration) != "number" || typeof(arr.list) != "string" || typeof(arr.duration) != "number") {
typeof(arr.playlist) != "boolean" || typeof(arr.num) != "number" ||
typeof(arr.total) != "number") {
var result = { var result = {
start: { start: {
expected: "number or string that can be cast to int", expected: "number or string that can be cast to int",
@@ -317,18 +315,6 @@ function add_function(arr, coll, guid, offline, socket) {
expected: "number or string that can be cast to int", expected: "number or string that can be cast to int",
got: arr.hasOwnProperty("duration") ? typeof(arr.duration) : undefined got: arr.hasOwnProperty("duration") ? typeof(arr.duration) : undefined
}, },
playlist: {
expected: "boolean",
got: arr.hasOwnProperty("playlist") ? typeof(arr.playlist) : undefined
},
num: {
expected: "number or string that can be cast to int",
got: arr.hasOwnProperty("num") ? typeof(arr.num) : undefined
},
total: {
expected: "number or string that can be cast to int",
got: arr.hasOwnProperty("total") ? typeof(arr.total) : undefined
},
pass: { pass: {
expected: "string", expected: "string",
got: arr.hasOwnProperty("pass") ? typeof(arr.pass) : undefined got: arr.hasOwnProperty("pass") ? typeof(arr.pass) : undefined
@@ -358,64 +344,19 @@ function add_function(arr, coll, guid, offline, socket) {
var title = arr.title; var title = arr.title;
var hash = Functions.hash_pass(Functions.hash_pass(Functions.decrypt_string(socketid, arr.adminpass), true)); var hash = Functions.hash_pass(Functions.hash_pass(Functions.decrypt_string(socketid, arr.adminpass), true));
var duration = parseInt(arr.duration); var duration = parseInt(arr.duration);
var full_list = arr.playlist;
var last = arr.num == arr.total - 1;
var num = arr.num;
var total = arr.total;
/*db.collection(coll + "_settings").find(function(err, docs) /*db.collection(coll + "_settings").find(function(err, docs)
{*/ {*/
conf = docs; conf = docs;
if(docs !== null && docs.length !== 0 && ((docs[0].addsongs === true && (hash == docs[0].adminpass || docs[0].adminpass === "")) || if(docs !== null && docs.length !== 0 && ((docs[0].addsongs === true && (hash == docs[0].adminpass || docs[0].adminpass === "")) ||
docs[0].addsongs === false)) docs[0].addsongs === false)) {
{
db.collection(coll).find({id:id, type:{$ne:"suggested"}}, function(err, docs){ db.collection(coll).find({id:id, type:{$ne:"suggested"}}, function(err, docs){
if(docs !== null && docs.length === 0) if(docs !== null && docs.length === 0) {
{ var guids = [guid];
var guids = full_list === true ? [] : [guid]; var added = Functions.get_time();
var votes; var votes = 1;
var added;
if(full_list) {
var time = Functions.get_time()-total;
time = time.toString();
var total_len = total.toString().length;
var now_len = num.toString().length;
var to_add = num.toString();
while(now_len < total_len) {
to_add = "0" + to_add;
now_len = to_add.length;
}
time = time.substring(0, time.length - total_len);
time = time + to_add;
time = parseInt(time);
added = time;
votes = 0;
} else {
added = Functions.get_time();
votes = 1;
}
db.collection(coll).find({now_playing:true}, function(err, docs){ db.collection(coll).find({now_playing:true}, function(err, docs){
if((docs !== null && docs.length === 0)){ if((docs !== null && docs.length === 0)){
np = true; np = true;
if(full_list && num === 0){
np = true;
time = time.toString();
total += 1;
var total_len = total.toString().length;
var now_len = total.toString().length;
var to_add = total.toString();
while(now_len < total_len) {
to_add = "0" + to_add;
now_len = to_add.length;
}
time = time.substring(0, time.length - total_len);
time = parseInt(time).toString() + to_add;
time = parseInt(time);
added = time;
votes = 0;
} else if(full_list) {
np = false;
}
} else { } else {
np = false; np = false;
} }
@@ -427,30 +368,21 @@ function add_function(arr, coll, guid, offline, socket) {
db.collection(coll + "_settings").update({ id: "config" }, {$set:{startTime: Functions.get_time()}}); db.collection(coll + "_settings").update({ id: "config" }, {$set:{startTime: Functions.get_time()}});
List.send_play(coll, undefined); List.send_play(coll, undefined);
Frontpage.update_frontpage(coll, id, title); Frontpage.update_frontpage(coll, id, title);
if(!full_list) Search.get_correct_info(new_song, coll, false); Search.get_correct_info(new_song, coll, false);
} else { } else {
io.to(coll).emit("channel", {type: "added", value: new_song}); io.to(coll).emit("channel", {type: "added", value: new_song});
if(!full_list) Search.get_correct_info(new_song, coll, true); Search.get_correct_info(new_song, coll, true);
} }
db.collection("frontpage_lists").update({_id:coll}, {$inc:{count:1}, $set:{accessed: Functions.get_time()}}, {upsert:true}, function(err, docs){}); db.collection("frontpage_lists").update({_id:coll}, {$inc:{count:1}, $set:{accessed: Functions.get_time()}}, {upsert:true}, function(err, docs){});
List.getNextSong(coll); List.getNextSong(coll);
}); });
if(!full_list) { socket.emit("toast", "addedsong");
socket.emit("toast", "addedsong");
} else if(full_list && last) {
socket.emit("toast", "addedplaylist");
}
}); });
} else if(!full_list) { } else {
ListChange.vote(coll, id, guid, socket, full_list, last); ListChange.vote(coll, id, guid, socket);
if(full_list && last) {
socket.emit("toast", "addedplaylist");
}
} else if(full_list && last) {
socket.emit("toast", "addedplaylist");
} }
}); });
} else if(!full_list) { } else {
db.collection(coll).find({id: id}, function(err, docs) { db.collection(coll).find({id: id}, function(err, docs) {
if(docs.length === 0) { if(docs.length === 0) {
db.collection(coll).update({id: id}, {$set:{ db.collection(coll).update({id: id}, {$set:{
@@ -472,14 +404,10 @@ function add_function(arr, coll, guid, offline, socket) {
} else if(docs[0].now_playing === true){ } else if(docs[0].now_playing === true){
socket.emit("toast", "alreadyplay"); socket.emit("toast", "alreadyplay");
} else{ } else{
if(conf[0].vote === false) ListChange.vote(coll, id, guid, socket, full_list, last); if(conf[0].vote === false) ListChange.vote(coll, id, guid, socket);
else socket.emit("toast", "listhaspass"); else socket.emit("toast", "listhaspass");
} }
}); });
} else if (full_list){
if(arr.num == 0) {
socket.emit("toast", "listhaspass");
}
} }
//}); //});
} else { } else {
@@ -555,7 +483,7 @@ function voteUndecided(msg, coll, guid, offline, socket) {
var hash = Functions.hash_pass(Functions.hash_pass(Functions.decrypt_string(socketid, msg.adminpass), true)); var hash = Functions.hash_pass(Functions.hash_pass(Functions.decrypt_string(socketid, msg.adminpass), true));
if(docs !== null && docs.length !== 0 && ((docs[0].vote === true && (hash == docs[0].adminpass || docs[0].adminpass === "")) || if(docs !== null && docs.length !== 0 && ((docs[0].vote === true && (hash == docs[0].adminpass || docs[0].adminpass === "")) ||
docs[0].vote === false)) { docs[0].vote === false)) {
ListChange.vote(coll, id, guid, socket, false, false); ListChange.vote(coll, id, guid, socket);
} else { } else {
socket.emit("toast", "listhaspass"); socket.emit("toast", "listhaspass");
} }
@@ -753,14 +681,13 @@ function delete_all(msg, coll, guid, offline, socket) {
} }
} }
function vote(coll, id, guid, socket, full_list, last) { function vote(coll, id, guid, socket) {
coll = coll.replace(/ /g,''); coll = coll.replace(/ /g,'');
db.collection(coll).find({id:id, now_playing: false, type:"video"}, function(err, docs){ db.collection(coll).find({id:id, now_playing: false, type:"video"}, function(err, docs){
if(docs !== null && docs.length > 0 && !Functions.contains(docs[0].guids, guid)) if(docs !== null && docs.length > 0 && !Functions.contains(docs[0].guids, guid))
{ {
db.collection(coll).update({id:id}, {$inc:{votes:1}, $set:{added:Functions.get_time()}, $push :{guids: guid}}, function(err, docs) db.collection(coll).update({id:id}, {$inc:{votes:1}, $set:{added:Functions.get_time()}, $push :{guids: guid}}, function(err, docs)
{ {
if((full_list && last) || (!full_list))
socket.emit("toast", "voted"); socket.emit("toast", "voted");
io.to(coll).emit("channel", {type: "vote", value: id, time: Functions.get_time()}); io.to(coll).emit("channel", {type: "vote", value: id, time: Functions.get_time()});

View File

@@ -142,6 +142,7 @@ function start_auth() {
$("#player_overlay").removeClass("hide"); $("#player_overlay").removeClass("hide");
$("#player_overlay").css("display", "block"); $("#player_overlay").css("display", "block");
$("#user_password").modal("open"); $("#user_password").modal("open");
$("#user-pass-input").focus();
//Crypt.remove_userpass(chan.toLowerCase()); //Crypt.remove_userpass(chan.toLowerCase());
before_toast(); before_toast();
Materialize.toast("That is not the correct password, try again..", 4000); Materialize.toast("That is not the correct password, try again..", 4000);
@@ -632,6 +633,11 @@ function toast(msg) {
break; break;
case "addedplaylist": case "addedplaylist":
if(embed) return; if(embed) return;
if(Search.submitYouTubeError) {
Materialize.toast("Added most of your playlist, but something was wrong. Check the playlist..", 4000, "red lighten connect_error");
Search.submitYouTubeError = false;
return;
}
msg=Helper.rnd(["I added the playlist", "Your playlist has been added", "Yay, many more songs!", "Thats a cool playlist!", "I added all the songs for you", "I see you like adding songs.."]); msg=Helper.rnd(["I added the playlist", "Your playlist has been added", "Yay, many more songs!", "Thats a cool playlist!", "I added all the songs for you", "I see you like adding songs.."]);
document.getElementById("import").disabled = false; document.getElementById("import").disabled = false;
$("#playlist_loader").addClass("hide"); $("#playlist_loader").addClass("hide");

View File

@@ -17,43 +17,43 @@ var List = {
{ {
case "list": case "list":
//if(full_playlist == undefined || !offline){ //if(full_playlist == undefined || !offline){
if((!offline || (offline && !msg.shuffled)) && !(offline && prev_chan_list == chan)){ if((!offline || (offline && !msg.shuffled)) && !(offline && prev_chan_list == chan)){
prev_chan_list = chan; prev_chan_list = chan;
List.populate_list(msg.playlist); List.populate_list(msg.playlist);
if(full_playlist.length > 0) { if(full_playlist.length > 0) {
Player.sendNext({title: full_playlist[0].title, videoId: full_playlist[0].id}); Player.sendNext({title: full_playlist[0].title, videoId: full_playlist[0].id});
}
} else if(offline && prev_chan_list == chan && full_playlist != undefined && !msg.shuffled){
List.populate_list(full_playlist, true);
if(full_playlist.length > 0) {
Player.sendNext({title: full_playlist[0].title, videoId: full_playlist[0].id});
}
} }
} else if(offline && prev_chan_list == chan && full_playlist != undefined && !msg.shuffled){ break;
List.populate_list(full_playlist, true);
if(full_playlist.length > 0) {
Player.sendNext({title: full_playlist[0].title, videoId: full_playlist[0].id});
}
}
break;
case "added": case "added":
List.added_song(msg.value); List.added_song(msg.value);
if(full_playlist.length > 0) {
Player.sendNext({title: full_playlist[0].title, videoId: full_playlist[0].id});
}
found_array = [];
found_array_index = 0;
break;
case "deleted":
List.deleted_song(msg.value, msg.removed);
found_array = [];
found_array_index = 0;
break;
case "vote":
if(!offline){
List.voted_song(msg.value, msg.time);
if(full_playlist.length > 0) { if(full_playlist.length > 0) {
Player.sendNext({title: full_playlist[0].title, videoId: full_playlist[0].id}); Player.sendNext({title: full_playlist[0].title, videoId: full_playlist[0].id});
} }
} found_array = [];
found_array = []; found_array_index = 0;
found_array_index = 0; break;
break; case "deleted":
List.deleted_song(msg.value, msg.removed);
found_array = [];
found_array_index = 0;
break;
case "vote":
if(!offline){
List.voted_song(msg.value, msg.time);
if(full_playlist.length > 0) {
Player.sendNext({title: full_playlist[0].title, videoId: full_playlist[0].id});
}
}
found_array = [];
found_array_index = 0;
break;
case "song_change": case "song_change":
if((offline && msg.offline_change) || !offline) { if((offline && msg.offline_change) || !offline) {
if(window.location.pathname != "/") List.song_change(msg.time, msg.remove); if(window.location.pathname != "/") List.song_change(msg.time, msg.remove);

View File

@@ -2,6 +2,10 @@ var Search = {
submitArray: [], submitArray: [],
submitArrayExpected: null, submitArrayExpected: null,
submitYouTubeArrayIds: [],
submitYouTubeArray: [],
submitYouTubeExpected: 0,
submitYouTubeError: false,
showSearch: function(){ showSearch: function(){
$("#search-wrapper").toggleClass("hide"); $("#search-wrapper").toggleClass("hide");
@@ -313,7 +317,7 @@ var Search = {
if(pageToken !== undefined) if(pageToken !== undefined)
token = "&pageToken="+pageToken; token = "&pageToken="+pageToken;
playlist_url = "https://www.googleapis.com/youtube/v3/playlistItems?part=contentDetails&maxResults=49&key="+api_key+"&playlistId="+pId+token; playlist_url = "https://www.googleapis.com/youtube/v3/playlistItems?part=contentDetails&maxResults=49&key="+api_key+"&playlistId="+pId+token;
if(youtube_authenticated){ if(youtube_authenticated) {
datatype = "html"; datatype = "html";
headers = { headers = {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
@@ -329,8 +333,7 @@ var Search = {
dataType: datatype, dataType: datatype,
//dataType:"jsonp", //dataType:"jsonp",
headers: headers, headers: headers,
success: function(response) success: function(response) {
{
if(response.error){ if(response.error){
if(response.error.errors[0].reason == "playlistItemsNotAccessible"){ if(response.error.errors[0].reason == "playlistItemsNotAccessible"){
var nonce = Helper.randomString(29); var nonce = Helper.randomString(29);
@@ -365,15 +368,26 @@ var Search = {
} else { } else {
var ids=""; var ids="";
var this_length = 0;
if(typeof(response) == "string") response = $.parseJSON(response); if(typeof(response) == "string") response = $.parseJSON(response);
//Search.addVideos(response.items[0].contentDetails.videoId); //Search.addVideos(response.items[0].contentDetails.videoId);
//response.items.shift(); //response.items.shift();
$.each(response.items, function(i,data) $.each(response.items, function(i,data) {
{
ids+=data.contentDetails.videoId+","; ids+=data.contentDetails.videoId+",";
Search.submitYouTubeArrayIds.push(data.contentDetails.videoId);
this_length += 1;
Search.submitYouTubeExpected += 1;
}); });
Search.addVideos(ids, true);
if(response.nextPageToken) Search.importPlaylist(pId, response.nextPageToken); if(response.nextPageToken) {
console.log(Search.submitYouTubeExpected);
//Search.addVideos(ids, true, 0, false, this_length);
Search.importPlaylist(pId, response.nextPageToken);
} else {
Search.addVideos(Search.submitYouTubeArrayIds);
//Search.addVideos(ids, true, Search.submitYouTubeExpected, true, this_length);
//Search.submitYouTubeExpected = 0;
}
document.getElementById("import").value = ""; document.getElementById("import").value = "";
} }
} }
@@ -407,9 +421,20 @@ var Search = {
}); });
}, },
addVideos: function(ids, playlist){ addVideos: function(ids){
var more = false;
var next_ids = [];
var this_ids = [];
var request_url="https://www.googleapis.com/youtube/v3/videos?part=contentDetails,snippet,id&key=***REMOVED***&id="; var request_url="https://www.googleapis.com/youtube/v3/videos?part=contentDetails,snippet,id&key=***REMOVED***&id=";
request_url += ids; for(var i = 0; i < ids.length; i++) {
if(i > 48) {
more = true;
next_ids = ids.slice(i, ids.length);
break;
}
request_url += ids[i] + ",";
this_ids.push(ids[i]);
}
$.ajax({ $.ajax({
type: "POST", type: "POST",
@@ -417,23 +442,24 @@ var Search = {
dataType:"jsonp", dataType:"jsonp",
success: function(response){ success: function(response){
var x = 0; var x = 0;
var to_add = []; if(response.error) {
$.each(response.items, function(i,song) Search.submitYouTubeError = true;
{ }
$.each(response.items, function(i,song) {
var duration=Search.durationToSeconds(song.contentDetails.duration); var duration=Search.durationToSeconds(song.contentDetails.duration);
if(!longsongs || duration<720){ if(!longsongs || duration<720){
enc_title= song.snippet.title;//encodeURIComponent(song.snippet.title); enc_title= song.snippet.title;//encodeURIComponent(song.snippet.title);
//Search.submit(song.id, enc_title, duration, playlist, i); //Search.submit(song.id, enc_title, duration, playlist, i);
x += 1; x += 1;
to_add.push({id: song.id, title: enc_title, duration: duration, playlist: playlist}); Search.submitYouTubeArray.push({id: song.id, title: enc_title, duration: duration});
} }
}); });
socket.emit("addPlaylist", {channel: chan.toLowerCase(), songs: to_add}); if(more) Search.addVideos(next_ids);
/* else {
$.each(to_add, function(i, item){ socket.emit("addPlaylist", {channel: chan.toLowerCase(), songs: Search.submitYouTubeArray});
Search.submit(item.id, item.enc_title, item.duration, item.playlist, i, x, 0, item.duration); Search.submitYouTubeArray = [];
}); Search.submitYouTubeExpected = 0;
*/ }
} }
}); });
}, },
@@ -451,14 +477,14 @@ var Search = {
} }
}); });
if(found_array.length == 0){ if(found_array.length == 0){
List.channel_function({type: "added", start: start, end: end, value: {added: (new Date).getTime()/1000, guids: [1], id: id, title: title, duration: duration, playlist: false, now_playing: false, votes: 1}}); List.channel_function({type: "added", start: start, end: end, value: {added: (new Date).getTime()/1000, guids: [1], id: id, title: title, duration: duration, now_playing: false, votes: 1}});
} else { } else {
List.vote(id, "pos"); List.vote(id, "pos");
} }
} else { } else {
/*var u = Crypt.crypt_pass(Crypt.get_userpass(chan.toLowerCase()), true); /*var u = Crypt.crypt_pass(Crypt.get_userpass(chan.toLowerCase()), true);
if(u == undefined) u = "";*/ if(u == undefined) u = "";*/
emit("add", {id: id, start: start, end: end, title: title, list: chan.toLowerCase(), duration: duration, playlist: playlist, num: num, total: full_num}); emit("add", {id: id, start: start, end: end, title: title, list: chan.toLowerCase(), duration: duration});
}//[id, decodeURIComponent(title), adminpass, duration, playlist]); }//[id, decodeURIComponent(title), adminpass, duration, playlist]);
}, },