From 9ccdcf7aecdfb5efa4f1b9b733ad27407ec82be2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Rynning-T=C3=B8nnesen?= Date: Thu, 22 Mar 2018 15:53:55 +0100 Subject: [PATCH] Better YouTube importing, and auth-form prettier --- server/EVENTS.md | 3 - server/handlers/list_change.js | 103 ++++----------------------- server/public/assets/js/functions.js | 6 ++ server/public/assets/js/list.js | 64 ++++++++--------- server/public/assets/js/search.js | 68 ++++++++++++------ 5 files changed, 100 insertions(+), 144 deletions(-) diff --git a/server/EVENTS.md b/server/EVENTS.md index 48c9b00f..5b612e1e 100644 --- a/server/EVENTS.md +++ b/server/EVENTS.md @@ -29,9 +29,6 @@ adminpass: Base64(PASSWORD), duration: VIDEO_DURATION, list: channel_name, - playlist: true_if_importing_playlist, - num: current_number_of_sending_songs, - total: total_number_of_sending_songs, pass: Base64(channel_pass) } diff --git a/server/handlers/list_change.js b/server/handlers/list_change.js index 841a0787..254d239d 100644 --- a/server/handlers/list_change.js +++ b/server/handlers/list_change.js @@ -293,9 +293,7 @@ function add_function(arr, coll, guid, offline, socket) { if(typeof(arr.id) != "string" || typeof(arr.start) != "number" || typeof(arr.end) != "number" || typeof(arr.title) != "string" || - typeof(arr.list) != "string" || typeof(arr.duration) != "number" || - typeof(arr.playlist) != "boolean" || typeof(arr.num) != "number" || - typeof(arr.total) != "number") { + typeof(arr.list) != "string" || typeof(arr.duration) != "number") { var result = { start: { 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", 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: { expected: "string", 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 hash = Functions.hash_pass(Functions.hash_pass(Functions.decrypt_string(socketid, arr.adminpass), true)); 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) {*/ conf = docs; 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){ - if(docs !== null && docs.length === 0) - { - var guids = full_list === true ? [] : [guid]; - var votes; - 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; - } - + if(docs !== null && docs.length === 0) { + var guids = [guid]; + var added = Functions.get_time(); + var votes = 1; db.collection(coll).find({now_playing:true}, function(err, docs){ if((docs !== null && docs.length === 0)){ 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 { 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()}}); List.send_play(coll, undefined); 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 { 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){}); List.getNextSong(coll); }); - if(!full_list) { - socket.emit("toast", "addedsong"); - } else if(full_list && last) { - socket.emit("toast", "addedplaylist"); - } + socket.emit("toast", "addedsong"); }); - } else if(!full_list) { - ListChange.vote(coll, id, guid, socket, full_list, last); - if(full_list && last) { - socket.emit("toast", "addedplaylist"); - } - } else if(full_list && last) { - socket.emit("toast", "addedplaylist"); + } else { + ListChange.vote(coll, id, guid, socket); } }); - } else if(!full_list) { + } else { db.collection(coll).find({id: id}, function(err, docs) { if(docs.length === 0) { 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){ socket.emit("toast", "alreadyplay"); } 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 if (full_list){ - if(arr.num == 0) { - socket.emit("toast", "listhaspass"); - } } //}); } 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)); if(docs !== null && docs.length !== 0 && ((docs[0].vote === true && (hash == docs[0].adminpass || docs[0].adminpass === "")) || docs[0].vote === false)) { - ListChange.vote(coll, id, guid, socket, false, false); + ListChange.vote(coll, id, guid, socket); } else { 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,''); 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)) { 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"); io.to(coll).emit("channel", {type: "vote", value: id, time: Functions.get_time()}); diff --git a/server/public/assets/js/functions.js b/server/public/assets/js/functions.js index 63ee01ae..2c7d36af 100644 --- a/server/public/assets/js/functions.js +++ b/server/public/assets/js/functions.js @@ -142,6 +142,7 @@ function start_auth() { $("#player_overlay").removeClass("hide"); $("#player_overlay").css("display", "block"); $("#user_password").modal("open"); + $("#user-pass-input").focus(); //Crypt.remove_userpass(chan.toLowerCase()); before_toast(); Materialize.toast("That is not the correct password, try again..", 4000); @@ -632,6 +633,11 @@ function toast(msg) { break; case "addedplaylist": 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.."]); document.getElementById("import").disabled = false; $("#playlist_loader").addClass("hide"); diff --git a/server/public/assets/js/list.js b/server/public/assets/js/list.js index a34fc1b4..64001dc5 100755 --- a/server/public/assets/js/list.js +++ b/server/public/assets/js/list.js @@ -17,43 +17,43 @@ var List = { { case "list": - //if(full_playlist == undefined || !offline){ - if((!offline || (offline && !msg.shuffled)) && !(offline && prev_chan_list == chan)){ - prev_chan_list = chan; - List.populate_list(msg.playlist); - if(full_playlist.length > 0) { - Player.sendNext({title: full_playlist[0].title, videoId: full_playlist[0].id}); + //if(full_playlist == undefined || !offline){ + if((!offline || (offline && !msg.shuffled)) && !(offline && prev_chan_list == chan)){ + prev_chan_list = chan; + List.populate_list(msg.playlist); + 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){ + 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){ - List.populate_list(full_playlist, true); - if(full_playlist.length > 0) { - Player.sendNext({title: full_playlist[0].title, videoId: full_playlist[0].id}); - } - } - break; + break; case "added": - 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); + 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; + 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) { + Player.sendNext({title: full_playlist[0].title, videoId: full_playlist[0].id}); + } + } + found_array = []; + found_array_index = 0; + break; case "song_change": if((offline && msg.offline_change) || !offline) { if(window.location.pathname != "/") List.song_change(msg.time, msg.remove); diff --git a/server/public/assets/js/search.js b/server/public/assets/js/search.js index 4f49056a..87d75935 100755 --- a/server/public/assets/js/search.js +++ b/server/public/assets/js/search.js @@ -2,6 +2,10 @@ var Search = { submitArray: [], submitArrayExpected: null, + submitYouTubeArrayIds: [], + submitYouTubeArray: [], + submitYouTubeExpected: 0, + submitYouTubeError: false, showSearch: function(){ $("#search-wrapper").toggleClass("hide"); @@ -313,7 +317,7 @@ var Search = { if(pageToken !== undefined) token = "&pageToken="+pageToken; 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"; headers = { 'Content-Type': 'application/json', @@ -329,8 +333,7 @@ var Search = { dataType: datatype, //dataType:"jsonp", headers: headers, - success: function(response) - { + success: function(response) { if(response.error){ if(response.error.errors[0].reason == "playlistItemsNotAccessible"){ var nonce = Helper.randomString(29); @@ -365,15 +368,26 @@ var Search = { } else { var ids=""; + var this_length = 0; if(typeof(response) == "string") response = $.parseJSON(response); //Search.addVideos(response.items[0].contentDetails.videoId); //response.items.shift(); - $.each(response.items, function(i,data) - { + $.each(response.items, function(i,data) { 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 = ""; } } @@ -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="; - 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({ type: "POST", @@ -417,23 +442,24 @@ var Search = { dataType:"jsonp", success: function(response){ var x = 0; - var to_add = []; - $.each(response.items, function(i,song) - { + if(response.error) { + Search.submitYouTubeError = true; + } + $.each(response.items, function(i,song) { var duration=Search.durationToSeconds(song.contentDetails.duration); if(!longsongs || duration<720){ enc_title= song.snippet.title;//encodeURIComponent(song.snippet.title); //Search.submit(song.id, enc_title, duration, playlist, i); 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}); - /* - $.each(to_add, function(i, item){ - Search.submit(item.id, item.enc_title, item.duration, item.playlist, i, x, 0, item.duration); - }); - */ + if(more) Search.addVideos(next_ids); + else { + socket.emit("addPlaylist", {channel: chan.toLowerCase(), songs: Search.submitYouTubeArray}); + Search.submitYouTubeArray = []; + Search.submitYouTubeExpected = 0; + } } }); }, @@ -451,14 +477,14 @@ var Search = { } }); 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 { List.vote(id, "pos"); } } else { /*var u = Crypt.crypt_pass(Crypt.get_userpass(chan.toLowerCase()), true); 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]); },