app.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. History = new Mongo.Collection("history");
  2. Playlists = new Mongo.Collection("playlists");
  3. if (Meteor.isClient) {
  4. Meteor.startup(function() {
  5. reCAPTCHA.config({
  6. publickey: '6LcVxg0TAAAAAE18vBiH00UAyaJggsmLm890SjZl'
  7. });
  8. });
  9. var hpSound = undefined;
  10. var songsArr = [];
  11. var ytArr = [];
  12. var _sound = undefined;
  13. var parts = location.href.split('/');
  14. var id = parts.pop();
  15. var type = id.toLowerCase();
  16. Template.register.events({
  17. "submit form": function(e){
  18. e.preventDefault();
  19. var username = e.target.registerUsername.value;
  20. var email = e.target.registerEmail.value;
  21. var password = e.target.registerPassword.value;
  22. var captchaData = grecaptcha.getResponse();
  23. Meteor.call("createUserMethod", {username: username, email: email, password: password}, captchaData, function(err, res) {
  24. grecaptcha.reset();
  25. if (err) {
  26. console.log(err);
  27. } else {
  28. Meteor.loginWithPassword(username, password);
  29. }
  30. });
  31. },
  32. "click #facebook-login": function(){
  33. Meteor.loginWithFacebook()
  34. },
  35. "click #github-login": function(){
  36. Meteor.loginWithGithub()
  37. },
  38. "click #login": function(){
  39. $("#register-view").hide();
  40. $("#login-view").show();
  41. }
  42. });
  43. Template.login.events({
  44. "submit form": function(e){
  45. e.preventDefault();
  46. var username = e.target.loginUsername.value;
  47. var password = e.target.loginPassword.value;
  48. Meteor.loginWithPassword(username, password);
  49. Accounts.onLoginFailure(function(){
  50. $("input").css("background-color","indianred").addClass("animated shake");
  51. $("input").on("click",function(){
  52. $("input").css({
  53. "background-color": "transparent",
  54. "width": "250px"
  55. });
  56. })
  57. });
  58. },
  59. "click #facebook-login": function(){
  60. Meteor.loginWithFacebook()
  61. },
  62. "click #github-login": function(){
  63. Meteor.loginWithGithub()
  64. },
  65. "click #register": function(){
  66. $("#login-view").hide();
  67. $("#register-view").show();
  68. }
  69. });
  70. Template.dashboard.events({
  71. "click .logout": function(e){
  72. e.preventDefault();
  73. Meteor.logout();
  74. if (hpSound !== undefined) {
  75. hpSound.stop();
  76. }
  77. },
  78. "click .button-tunein": function(){
  79. SC.stream("/tracks/172055891/", function(sound){
  80. sound._player._volume = 0.3;
  81. sound.play();
  82. });
  83. },
  84. "click #play": function(){
  85. $("#play").hide();
  86. SC.stream("/tracks/172055891/", function(sound){
  87. hpSound = sound;
  88. sound._player._volume = 0.3;
  89. sound.play();
  90. $("#stop").on("click", function(){
  91. $("#stop").hide();
  92. $("#play").show();
  93. sound._player.pause();
  94. })
  95. });
  96. $("#stop").show();
  97. }
  98. });
  99. Template.room.events({
  100. "click #search-song": function(){
  101. $("#song-results").empty()
  102. $.ajax({
  103. type: "GET",
  104. url: "https://www.googleapis.com/youtube/v3/search?part=snippet&q=" + $("#song-input").val() + "&key=AIzaSyAgBdacEWrHCHVPPM4k-AFM7uXg-Q__YXY",
  105. applicationType: "application/json",
  106. contentType: "json",
  107. success: function(data){
  108. console.log(data);
  109. for(var i in data.items){
  110. $("#song-results").append("<p>" + data.items[i].snippet.title + "</p>")
  111. ytArr.push({title: data.items[i].snippet.title, id: data.items[i].id.videoId});
  112. }
  113. console.log(ytArr);
  114. $("#song-results p").click(function(){
  115. var title = $(this).text();
  116. for(var i in ytArr){
  117. if(ytArr[i].title === title){
  118. console.log(ytArr[i].title)
  119. var songObj = {
  120. id: ytArr[i].id,
  121. title: ytArr[i].title,
  122. type: "youtube"
  123. }
  124. /*Meteor.call("addToPlaylist", songObj, function(err,res){
  125. console.log(res);
  126. })*/
  127. }
  128. }
  129. })
  130. }
  131. })
  132. SC.get('/tracks', { q: $("#song-input").val()}, function(tracks) {
  133. console.log(tracks);
  134. for(var i in tracks){
  135. $("#song-results").append("<p>" + tracks[i].title + "</p>")
  136. songsArr.push({title: tracks[i].title, id: tracks[i].id, duration: tracks[i].duration / 1000});
  137. }
  138. $("#song-results p").click(function(){
  139. var title = $(this).text();
  140. for(var i in songsArr){
  141. if(songsArr[i].title === title){
  142. var id = songsArr[i].id;
  143. var duration = songsArr[i].duration;
  144. var songObj = {
  145. title: songsArr[i].title,
  146. id: id,
  147. duration: duration,
  148. type: "soundcloud"
  149. }
  150. }
  151. }
  152. console.log(id);
  153. /*Meteor.call("addToPlaylist", songObj, function(err,res){
  154. return true;
  155. });*/
  156. // if (_sound !== undefined)_sound.stop();
  157. // SC.stream("/tracks/" + id, function(sound){
  158. // _sound = sound;
  159. // sound._player._volume = 0.3;
  160. // sound.play()
  161. // });
  162. })
  163. });
  164. }
  165. });
  166. Template.room.helpers({
  167. type: function() {
  168. var parts = location.href.split('/');
  169. var id = parts.pop();
  170. return id.toUpperCase();
  171. },
  172. title: function(){
  173. return Session.get("title");
  174. },
  175. artist: function(){
  176. return Session.get("artist");
  177. }
  178. });
  179. Template.playlist.helpers({
  180. playlist_songs: function() {
  181. var data = Playlists.find({type: type}).fetch();
  182. if (data !== undefined && data.length > 0) {
  183. return data[0].songs;
  184. } else {
  185. return [];
  186. }
  187. }
  188. });
  189. Template.room.onCreated(function () {
  190. var tag = document.createElement("script");
  191. tag.src = "https://www.youtube.com/iframe_api";
  192. var firstScriptTag = document.getElementsByTagName('script')[0];
  193. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  194. var currentSong = undefined;
  195. var _sound = undefined;
  196. var yt_player = undefined;
  197. var size = 0;
  198. var artistStr;
  199. var temp = "";
  200. function getTimeElapsed() {
  201. if (currentSong !== undefined) {
  202. return Date.now() - currentSong.started;
  203. }
  204. return 0;
  205. }
  206. function getSongInfo(query, type){
  207. var search = query;
  208. var titles = [];
  209. var artists = [];
  210. query = query.toLowerCase().split(" ").join("%20");
  211. $.ajax({
  212. type: "GET",
  213. url: 'https://api.spotify.com/v1/search?q=' + query + '&type=track',
  214. applicationType: "application/json",
  215. contentType: "json",
  216. success: function(data){
  217. console.log(data);
  218. for(var i in data){
  219. for(var j in data[i].items){
  220. if(search.indexOf(data[i].items[j].name) !== -1){
  221. console.log(data[i].items[j].name);
  222. titles.push(data[i].items[j].name);
  223. var info = data[i].items[j];
  224. Session.set("title", titles[0]);
  225. console.log("Info: " + info);
  226. if(type === "youtube"){
  227. Session.set("duration", data[i].items[j].duration_ms / 1000)
  228. }
  229. temp = "";
  230. if(data[i].items[j].artists.length >= 2){
  231. for(var k in data[i].items[j].artists){
  232. temp = temp + data[i].items[j].artists[k].name + ", ";
  233. }
  234. } else{
  235. for(var k in data[i].items[j].artists){
  236. temp = temp + data[i].items[j].artists[k].name;
  237. }
  238. }
  239. if(temp[temp.length-2] === ","){
  240. artistStr = temp.substr(0,temp.length-2);
  241. } else{
  242. artistStr = temp;
  243. }
  244. Session.set("artist", artistStr);
  245. $("#albumart").remove();
  246. $(".room-title").before("<img id='albumart' src='" + data[i].items[j].album.images[1].url + "' />")
  247. return true;
  248. }
  249. }
  250. //---------------------------------------------------------------//
  251. }
  252. }
  253. })
  254. }
  255. function resizeSeekerbar() {
  256. $("#seeker-bar").width(((getTimeElapsed() / 1000) / Session.get("duration") * 100) + "%");
  257. }
  258. function startSong() {
  259. if (currentSong !== undefined) {
  260. if (_sound !== undefined) _sound.stop();
  261. if (yt_player !== undefined && yt_player.stopVideo !== undefined) yt_player.stopVideo();
  262. if (currentSong.song.type === "soundcloud") {
  263. $("#player").attr("src", "")
  264. getSongInfo(currentSong.song.title);
  265. SC.stream("/tracks/" + currentSong.song.id + "#t=20s", function(sound){
  266. console.log(sound);
  267. _sound = sound;
  268. sound._player._volume = 0.3;
  269. sound.play();
  270. console.log(getTimeElapsed());
  271. var interval = setInterval(function() {
  272. if (sound.getState() === "playing") {
  273. sound.seek(getTimeElapsed());
  274. window.clearInterval(interval);
  275. }
  276. }, 200);
  277. // Session.set("title", currentSong.song.title || "Title");
  278. // Session.set("artist", currentSong.song.artist || "Artist");
  279. Session.set("duration", currentSong.song.duration);
  280. resizeSeekerbar();
  281. });
  282. } else {
  283. if (yt_player === undefined) {
  284. yt_player = new YT.Player("player", {
  285. height: 540,
  286. width: 960,
  287. videoId: currentSong.song.id,
  288. events: {
  289. 'onReady': function(event) {
  290. event.target.seekTo(getTimeElapsed() / 1000);
  291. event.target.playVideo();
  292. resizeSeekerbar();
  293. },
  294. 'onStateChange': function(event){
  295. if (event.data == YT.PlayerState.PAUSED) {
  296. event.target.seekTo(getTimeElapsed() / 1000);
  297. event.target.playVideo();
  298. }
  299. }
  300. }
  301. });
  302. } else {
  303. yt_player.loadVideoById(currentSong.song.id);
  304. }
  305. // Session.set("title", currentSong.song.title || "Title");
  306. // Session.set("artist", currentSong.song.artist || "Artist");
  307. getSongInfo(currentSong.song.title, "youtube");
  308. //Session.set("duration", currentSong.song.duration);
  309. }
  310. }
  311. }
  312. Meteor.subscribe("history");
  313. Meteor.subscribe("playlists");
  314. var room_types = ["edm", "nightcore"];
  315. if (room_types.indexOf(type) === -1) {
  316. window.location = "/";
  317. }
  318. Meteor.setInterval(function() {
  319. var data = undefined;
  320. var dataCursor = History.find({type: type});
  321. dataCursor.map(function(doc) {
  322. if (data === undefined) {
  323. data = doc;
  324. }
  325. });
  326. if (data !== undefined && data.history.length > size) {
  327. currentSong = data.history[data.history.length-1];
  328. size = data.history.length;
  329. startSong();
  330. }
  331. }, 1000);
  332. Meteor.setInterval(function() {
  333. resizeSeekerbar();
  334. }, 50);
  335. });
  336. }
  337. if (Meteor.isServer) {
  338. Meteor.startup(function() {
  339. reCAPTCHA.config({
  340. privatekey: '6LcVxg0TAAAAAI2fgIEEWHFxwNXeVIs8mzq5cfRM'
  341. });
  342. });
  343. Meteor.users.deny({update: function () { return true; }});
  344. Meteor.users.deny({insert: function () { return true; }});
  345. Meteor.users.deny({remove: function () { return true; }});
  346. function getSongDuration(query){
  347. var duration;
  348. search = query;
  349. query = query.toLowerCase().split(" ").join("%20");
  350. var res = Meteor.http.get('https://api.spotify.com/v1/search?q=' + query + '&type=track');
  351. for(var i in res.data){
  352. for(var j in res.data[i].items){
  353. if(search.indexOf(res.data[i].items[j].name) !== -1){
  354. duration = res.data[i].items[j].duration_ms / 1000;
  355. }
  356. }
  357. }
  358. console.log(duration);
  359. return duration;
  360. }
  361. var room_types = ["edm", "nightcore"];
  362. room_types.forEach(function(type) {
  363. if (Playlists.find({type: type}).fetch().length === 0) {
  364. if (type === "edm") {
  365. 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"}]});
  366. } else if (type === "nightcore") {
  367. Playlists.insert({type: type, songs: [{id: "7vQRbHY9CAU", title: "Monster (DotEXE Remix)", duration: getSongDuration("Monster (DotEXE Remix)") , type: "youtube"}]});
  368. }
  369. }
  370. if (History.find({type: type}).fetch().length === 0) {
  371. History.insert({type: type, history: []});
  372. }
  373. var startedAt = Date.now();
  374. var songs = Playlists.find({type: type}).fetch()[0].songs;
  375. var currentSong = 0;
  376. addToHistory(songs[currentSong], startedAt);
  377. function addToHistory(song, startedAt) {
  378. History.update({type: type}, {$push: {history: {song: song, started: startedAt}}});
  379. }
  380. function skipSong() {
  381. if (currentSong < (songs.length - 1)) {
  382. currentSong++;
  383. } else currentSong = 0;
  384. songTimer();
  385. addToHistory(songs[currentSong], startedAt);
  386. }
  387. function songTimer() {
  388. startedAt = Date.now();
  389. Meteor.setTimeout(function() {
  390. skipSong();
  391. }, songs[currentSong].duration * 1000);
  392. }
  393. songTimer();
  394. });
  395. ServiceConfiguration.configurations.remove({
  396. service: "facebook"
  397. });
  398. ServiceConfiguration.configurations.insert({
  399. service: "facebook",
  400. appId: "1496014310695890",
  401. secret: "9a039f254a08a1488c08bb0737dbd2a6"
  402. });
  403. ServiceConfiguration.configurations.remove({
  404. service: "github"
  405. });
  406. ServiceConfiguration.configurations.insert({
  407. service: "github",
  408. clientId: "dcecd720f47c0e4001f7",
  409. secret: "375939d001ef1a0ca67c11dbf8fb9aeaa551e01b"
  410. });
  411. Meteor.publish("history", function() {
  412. return History.find({})
  413. });
  414. Meteor.publish("playlists", function() {
  415. return Playlists.find({})
  416. });
  417. Meteor.methods({
  418. createUserMethod: function(formData, captchaData) {
  419. var verifyCaptchaResponse = reCAPTCHA.verifyCaptcha(this.connection.clientAddress, captchaData);
  420. if (!verifyCaptchaResponse.success) {
  421. console.log('reCAPTCHA check failed!', verifyCaptchaResponse);
  422. throw new Meteor.Error(422, 'reCAPTCHA Failed: ' + verifyCaptchaResponse.error);
  423. } else {
  424. console.log('reCAPTCHA verification passed!');
  425. Accounts.createUser({
  426. username: formData.username,
  427. email: formData.email,
  428. password: formData.password
  429. });
  430. }
  431. return true;
  432. }
  433. });
  434. }
  435. Router.route("/", {
  436. template: "home"
  437. });
  438. Router.route("/:type", {
  439. template: "room"
  440. });