app.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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 .button-tunein": function(){
  80. SC.stream("/tracks/172055891/", function(sound){
  81. sound._player._volume = 0.3;
  82. sound.play();
  83. });
  84. },
  85. "click #play": function(){
  86. $("#play").hide();
  87. SC.stream("/tracks/172055891/", function(sound){
  88. hpSound = sound;
  89. sound._player._volume = 0.3;
  90. sound.play();
  91. $("#stop").on("click", function(){
  92. $("#stop").hide();
  93. $("#play").show();
  94. sound._player.pause();
  95. })
  96. });
  97. $("#stop").show();
  98. }
  99. });
  100. Template.room.events({
  101. "click #search-song": function(){
  102. $("#song-results").empty()
  103. $.ajax({
  104. type: "GET",
  105. url: "https://www.googleapis.com/youtube/v3/search?part=snippet&q=" + $("#song-input").val() + "&key=AIzaSyAgBdacEWrHCHVPPM4k-AFM7uXg-Q__YXY",
  106. applicationType: "application/json",
  107. contentType: "json",
  108. success: function(data){
  109. console.log(data);
  110. for(var i in data.items){
  111. $("#song-results").append("<p>" + data.items[i].snippet.title + "</p>")
  112. ytArr.push({title: data.items[i].snippet.title, id: data.items[i].id.videoId});
  113. }
  114. console.log(ytArr);
  115. $("#song-results p").click(function(){
  116. var title = $(this).text();
  117. for(var i in ytArr){
  118. if(ytArr[i].title === title){
  119. console.log(ytArr[i].title)
  120. var songObj = {
  121. id: ytArr[i].id,
  122. title: ytArr[i].title,
  123. type: "youtube"
  124. }
  125. }
  126. }
  127. })
  128. }
  129. })
  130. SC.get('/tracks', { q: $("#song-input").val()}, function(tracks) {
  131. console.log(tracks);
  132. for(var i in tracks){
  133. $("#song-results").append("<p>" + tracks[i].title + "</p>")
  134. songsArr.push({title: tracks[i].title, id: tracks[i].id, duration: tracks[i].duration / 1000});
  135. }
  136. $("#song-results p").click(function(){
  137. var title = $(this).text();
  138. for(var i in songsArr){
  139. if(songsArr[i].title === title){
  140. var id = songsArr[i].id;
  141. var duration = songsArr[i].duration;
  142. var songObj = {
  143. title: songsArr[i].title,
  144. id: id,
  145. duration: duration,
  146. type: "soundcloud"
  147. }
  148. }
  149. }
  150. console.log(id);
  151. })
  152. });
  153. }
  154. });
  155. Template.room.helpers({
  156. type: function() {
  157. var parts = location.href.split('/');
  158. var id = parts.pop();
  159. return id.toUpperCase();
  160. },
  161. title: function(){
  162. return Session.get("title");
  163. },
  164. artist: function(){
  165. return Session.get("artist");
  166. },
  167. loaded: function() {
  168. return Session.get("loaded");
  169. }
  170. });
  171. Template.playlist.helpers({
  172. playlist_songs: function() {
  173. var data = Playlists.find({type: type}).fetch();
  174. if (data !== undefined && data.length > 0) {
  175. return data[0].songs;
  176. } else {
  177. return [];
  178. }
  179. }
  180. });
  181. Template.room.onCreated(function () {
  182. var tag = document.createElement("script");
  183. tag.src = "https://www.youtube.com/iframe_api";
  184. var firstScriptTag = document.getElementsByTagName('script')[0];
  185. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  186. var currentSong = undefined;
  187. var _sound = undefined;
  188. var yt_player = undefined;
  189. var size = 0;
  190. var artistStr;
  191. var temp = "";
  192. function getTimeElapsed() {
  193. if (currentSong !== undefined) {
  194. return Date.now() - currentSong.started;
  195. }
  196. return 0;
  197. }
  198. function getSongInfo(query, type){
  199. var search = query;
  200. var titles = [];
  201. var artists = [];
  202. query = query.toLowerCase().split(" ").join("%20");
  203. $.ajax({
  204. type: "GET",
  205. url: 'https://api.spotify.com/v1/search?q=' + query + '&type=track',
  206. applicationType: "application/json",
  207. contentType: "json",
  208. success: function(data){
  209. console.log(data);
  210. for(var i in data){
  211. for(var j in data[i].items){
  212. if(search.indexOf(data[i].items[j].name) !== -1){
  213. console.log(data[i].items[j].name);
  214. titles.push(data[i].items[j].name);
  215. var info = data[i].items[j];
  216. Session.set("title", titles[0]);
  217. console.log("Info: " + info);
  218. if(type === "youtube"){
  219. Session.set("duration", data[i].items[j].duration_ms / 1000)
  220. }
  221. temp = "";
  222. if(data[i].items[j].artists.length >= 2){
  223. for(var k in data[i].items[j].artists){
  224. temp = temp + data[i].items[j].artists[k].name + ", ";
  225. }
  226. } else{
  227. for(var k in data[i].items[j].artists){
  228. temp = temp + data[i].items[j].artists[k].name;
  229. }
  230. }
  231. if(temp[temp.length-2] === ","){
  232. artistStr = temp.substr(0,temp.length-2);
  233. } else{
  234. artistStr = temp;
  235. }
  236. Session.set("artist", artistStr);
  237. $("#albumart").remove();
  238. $(".room-title").before("<img id='albumart' src='" + data[i].items[j].album.images[1].url + "' />")
  239. return true;
  240. }
  241. }
  242. //---------------------------------------------------------------//
  243. }
  244. }
  245. })
  246. }
  247. function resizeSeekerbar() {
  248. $("#seeker-bar").width(((getTimeElapsed() / 1000) / Session.get("duration") * 100) + "%");
  249. }
  250. function startSong() {
  251. if (currentSong !== undefined) {
  252. if (_sound !== undefined) _sound.stop();
  253. if (yt_player !== undefined && yt_player.stopVideo !== undefined) yt_player.stopVideo();
  254. if (currentSong.song.type === "soundcloud") {
  255. $("#player").attr("src", "")
  256. getSongInfo(currentSong.song.title);
  257. SC.stream("/tracks/" + currentSong.song.id + "#t=20s", function(sound){
  258. console.log(sound);
  259. _sound = sound;
  260. sound._player._volume = 0.3;
  261. sound.play();
  262. console.log(getTimeElapsed());
  263. var interval = setInterval(function() {
  264. if (sound.getState() === "playing") {
  265. sound.seek(getTimeElapsed());
  266. window.clearInterval(interval);
  267. }
  268. }, 200);
  269. // Session.set("title", currentSong.song.title || "Title");
  270. // Session.set("artist", currentSong.song.artist || "Artist");
  271. Session.set("duration", currentSong.song.duration);
  272. resizeSeekerbar();
  273. });
  274. } else {
  275. if (yt_player === undefined) {
  276. yt_player = new YT.Player("player", {
  277. height: 540,
  278. width: 960,
  279. videoId: currentSong.song.id,
  280. events: {
  281. 'onReady': function(event) {
  282. event.target.seekTo(getTimeElapsed() / 1000);
  283. event.target.playVideo();
  284. resizeSeekerbar();
  285. },
  286. 'onStateChange': function(event){
  287. if (event.data == YT.PlayerState.PAUSED) {
  288. event.target.seekTo(getTimeElapsed() / 1000);
  289. event.target.playVideo();
  290. }
  291. }
  292. }
  293. });
  294. } else {
  295. yt_player.loadVideoById(currentSong.song.id);
  296. }
  297. // Session.set("title", currentSong.song.title || "Title");
  298. // Session.set("artist", currentSong.song.artist || "Artist");
  299. getSongInfo(currentSong.song.title, "youtube");
  300. //Session.set("duration", currentSong.song.duration);
  301. }
  302. }
  303. }
  304. Meteor.subscribe("history");
  305. Meteor.subscribe("playlists");
  306. Meteor.subscribe("rooms", function() {
  307. Session.set("loaded", false);
  308. if (Rooms.find({type: type}).fetch().length !== 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. }
  366. }
  367. }
  368. return duration;
  369. }
  370. //var room_types = ["edm", "nightcore"];
  371. var songsArr = [];
  372. Rooms.find({}).fetch().forEach(function(room) {
  373. var type = room.type;
  374. if (Playlists.find({type: type}).fetch().length === 0) {
  375. if (type === "edm") {
  376. Playlists.insert({type: type, songs: [{id: "aE2GCa-_nyU", title: "Radioactive - Lindsey Stirling and Pentatonix", duration: getSongDuration("Radioactive - Lindsey Stirling and Pentatonix"), type: "youtube"}, {id: "aHjpOzsQ9YI", title: "Crystallize", artist: "Linsdey Stirling", duration: getSongDuration("Crystallize"), type: "youtube"}]});
  377. } else if (type === "nightcore") {
  378. Playlists.insert({type: type, songs: [{id: "7vQRbHY9CAU", title: "Monster (DotEXE Remix)", duration: getSongDuration("Monster (DotEXE Remix)") , type: "youtube"}]});
  379. }
  380. }
  381. if (History.find({type: type}).fetch().length === 0) {
  382. History.insert({type: type, history: []});
  383. }
  384. var startedAt = Date.now();
  385. var songs = Playlists.find({type: type}).fetch()[0].songs;
  386. var currentSong = 0;
  387. addToHistory(songs[currentSong], startedAt);
  388. function addToHistory(song, startedAt) {
  389. History.update({type: type}, {$push: {history: {song: song, started: startedAt}}});
  390. }
  391. function skipSong() {
  392. songs = Playlists.find({type: type}).fetch()[0].songs;
  393. if (currentSong < (songs.length - 1)) {
  394. currentSong++;
  395. } else currentSong = 0;
  396. songTimer();
  397. addToHistory(songs[currentSong], startedAt);
  398. }
  399. function songTimer() {
  400. startedAt = Date.now();
  401. Meteor.setTimeout(function() {
  402. skipSong();
  403. }, songs[currentSong].duration * 1000);
  404. }
  405. songTimer();
  406. });
  407. ServiceConfiguration.configurations.remove({
  408. service: "facebook"
  409. });
  410. ServiceConfiguration.configurations.insert({
  411. service: "facebook",
  412. appId: "1496014310695890",
  413. secret: "9a039f254a08a1488c08bb0737dbd2a6"
  414. });
  415. ServiceConfiguration.configurations.remove({
  416. service: "github"
  417. });
  418. ServiceConfiguration.configurations.insert({
  419. service: "github",
  420. clientId: "dcecd720f47c0e4001f7",
  421. secret: "375939d001ef1a0ca67c11dbf8fb9aeaa551e01b"
  422. });
  423. Meteor.publish("history", function() {
  424. return History.find({})
  425. });
  426. Meteor.publish("playlists", function() {
  427. return Playlists.find({})
  428. });
  429. Meteor.publish("rooms", function() {
  430. return Rooms.find()
  431. });
  432. Meteor.methods({
  433. createUserMethod: function(formData, captchaData) {
  434. var verifyCaptchaResponse = reCAPTCHA.verifyCaptcha(this.connection.clientAddress, captchaData);
  435. if (!verifyCaptchaResponse.success) {
  436. console.log('reCAPTCHA check failed!', verifyCaptchaResponse);
  437. throw new Meteor.Error(422, 'reCAPTCHA Failed: ' + verifyCaptchaResponse.error);
  438. } else {
  439. console.log('reCAPTCHA verification passed!');
  440. Accounts.createUser({
  441. username: formData.username,
  442. email: formData.email,
  443. password: formData.password
  444. });
  445. }
  446. return true;
  447. },
  448. addPlaylistSong: function(type, songData) {
  449. type = type.toLowerCase();
  450. if (Rooms.find({type: type}).fetch().length === 1) {
  451. if (songData !== undefined && Object.keys(songData).length === 4 && songData.type !== undefined && songData.title !== undefined && songData.title !== undefined && songData.artist !== undefined) {
  452. songData.duration = getSongDuration(songData.title);
  453. Playlists.update({type: type}, {$push: {songs: {id: songData.id, title: songData.title, artist: songData.artist, duration: songData.duration, type: songData.type}}});
  454. return true;
  455. } else {
  456. throw new Meteor.error(403, "Invalid data.");
  457. }
  458. } else {
  459. throw new Meteor.error(403, "Invalid genre.");
  460. }
  461. }
  462. });
  463. }
  464. Router.route("/", {
  465. template: "home"
  466. });
  467. Router.route("/admin", {
  468. template: "admin"
  469. });
  470. Router.route("/:type", {
  471. template: "room"
  472. });