diff --git a/server/handlers/io.js b/server/handlers/io.js index 2df6cc53..64268256 100644 --- a/server/handlers/io.js +++ b/server/handlers/io.js @@ -57,6 +57,20 @@ module.exports = function() { } }); + socket.on("error_video", function(msg) { + try { + var _list = msg.channel; + if(_list.length == 0) return; + coll = emojiStrip(_list).toLowerCase(); + coll = coll.replace("_", ""); + coll = encodeURIComponent(coll).replace(/\W/g, ''); + coll = filter.clean(coll); + } catch(e) { + return; + } + Search.check_error_video(msg, coll); + }); + socket.on("get_spread", function(){ db.collection("connected_users").find({"_id": "total_users"}, function(err, tot) { db.collection("connected_users").find({"_id": "offline_users"}, function(err, off) { diff --git a/server/handlers/list_change.js b/server/handlers/list_change.js index b433a720..446d442e 100644 --- a/server/handlers/list_change.js +++ b/server/handlers/list_change.js @@ -84,6 +84,7 @@ function add_function(arr, coll, guid, offline, socket) { db.collection(coll).update({id: id}, {"added": added,"guids":guids,"id":id,"now_playing":np,"title":title,"votes":votes, "duration":duration, "start": start, "end": end}, {upsert: true}, function(err, docs){ if(np) { + var new_song = {"added": added,"guids":guids,"id":id,"now_playing":np,"title":title,"votes":votes, "duration":duration, "start": start, "end": end}; List.send_list(coll, undefined, false, true, false); db.collection(coll).update({views:{$exists:true}}, {$set:{startTime: Functions.get_time()}}); List.send_play(coll, undefined); diff --git a/server/handlers/search.js b/server/handlers/search.js index fc7ad98a..1f2e0c11 100644 --- a/server/handlers/search.js +++ b/server/handlers/search.js @@ -29,7 +29,8 @@ function get_correct_info(song_generated, channel, broadcast) { "title": song_generated.title, } }, function(err, docs) { - if(broadcast) { + if(broadcast && docs.nModified == 1) { + song_generated.new_id = song_generated.id; io.to(channel).emit("channel", {type: "changed_values", value: song_generated}); } }); @@ -39,6 +40,110 @@ function get_correct_info(song_generated, channel, broadcast) { }); } +function check_error_video(msg, channel) { + if(!msg.hasOwnProperty("id") || !msg.hasOwnProperty("title")) { + socket.emit("update_required"); + return; + } + + request({ + type: "GET", + url: "https://www.googleapis.com/youtube/v3/videos?part=id&key="+key+"&id=" + msg.id, + + }, function(error, response, body) { + var resp = JSON.parse(body); + if(resp.pageInfo.totalResults == 0) { + var yt_url = "https://www.googleapis.com/youtube/v3/search?key="+key+"&videoEmbeddable=true&part=id&type=video&order=viewCount&safeSearch=none&maxResults=5&q=" + encodeURIComponent(msg.title); + request({ + method: "GET", + url: yt_url, + }, function(error, response, body){ + var resp = JSON.parse(body); + if(resp.items.length > 0) { + var vid_url = "https://www.googleapis.com/youtube/v3/videos?part=contentDetails,snippet,id&key="+key+"&id="; + for(var i = 0; i < resp.items.length; i++) { + vid_url += resp.items[i].id.videoId + ","; + } + + request({ + type: "GET", + url: vid_url + }, function(error, response, body) { + var resp = JSON.parse(body); + var found = false; + var element = {}; + for(var i = 0; i < resp.items.length; i++) { + if(similarity(resp.items[i].snippet.localized.title, msg.title) > 0.75) { + found = true; + element = { + title: resp.items[i].snippet.localized.title, + duration: parseInt(durationToSeconds(resp.items[i].contentDetails.duration)), + id: resp.items[i].id, + start: 0, + end: parseInt(durationToSeconds(resp.items[i].contentDetails.duration)), + } + break; + } + } + if(found) { + db.collection(channel).update({"id": msg.id}, { + $set: element + }, function(err, docs) { + if(docs.nModified == 1) { + element.new_id = element.id; + element.id = msg.id; + io.to(channel).emit("channel", {type: "changed_values", value: element}); + } + }); + } + }); + } + }); + } + }); +} + +function similarity(s1, s2) { + var longer = s1; + var shorter = s2; + if (s1.length < s2.length) { + longer = s2; + shorter = s1; + } + var longerLength = longer.length; + if (longerLength == 0) { + return 1.0; + } + return (longerLength - editDistance(longer, shorter)) / parseFloat(longerLength); +} + +function editDistance(s1, s2) { + s1 = s1.toLowerCase(); + s2 = s2.toLowerCase(); + + var costs = new Array(); + for (var i = 0; i <= s1.length; i++) { + var lastValue = i; + for (var j = 0; j <= s2.length; j++) { + if (i == 0) + costs[j] = j; + else { + if (j > 0) { + var newValue = costs[j - 1]; + if (s1.charAt(i - 1) != s2.charAt(j - 1)) + newValue = Math.min(Math.min(newValue, lastValue), + costs[j]) + 1; + costs[j - 1] = lastValue; + lastValue = newValue; + } + } + } + if (i > 0) + costs[s2.length] = lastValue; + } + return costs[s2.length]; +} + function durationToSeconds(duration) { var matches = duration.match(time_regex); hours= parseInt(matches[12])||0; @@ -47,4 +152,5 @@ function durationToSeconds(duration) { return hours*60*60+minutes*60+seconds; } +module.exports.check_error_video = check_error_video; module.exports.get_correct_info = get_correct_info; diff --git a/server/public/assets/js/list.js b/server/public/assets/js/list.js index 92dd077e..e0edeee5 100755 --- a/server/public/assets/js/list.js +++ b/server/public/assets/js/list.js @@ -153,16 +153,21 @@ var List = { changedValues: function(song) { var i = List.getIndexOfSong(song.id); - full_playlist[i].title = song.title; - full_playlist[i].duration = song.duration; - full_playlist[i].start = song.start; - full_playlist[i].end = song.end; + if(i >= 0) { + full_playlist[i].title = song.title; + full_playlist[i].duration = song.duration; + full_playlist[i].start = song.start; + full_playlist[i].end = song.end; + full_playlist[i].id = song.new_id; - $("#" + song.id).find(".vote-container").attr("title", song.title); - $("#" + song.id).find(".list-title").attr("title", song.title); - $("#" + song.id).find(".list-title").text(song.title); - var _temp_duration = Helper.secondsToOther(song.duration); - $("#" + song.id).find(".card-duration").text(Helper.pad(_temp_duration[0]) + ":" + Helper.pad(_temp_duration[1])); + $("#" + song.id).find(".vote-container").attr("title", song.title); + $("#" + song.id).find(".list-title").attr("title", song.title); + $("#" + song.id).find(".list-title").text(song.title); + var _temp_duration = Helper.secondsToOther(song.duration); + $("#" + song.id).find(".card-duration").text(Helper.pad(_temp_duration[0]) + ":" + Helper.pad(_temp_duration[1])); + $("#" + song.id).find(".list-image").attr("style", "background-image:url('//img.youtube.com/vi/"+song.new_id+"/mqdefault.jpg');"); + $("#" + song.id).attr("id", song.new_id); + } }, insertAtBeginning: function(song_info, transition) { @@ -934,7 +939,8 @@ var List = { var video_id = _song_info.id; var video_title = _song_info.title; var video_votes = _song_info.votes; - var video_thumb = "background-image:url('//img.youtube.com/vi/"+video_id+"/mqdefault.jpg');"; + var video_thumb_url = "//img.youtube.com/vi/"+video_id+"/mqdefault.jpg"; + var video_thumb = "background-image:url('" + video_thumb_url + "');"; var song = $("
"+list_html+"
"); var image_attr = "style"; if(_song_info.hasOwnProperty("start") && _song_info.hasOwnProperty("end")) { @@ -966,6 +972,19 @@ var List = { attr = ".vote-container"; del_attr = "delete_button"; + var url = "https://zoff.me:8081/https://img.youtube.com/vi/"+video_id+"/mqdefault.jpg"; + $.ajax({ + type: "HEAD", + url: url, + error: function(e) { + if(e.status == 404) { + setTimeout(function() { + socket.emit("error_video", {channel: chan.toLowerCase(), id: video_id, title: video_title}); + }, 500); + } + } + }); + var _temp_duration = Helper.secondsToOther(_song_info.duration); song.find(".card-duration").text(Helper.pad(_temp_duration[0]) + ":" + Helper.pad(_temp_duration[1])); }else if(!list){ @@ -1001,6 +1020,7 @@ var List = { song.find(".list-title").attr("title", video_title); song.find(attr).attr("data-video-id", video_id); song.find(".list-image").attr(image_attr,video_thumb); + song.find(".list-image-placeholder").attr("src", video_thumb_url); song.find(".list-suggested-image").attr(image_attr,video_thumb); song.find("."+del_attr).attr("data-video-id", video_id); return song.html(); diff --git a/server/public/partials/channel/playlist.handlebars b/server/public/partials/channel/playlist.handlebars index 027be024..4ded5276 100644 --- a/server/public/partials/channel/playlist.handlebars +++ b/server/public/partials/channel/playlist.handlebars @@ -3,8 +3,7 @@
- - + 01:00