app.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. History = new Mongo.Collection("history");
  2. Playlists = new Mongo.Collection("playlists");
  3. Rooms = new Mongo.Collection("rooms");
  4. if (Meteor.isClient) {
  5. Meteor.startup(function() {
  6. reCAPTCHA.config({
  7. publickey: '6LcVxg0TAAAAAE18vBiH00UAyaJggsmLm890SjZl'
  8. });
  9. });
  10. var hpSound = undefined;
  11. var songsArr = [];
  12. var ytArr = [];
  13. var _sound = undefined;
  14. var parts = location.href.split('/');
  15. var id = parts.pop();
  16. var type = id.toLowerCase();
  17. Template.register.events({
  18. "submit form": function(e){
  19. e.preventDefault();
  20. var username = e.target.registerUsername.value;
  21. var email = e.target.registerEmail.value;
  22. var password = e.target.registerPassword.value;
  23. var captchaData = grecaptcha.getResponse();
  24. Meteor.call("createUserMethod", {username: username, email: email, password: password}, captchaData, function(err, res) {
  25. grecaptcha.reset();
  26. if (err) {
  27. console.log(err);
  28. } else {
  29. Meteor.loginWithPassword(username, password);
  30. }
  31. });
  32. },
  33. "click #facebook-login": function(){
  34. Meteor.loginWithFacebook()
  35. },
  36. "click #github-login": function(){
  37. Meteor.loginWithGithub()
  38. },
  39. "click #login": function(){
  40. $("#register-view").hide();
  41. $("#login-view").show();
  42. }
  43. });
  44. Template.login.events({
  45. "submit form": function(e){
  46. e.preventDefault();
  47. var username = e.target.loginUsername.value;
  48. var password = e.target.loginPassword.value;
  49. Meteor.loginWithPassword(username, password);
  50. Accounts.onLoginFailure(function(){
  51. $("input").css("background-color","indianred").addClass("animated shake");
  52. $("input").on("click",function(){
  53. $("input").css({
  54. "background-color": "transparent",
  55. "width": "250px"
  56. });
  57. })
  58. });
  59. },
  60. "click #facebook-login": function(){
  61. Meteor.loginWithFacebook()
  62. },
  63. "click #github-login": function(){
  64. Meteor.loginWithGithub()
  65. },
  66. "click #register": function(){
  67. $("#login-view").hide();
  68. $("#register-view").show();
  69. }
  70. });
  71. Template.dashboard.events({
  72. "click .logout": function(e){
  73. e.preventDefault();
  74. Meteor.logout();
  75. if (hpSound !== undefined) {
  76. hpSound.stop();
  77. }
  78. },
  79. "click #croom_create": function() {
  80. Meteor.call("createRoom", $("#croom").val(), function (err, res) {
  81. if (err) {
  82. alert("Error " + err.error + ": " + err.reason);
  83. } else {
  84. window.location = "/" + $("#croom").val();
  85. }
  86. });
  87. }
  88. });
  89. Template.dashboard.helpers({
  90. rooms: function() {
  91. return Rooms.find({});
  92. }
  93. })
  94. Template.room.events({
  95. "click #search-song": function(){
  96. $("#song-results").empty()
  97. $.ajax({
  98. type: "GET",
  99. url: "https://www.googleapis.com/youtube/v3/search?part=snippet&q=" + $("#song-input").val() + "&key=AIzaSyAgBdacEWrHCHVPPM4k-AFM7uXg-Q__YXY",
  100. applicationType: "application/json",
  101. contentType: "json",
  102. success: function(data){
  103. console.log(data);
  104. for(var i in data.items){
  105. $("#song-results").append("<p>" + data.items[i].snippet.title + "</p>")
  106. ytArr.push({title: data.items[i].snippet.title, id: data.items[i].id.videoId});
  107. }
  108. console.log(ytArr);
  109. $("#song-results p").click(function(){
  110. var title = $(this).text();
  111. for(var i in ytArr){
  112. if(ytArr[i].title === title){
  113. console.log(ytArr[i].title)
  114. var songObj = {
  115. id: ytArr[i].id,
  116. title: ytArr[i].title,
  117. type: "youtube"
  118. }
  119. }
  120. }
  121. })
  122. }
  123. })
  124. // SC.get('/tracks', { q: $("#song-input").val()}, function(tracks) {
  125. // console.log(tracks);
  126. // for(var i in tracks){
  127. // $("#song-results").append("<p>" + tracks[i].title + "</p>")
  128. // songsArr.push({title: tracks[i].title, id: tracks[i].id, duration: tracks[i].duration / 1000});
  129. // }
  130. // $("#song-results p").click(function(){
  131. // var title = $(this).text();
  132. // for(var i in songsArr){
  133. // if(songsArr[i].title === title){
  134. // var id = songsArr[i].id;
  135. // var duration = songsArr[i].duration;
  136. // var songObj = {
  137. // title: songsArr[i].title,
  138. // id: id,
  139. // duration: duration,
  140. // type: "soundcloud"
  141. // }
  142. // }
  143. // }
  144. // console.log(id);
  145. // })
  146. // });
  147. },
  148. "click #add-songs": function(){
  149. $("#add-songs-modal").show();
  150. }
  151. });
  152. Template.room.helpers({
  153. type: function() {
  154. var parts = location.href.split('/');
  155. var id = parts.pop();
  156. return id.toUpperCase();
  157. },
  158. title: function(){
  159. return Session.get("title");
  160. },
  161. artist: function(){
  162. return Session.get("artist");
  163. },
  164. loaded: function() {
  165. return Session.get("loaded");
  166. }
  167. });
  168. Template.admin.helpers({
  169. rooms: function() {
  170. return Rooms.find({});
  171. }
  172. });
  173. Template.playlist.helpers({
  174. playlist_songs: function() {
  175. var data = Playlists.find({type: type}).fetch();
  176. if (data !== undefined && data.length > 0) {
  177. return data[0].songs;
  178. } else {
  179. return [];
  180. }
  181. }
  182. });
  183. Meteor.subscribe("rooms");
  184. Template.room.onCreated(function () {
  185. var tag = document.createElement("script");
  186. tag.src = "https://www.youtube.com/iframe_api";
  187. var firstScriptTag = document.getElementsByTagName('script')[0];
  188. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  189. var currentSong = undefined;
  190. var _sound = undefined;
  191. var yt_player = undefined;
  192. var size = 0;
  193. var artistStr;
  194. var temp = "";
  195. var currentArt;
  196. function getTimeElapsed() {
  197. if (currentSong !== undefined) {
  198. return Date.now() - currentSong.started;
  199. }
  200. return 0;
  201. }
  202. function getSongInfo(query, platform){
  203. var search = query;
  204. var titles = [];
  205. query = query.toLowerCase().split(" ").join("%20");
  206. $.ajax({
  207. type: "GET",
  208. url: 'https://api.spotify.com/v1/search?q=' + query + '&type=track',
  209. applicationType: "application/json",
  210. contentType: "json",
  211. success: function(data){
  212. console.log(data);
  213. for(var i in data){
  214. for(var j in data[i].items){
  215. if(search.indexOf(data[i].items[j].name) !== -1){
  216. console.log(data[i].items[j].name);
  217. var info = data[i].items[j];
  218. Session.set("title", data[i].items[j].name);
  219. console.log("Info: " + info);
  220. if(platform === "youtube"){
  221. Session.set("duration", data[i].items[j].duration_ms / 1000)
  222. console.log(Session.get("duration"));
  223. }
  224. temp = "";
  225. if(data[i].items[j].artists.length >= 2){
  226. for(var k in data[i].items[j].artists){
  227. temp = temp + data[i].items[j].artists[k].name + ", ";
  228. }
  229. } else{
  230. for(var k in data[i].items[j].artists){
  231. temp = temp + data[i].items[j].artists[k].name;
  232. }
  233. }
  234. if(temp[temp.length-2] === ","){
  235. artistStr = temp.substr(0,temp.length-2);
  236. } else{
  237. artistStr = temp;
  238. }
  239. Session.set("artist", artistStr);
  240. $(".current").remove();
  241. $(".room-title").before("<img class='current' src='" + data[i].items[j].album.images[1].url + "' />");
  242. return true;
  243. }
  244. }
  245. //---------------------------------------------------------------//
  246. }
  247. }
  248. })
  249. }
  250. function resizeSeekerbar() {
  251. $("#seeker-bar").width(((getTimeElapsed() / 1000) / Session.get("duration") * 100) + "%");
  252. }
  253. function startSong() {
  254. if (currentSong !== undefined) {
  255. if (_sound !== undefined) _sound.stop();
  256. if (yt_player !== undefined && yt_player.stopVideo !== undefined) yt_player.stopVideo();
  257. if (currentSong.song.type === "soundcloud") {
  258. $("#player").attr("src", "")
  259. getSongInfo(currentSong.song.title);
  260. SC.stream("/tracks/" + currentSong.song.id + "#t=20s", function(sound){
  261. console.log(sound);
  262. _sound = sound;
  263. sound._player._volume = 0.3;
  264. sound.play();
  265. console.log(getTimeElapsed());
  266. var interval = setInterval(function() {
  267. if (sound.getState() === "playing") {
  268. sound.seek(getTimeElapsed());
  269. window.clearInterval(interval);
  270. }
  271. }, 200);
  272. // Session.set("title", currentSong.song.title || "Title");
  273. // Session.set("artist", currentSong.song.artist || "Artist");
  274. Session.set("duration", currentSong.song.duration);
  275. resizeSeekerbar();
  276. });
  277. } else {
  278. if (yt_player === undefined) {
  279. yt_player = new YT.Player("player", {
  280. height: 540,
  281. width: 960,
  282. videoId: currentSong.song.id,
  283. events: {
  284. 'onReady': function(event) {
  285. event.target.seekTo(getTimeElapsed() / 1000);
  286. event.target.playVideo();
  287. resizeSeekerbar();
  288. },
  289. 'onStateChange': function(event){
  290. if (event.data == YT.PlayerState.PAUSED) {
  291. event.target.seekTo(getTimeElapsed() / 1000);
  292. event.target.playVideo();
  293. }
  294. }
  295. }
  296. });
  297. } else {
  298. yt_player.loadVideoById(currentSong.song.id);
  299. }
  300. // Session.set("title", currentSong.song.title || "Title");
  301. // Session.set("artist", currentSong.song.artist || "Artist");
  302. getSongInfo(currentSong.song.title, "youtube");
  303. //Session.set("duration", currentSong.song.duration);
  304. }
  305. }
  306. }
  307. Meteor.subscribe("history");
  308. Meteor.subscribe("playlists");
  309. Session.set("loaded", true);
  310. Meteor.subscribe("rooms", function() {
  311. Session.set("loaded", false);
  312. console.log(Rooms.find({type: type}).fetch().length);
  313. if (Rooms.find({type: type}).count() !== 1) {
  314. window.location = "/";
  315. } else {
  316. Session.set("loaded", true);
  317. Meteor.setInterval(function () {
  318. var data = undefined;
  319. var dataCursor = History.find({type: type});
  320. dataCursor.map(function (doc) {
  321. if (data === undefined) {
  322. data = doc;
  323. }
  324. });
  325. if (data !== undefined && data.history.length > size) {
  326. currentSong = data.history[data.history.length - 1];
  327. size = data.history.length;
  328. startSong();
  329. }
  330. }, 1000);
  331. Meteor.setInterval(function () {
  332. resizeSeekerbar();
  333. }, 50);
  334. }
  335. });
  336. });
  337. Template.admin.events({
  338. "submit form": function(e){
  339. e.preventDefault();
  340. var genre = e.target.genre.value;
  341. var type = e.target.type.value;
  342. var id = e.target.id.value;
  343. var title = e.target.title.value;
  344. var artist = e.target.artist.value;
  345. var songData = {type: type, id: id, title: title, artist: artist};
  346. Meteor.call("addPlaylistSong", genre, songData, function(err, res) {
  347. console.log(err, res);
  348. });
  349. }
  350. });
  351. }
  352. if (Meteor.isServer) {
  353. Meteor.startup(function() {
  354. reCAPTCHA.config({
  355. privatekey: '6LcVxg0TAAAAAI2fgIEEWHFxwNXeVIs8mzq5cfRM'
  356. });
  357. });
  358. Meteor.users.deny({update: function () { return true; }});
  359. Meteor.users.deny({insert: function () { return true; }});
  360. Meteor.users.deny({remove: function () { return true; }});
  361. function getSongDuration(query){
  362. var duration;
  363. var search = query;
  364. query = query.toLowerCase().split(" ").join("%20");
  365. var res = Meteor.http.get('https://api.spotify.com/v1/search?q=' + query + '&type=track');
  366. for(var i in res.data){
  367. for(var j in res.data[i].items){
  368. if(search.indexOf(res.data[i].items[j].name) !== -1){
  369. duration = res.data[i].items[j].duration_ms / 1000;
  370. console.log(duration);
  371. return duration;
  372. }
  373. }
  374. }
  375. }
  376. function getSongAlbumArt(query){
  377. var albumart;
  378. var search = query;
  379. query = query.toLowerCase().split(" ").join("%20");
  380. var res = Meteor.http.get('https://api.spotify.com/v1/search?q=' + query + '&type=track');
  381. for(var i in res.data){
  382. for(var j in res.data[i].items){
  383. if(search.indexOf(res.data[i].items[j].name) !== -1){
  384. albumart = res.data[i].items[j].album.images[1].url
  385. return albumart;
  386. }
  387. }
  388. }
  389. }
  390. //var room_types = ["edm", "nightcore"];
  391. var songsArr = [];
  392. function getSongsByType(type) {
  393. if (type === "edm") {
  394. return [
  395. {id: "aE2GCa-_nyU", title: "Radioactive - Lindsey Stirling and Pentatonix", duration: getSongDuration("Radioactive - Lindsey Stirling and Pentatonix"), albumart: getSongAlbumArt("Radioactive - Lindsey Stirling and Pentatonix"), type: "youtube"},
  396. {id: "aHjpOzsQ9YI", title: "Crystallize", artist: "Linsdey Stirling", duration: getSongDuration("Crystallize"), albumart: getSongAlbumArt("Crystallize"), type: "youtube"}
  397. ];
  398. } else if (type === "nightcore") {
  399. return [{id: "f7RKOP87tt4", title: "Monster (DotEXE Remix)", duration: getSongDuration("Monster (DotEXE Remix)"), albumart: getSongAlbumArt("Monster (DotEXE Remix)"), type: "youtube"}];
  400. } else {
  401. return [{id: "dQw4w9WgXcQ", title: "Never Gonna Give You Up", duration: getSongDuration("Never Gonna Give You Up"), albumart: getSongAlbumArt("Never Gonna Give You Up"), type: "youtube"}];
  402. }
  403. }
  404. Rooms.find({}).fetch().forEach(function(room) {
  405. var type = room.type;
  406. if (Playlists.find({type: type}).count() === 0) {
  407. if (type === "edm") {
  408. Playlists.insert({type: type, songs: getSongsByType(type)});
  409. } else if (type === "nightcore") {
  410. Playlists.insert({type: type, songs: getSongsByType(type)});
  411. } else {
  412. Playlists.insert({type: type, songs: getSongsByType(type)});
  413. }
  414. }
  415. if (History.find({type: type}).count() === 0) {
  416. History.insert({type: type, history: []});
  417. }
  418. if (Playlists.find({type: type}).fetch()[0].songs.length === 0) {
  419. // Add a global video to Playlist so it can proceed
  420. } else {
  421. var startedAt = Date.now();
  422. var songs = Playlists.find({type: type}).fetch()[0].songs;
  423. var currentSong = 0;
  424. addToHistory(songs[currentSong], startedAt);
  425. function addToHistory(song, startedAt) {
  426. History.update({type: type}, {$push: {history: {song: song, started: startedAt}}});
  427. }
  428. function skipSong() {
  429. songs = Playlists.find({type: type}).fetch()[0].songs;
  430. if (currentSong < (songs.length - 1)) {
  431. currentSong++;
  432. } else currentSong = 0;
  433. songTimer();
  434. addToHistory(songs[currentSong], startedAt);
  435. }
  436. function songTimer() {
  437. startedAt = Date.now();
  438. Meteor.setTimeout(function() {
  439. skipSong();
  440. }, songs[currentSong].duration * 1000);
  441. }
  442. songTimer();
  443. }
  444. });
  445. ServiceConfiguration.configurations.remove({
  446. service: "facebook"
  447. });
  448. ServiceConfiguration.configurations.insert({
  449. service: "facebook",
  450. appId: "1496014310695890",
  451. secret: "9a039f254a08a1488c08bb0737dbd2a6"
  452. });
  453. ServiceConfiguration.configurations.remove({
  454. service: "github"
  455. });
  456. ServiceConfiguration.configurations.insert({
  457. service: "github",
  458. clientId: "dcecd720f47c0e4001f7",
  459. secret: "375939d001ef1a0ca67c11dbf8fb9aeaa551e01b"
  460. });
  461. Meteor.publish("history", function() {
  462. return History.find({})
  463. });
  464. Meteor.publish("playlists", function() {
  465. return Playlists.find({})
  466. });
  467. Meteor.publish("rooms", function() {
  468. return Rooms.find()
  469. });
  470. Meteor.methods({
  471. createUserMethod: function(formData, captchaData) {
  472. var verifyCaptchaResponse = reCAPTCHA.verifyCaptcha(this.connection.clientAddress, captchaData);
  473. if (!verifyCaptchaResponse.success) {
  474. console.log('reCAPTCHA check failed!', verifyCaptchaResponse);
  475. throw new Meteor.Error(422, 'reCAPTCHA Failed: ' + verifyCaptchaResponse.error);
  476. } else {
  477. console.log('reCAPTCHA verification passed!');
  478. Accounts.createUser({
  479. username: formData.username,
  480. email: formData.email,
  481. password: formData.password
  482. });
  483. }
  484. return true;
  485. },
  486. addPlaylistSong: function(type, songData) {
  487. type = type.toLowerCase();
  488. if (Rooms.find({type: type}).count() === 1) {
  489. if (songData !== undefined && Object.keys(songData).length === 4 && songData.type !== undefined && songData.title !== undefined && songData.title !== undefined && songData.artist !== undefined) {
  490. songData.duration = getSongDuration(songData.title);
  491. Playlists.update({type: type}, {$push: {songs: {id: songData.id, title: songData.title, artist: songData.artist, duration: songData.duration, type: songData.type}}});
  492. return true;
  493. } else {
  494. throw new Meteor.error(403, "Invalid data.");
  495. }
  496. } else {
  497. throw new Meteor.error(403, "Invalid genre.");
  498. }
  499. },
  500. createRoom: function(type) {
  501. if (Rooms.find({type: type}).count() === 0) {
  502. Rooms.insert({type: type}, function(err) {
  503. if (err) {
  504. throw err;
  505. } else {
  506. if (Playlists.find({type: type}).count() === 1) {
  507. if (History.find({type: type}).count() === 0) {
  508. History.insert({type: type, history: []}, function(err3) {
  509. if (err3) {
  510. throw err3;
  511. } else {
  512. startStation();
  513. return true;
  514. }
  515. });
  516. } else {
  517. startStation();
  518. return true;
  519. }
  520. } else {
  521. Playlists.insert({type: type, songs: getSongsByType(type)}, function (err2) {
  522. if (err2) {
  523. throw err2;
  524. } else {
  525. if (History.find({type: type}).count() === 0) {
  526. History.insert({type: type, history: []}, function(err3) {
  527. if (err3) {
  528. throw err3;
  529. } else {
  530. startStation();
  531. return true;
  532. }
  533. });
  534. } else {
  535. startStation();
  536. return true;
  537. }
  538. }
  539. });
  540. }
  541. }
  542. });
  543. } else {
  544. throw "Room already exists";
  545. }
  546. function startStation() {
  547. var startedAt = Date.now();
  548. var songs = Playlists.find({type: type}).fetch()[0].songs;
  549. var currentSong = 0;
  550. addToHistory(songs[currentSong], startedAt);
  551. function addToHistory(song, startedAt) {
  552. History.update({type: type}, {$push: {history: {song: song, started: startedAt}}});
  553. }
  554. function skipSong() {
  555. songs = Playlists.find({type: type}).fetch()[0].songs;
  556. if (currentSong < (songs.length - 1)) {
  557. currentSong++;
  558. } else currentSong = 0;
  559. songTimer();
  560. addToHistory(songs[currentSong], startedAt);
  561. }
  562. function songTimer() {
  563. startedAt = Date.now();
  564. Meteor.setTimeout(function() {
  565. skipSong();
  566. }, songs[currentSong].duration * 1000);
  567. }
  568. songTimer();
  569. }
  570. }
  571. });
  572. }
  573. Router.route("/", {
  574. template: "home"
  575. });
  576. Router.route("/admin", {
  577. template: "admin"
  578. });
  579. Router.route("/:type", {
  580. template: "room"
  581. });