app.js 13 KB

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