app.js 14 KB

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