app.js 23 KB

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