app.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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. }
  125. }
  126. })
  127. }
  128. })
  129. SC.get('/tracks', { q: $("#song-input").val()}, function(tracks) {
  130. console.log(tracks);
  131. for(var i in tracks){
  132. $("#song-results").append("<p>" + tracks[i].title + "</p>")
  133. songsArr.push({title: tracks[i].title, id: tracks[i].id, duration: tracks[i].duration / 1000});
  134. }
  135. $("#song-results p").click(function(){
  136. var title = $(this).text();
  137. for(var i in songsArr){
  138. if(songsArr[i].title === title){
  139. var id = songsArr[i].id;
  140. var duration = songsArr[i].duration;
  141. var songObj = {
  142. title: songsArr[i].title,
  143. id: id,
  144. duration: duration,
  145. type: "soundcloud"
  146. }
  147. }
  148. }
  149. console.log(id);
  150. })
  151. });
  152. }
  153. });
  154. Template.room.helpers({
  155. type: function() {
  156. var parts = location.href.split('/');
  157. var id = parts.pop();
  158. return id.toUpperCase();
  159. },
  160. title: function(){
  161. return Session.get("title");
  162. },
  163. artist: function(){
  164. return Session.get("artist");
  165. }
  166. });
  167. Template.playlist.helpers({
  168. playlist_songs: function() {
  169. var data = Playlists.find({type: type}).fetch();
  170. if (data !== undefined && data.length > 0) {
  171. return data[0].songs;
  172. } else {
  173. return [];
  174. }
  175. }
  176. });
  177. Template.room.onCreated(function () {
  178. var tag = document.createElement("script");
  179. tag.src = "https://www.youtube.com/iframe_api";
  180. var firstScriptTag = document.getElementsByTagName('script')[0];
  181. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  182. var currentSong = undefined;
  183. var _sound = undefined;
  184. var yt_player = undefined;
  185. var size = 0;
  186. var artistStr;
  187. var temp = "";
  188. function getTimeElapsed() {
  189. if (currentSong !== undefined) {
  190. return Date.now() - currentSong.started;
  191. }
  192. return 0;
  193. }
  194. function getSongInfo(query, type){
  195. var search = query;
  196. var titles = [];
  197. var artists = [];
  198. query = query.toLowerCase().split(" ").join("%20");
  199. $.ajax({
  200. type: "GET",
  201. url: 'https://api.spotify.com/v1/search?q=' + query + '&type=track',
  202. applicationType: "application/json",
  203. contentType: "json",
  204. success: function(data){
  205. console.log(data);
  206. for(var i in data){
  207. for(var j in data[i].items){
  208. if(search.indexOf(data[i].items[j].name) !== -1){
  209. console.log(data[i].items[j].name);
  210. titles.push(data[i].items[j].name);
  211. var info = data[i].items[j];
  212. Session.set("title", titles[0]);
  213. console.log("Info: " + info);
  214. if(type === "youtube"){
  215. Session.set("duration", data[i].items[j].duration_ms / 1000)
  216. }
  217. temp = "";
  218. if(data[i].items[j].artists.length >= 2){
  219. for(var k in data[i].items[j].artists){
  220. temp = temp + data[i].items[j].artists[k].name + ", ";
  221. }
  222. } else{
  223. for(var k in data[i].items[j].artists){
  224. temp = temp + data[i].items[j].artists[k].name;
  225. }
  226. }
  227. if(temp[temp.length-2] === ","){
  228. artistStr = temp.substr(0,temp.length-2);
  229. } else{
  230. artistStr = temp;
  231. }
  232. Session.set("artist", artistStr);
  233. $("#albumart").remove();
  234. $(".room-title").before("<img id='albumart' src='" + data[i].items[j].album.images[1].url + "' />")
  235. return true;
  236. }
  237. }
  238. //---------------------------------------------------------------//
  239. }
  240. }
  241. })
  242. }
  243. function resizeSeekerbar() {
  244. $("#seeker-bar").width(((getTimeElapsed() / 1000) / Session.get("duration") * 100) + "%");
  245. }
  246. function startSong() {
  247. if (currentSong !== undefined) {
  248. if (_sound !== undefined) _sound.stop();
  249. if (yt_player !== undefined && yt_player.stopVideo !== undefined) yt_player.stopVideo();
  250. if (currentSong.song.type === "soundcloud") {
  251. $("#player").attr("src", "")
  252. getSongInfo(currentSong.song.title);
  253. SC.stream("/tracks/" + currentSong.song.id + "#t=20s", function(sound){
  254. console.log(sound);
  255. _sound = sound;
  256. sound._player._volume = 0.3;
  257. sound.play();
  258. console.log(getTimeElapsed());
  259. var interval = setInterval(function() {
  260. if (sound.getState() === "playing") {
  261. sound.seek(getTimeElapsed());
  262. window.clearInterval(interval);
  263. }
  264. }, 200);
  265. // Session.set("title", currentSong.song.title || "Title");
  266. // Session.set("artist", currentSong.song.artist || "Artist");
  267. Session.set("duration", currentSong.song.duration);
  268. resizeSeekerbar();
  269. });
  270. } else {
  271. if (yt_player === undefined) {
  272. yt_player = new YT.Player("player", {
  273. height: 540,
  274. width: 960,
  275. videoId: currentSong.song.id,
  276. events: {
  277. 'onReady': function(event) {
  278. event.target.seekTo(getTimeElapsed() / 1000);
  279. event.target.playVideo();
  280. resizeSeekerbar();
  281. },
  282. 'onStateChange': function(event){
  283. if (event.data == YT.PlayerState.PAUSED) {
  284. event.target.seekTo(getTimeElapsed() / 1000);
  285. event.target.playVideo();
  286. }
  287. }
  288. }
  289. });
  290. } else {
  291. yt_player.loadVideoById(currentSong.song.id);
  292. }
  293. // Session.set("title", currentSong.song.title || "Title");
  294. // Session.set("artist", currentSong.song.artist || "Artist");
  295. getSongInfo(currentSong.song.title, "youtube");
  296. //Session.set("duration", currentSong.song.duration);
  297. }
  298. }
  299. }
  300. Meteor.subscribe("history");
  301. Meteor.subscribe("playlists");
  302. var room_types = ["edm", "nightcore"];
  303. if (room_types.indexOf(type) === -1) {
  304. window.location = "/";
  305. }
  306. Meteor.setInterval(function() {
  307. var data = undefined;
  308. var dataCursor = History.find({type: type});
  309. dataCursor.map(function(doc) {
  310. if (data === undefined) {
  311. data = doc;
  312. }
  313. });
  314. if (data !== undefined && data.history.length > size) {
  315. currentSong = data.history[data.history.length-1];
  316. size = data.history.length;
  317. startSong();
  318. }
  319. }, 1000);
  320. Meteor.setInterval(function() {
  321. resizeSeekerbar();
  322. }, 50);
  323. });
  324. }
  325. if (Meteor.isServer) {
  326. Meteor.startup(function() {
  327. reCAPTCHA.config({
  328. privatekey: '6LcVxg0TAAAAAI2fgIEEWHFxwNXeVIs8mzq5cfRM'
  329. });
  330. });
  331. Meteor.users.deny({update: function () { return true; }});
  332. Meteor.users.deny({insert: function () { return true; }});
  333. Meteor.users.deny({remove: function () { return true; }});
  334. function getSongDuration(query){
  335. var duration;
  336. var search = query;
  337. query = query.toLowerCase().split(" ").join("%20");
  338. var res = Meteor.http.get('https://api.spotify.com/v1/search?q=' + query + '&type=track');
  339. for(var i in res.data){
  340. for(var j in res.data[i].items){
  341. if(search.indexOf(res.data[i].items[j].name) !== -1){
  342. duration = res.data[i].items[j].duration_ms / 1000;
  343. }
  344. }
  345. }
  346. return duration;
  347. }
  348. var room_types = ["edm", "nightcore"];
  349. room_types.forEach(function(type) {
  350. if (Playlists.find({type: type}).fetch().length === 0) {
  351. if (type === "edm") {
  352. 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"}]});
  353. } else if (type === "nightcore") {
  354. Playlists.insert({type: type, songs: [{id: "7vQRbHY9CAU", title: "Monster (DotEXE Remix)", duration: getSongDuration("Monster (DotEXE Remix)") , type: "youtube"}]});
  355. }
  356. }
  357. if (History.find({type: type}).fetch().length === 0) {
  358. History.insert({type: type, history: []});
  359. }
  360. var startedAt = Date.now();
  361. var songs = Playlists.find({type: type}).fetch()[0].songs;
  362. var currentSong = 0;
  363. addToHistory(songs[currentSong], startedAt);
  364. function addToHistory(song, startedAt) {
  365. History.update({type: type}, {$push: {history: {song: song, started: startedAt}}});
  366. }
  367. function skipSong() {
  368. if (currentSong < (songs.length - 1)) {
  369. currentSong++;
  370. } else currentSong = 0;
  371. songTimer();
  372. addToHistory(songs[currentSong], startedAt);
  373. }
  374. function songTimer() {
  375. startedAt = Date.now();
  376. Meteor.setTimeout(function() {
  377. skipSong();
  378. }, songs[currentSong].duration * 1000);
  379. }
  380. songTimer();
  381. });
  382. ServiceConfiguration.configurations.remove({
  383. service: "facebook"
  384. });
  385. ServiceConfiguration.configurations.insert({
  386. service: "facebook",
  387. appId: "1496014310695890",
  388. secret: "9a039f254a08a1488c08bb0737dbd2a6"
  389. });
  390. ServiceConfiguration.configurations.remove({
  391. service: "github"
  392. });
  393. ServiceConfiguration.configurations.insert({
  394. service: "github",
  395. clientId: "dcecd720f47c0e4001f7",
  396. secret: "375939d001ef1a0ca67c11dbf8fb9aeaa551e01b"
  397. });
  398. Meteor.publish("history", function() {
  399. return History.find({})
  400. });
  401. Meteor.publish("playlists", function() {
  402. return Playlists.find({})
  403. });
  404. Meteor.methods({
  405. createUserMethod: function(formData, captchaData) {
  406. var verifyCaptchaResponse = reCAPTCHA.verifyCaptcha(this.connection.clientAddress, captchaData);
  407. if (!verifyCaptchaResponse.success) {
  408. console.log('reCAPTCHA check failed!', verifyCaptchaResponse);
  409. throw new Meteor.Error(422, 'reCAPTCHA Failed: ' + verifyCaptchaResponse.error);
  410. } else {
  411. console.log('reCAPTCHA verification passed!');
  412. Accounts.createUser({
  413. username: formData.username,
  414. email: formData.email,
  415. password: formData.password
  416. });
  417. }
  418. return true;
  419. }
  420. });
  421. }
  422. Router.route("/", {
  423. template: "home"
  424. });
  425. Router.route("/:type", {
  426. template: "room"
  427. });