diff --git a/.gitignore b/.gitignore index d54eb89b..a63cff8f 100755 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ server/public/assets/images/thumbnails/ server/config/mailconfig.js +server/config/api_key.js server/config/mongo_config.js server/config/cert_config.js server/public/assets/dist/callback.min.js diff --git a/server/config/api_key.example.js b/server/config/api_key.example.js new file mode 100644 index 00000000..59fd2d6a --- /dev/null +++ b/server/config/api_key.example.js @@ -0,0 +1,3 @@ +var key = "xxxx"; + +module.exports = key; diff --git a/server/handlers/list_change.js b/server/handlers/list_change.js index cec7a0df..b433a720 100644 --- a/server/handlers/list_change.js +++ b/server/handlers/list_change.js @@ -88,8 +88,11 @@ function add_function(arr, coll, guid, offline, socket) { db.collection(coll).update({views:{$exists:true}}, {$set:{startTime: Functions.get_time()}}); List.send_play(coll, undefined); Frontpage.update_frontpage(coll, id, title); + Search.get_correct_info(new_song, coll, false); } else { - io.to(coll).emit("channel", {type: "added", value: {"_id": "asd", "added":added,"guids":guids,"id":id,"now_playing":np,"title":title,"votes":votes, "duration":duration}}); + var new_song = {"_id": "asd", "added":added,"guids":guids,"id":id,"now_playing":np,"title":title,"votes":votes, "duration":duration}; + io.to(coll).emit("channel", {type: "added", value: new_song}); + 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); diff --git a/server/handlers/search.js b/server/handlers/search.js new file mode 100644 index 00000000..fc7ad98a --- /dev/null +++ b/server/handlers/search.js @@ -0,0 +1,50 @@ +var path = require('path'); +var time_regex = /P((([0-9]*\.?[0-9]*)Y)?(([0-9]*\.?[0-9]*)M)?(([0-9]*\.?[0-9]*)W)?(([0-9]*\.?[0-9]*)D)?)?(T(([0-9]*\.?[0-9]*)H)?(([0-9]*\.?[0-9]*)M)?(([0-9]*\.?[0-9]*)S)?)?/; +var key = require(path.join(__dirname, '../config/api_key.js')); + +function get_correct_info(song_generated, channel, broadcast) { + request({ + type: "GET", + url: "https://www.googleapis.com/youtube/v3/videos?part=contentDetails,snippet,id&key="+key+"&id=" + song_generated.id, + + }, function(error, response, body) { + var resp = JSON.parse(body); + if(resp.items.length == 1) { + var duration = parseInt(durationToSeconds(resp.items[0].contentDetails.duration)); + var title = resp.items[0].snippet.localized.title; + if(title != song_generated.title || duration < parseInt(song_generated.duration)) { + if(title != song_generated.title) { + song_generated.title = title; + } + if(duration < parseInt(song_generated.duration)) { + song_generated.duration = duration; + song_generated.start = 0; + song_generated.end = duration; + } + db.collection(channel).update({"id": song_generated.id}, { + $set: { + "duration": song_generated.duration, + "start": song_generated.start, + "end": song_generated.end, + "title": song_generated.title, + } + }, function(err, docs) { + if(broadcast) { + io.to(channel).emit("channel", {type: "changed_values", value: song_generated}); + } + }); + } + } + + }); +} + +function durationToSeconds(duration) { + var matches = duration.match(time_regex); + hours= parseInt(matches[12])||0; + minutes= parseInt(matches[14])||0; + seconds= parseInt(matches[16])||0; + return hours*60*60+minutes*60+seconds; +} + +module.exports.get_correct_info = get_correct_info; diff --git a/server/index.js b/server/index.js index ec121516..db4c58d9 100755 --- a/server/index.js +++ b/server/index.js @@ -87,6 +87,7 @@ List = require('./handlers/list.js'); Suggestions = require('./handlers/suggestions.js'); ListSettings = require('./handlers/list_settings.js'); Frontpage = require('./handlers/frontpage.js'); +Search = require('./handlers/search.js'); crypto = require('crypto'); node_cryptojs = require('node-cryptojs-aes'); CryptoJS = node_cryptojs.CryptoJS; diff --git a/server/public/assets/js/list.js b/server/public/assets/js/list.js index 684c6429..92dd077e 100755 --- a/server/public/assets/js/list.js +++ b/server/public/assets/js/list.js @@ -136,6 +136,9 @@ var List = { found_array_index = 0; //if(!w_p) List.dragging(true); break; + case "changed_values": + List.changedValues(msg.value); + break; case "song_change_prev": if(window.location.pathname != "/") List.song_change_prev(msg.time); if(full_playlist.length > 0) { @@ -148,6 +151,20 @@ 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; + + $("#" + 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])); + }, + insertAtBeginning: function(song_info, transition) { var display = List.page == 0 ? "" : "none"; var add = List.generateSong(song_info, transition, false, true, false, display, false);