app.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. }
  148. });
  149. Template.room.onCreated(function () {
  150. var tag = document.createElement("script");
  151. tag.src = "https://www.youtube.com/iframe_api";
  152. var firstScriptTag = document.getElementsByTagName('script')[0];
  153. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  154. var currentSong = undefined;
  155. var _sound = undefined;
  156. var yt_player = undefined;
  157. var size = 0;
  158. var timeMS = 0;
  159. function getTimeElapsed() {
  160. if (currentSong !== undefined) {
  161. return Date.now() - currentSong.started;
  162. }
  163. return 0;
  164. }
  165. function getSongInfo(query){
  166. query = query.toLowerCase().split(" ").join("%20");
  167. $.ajax({
  168. type: "GET",
  169. url: 'https://api.spotify.com/v1/search?q=' + query + '&type=track',
  170. applicationType: "application/json",
  171. contentType: "json",
  172. success: function(data){
  173. console.log(data);
  174. for(var i in data){
  175. Session.set("title", data[i].items[0].name);
  176. for(var j in data[i].items[0].artists){
  177. Session.set("artist", data[i].items[0].artists[j].name);
  178. }
  179. $("#albumart").remove();
  180. $(".room-title").before("<img id='albumart' src='" + data[i].items[0].album.images[1].url + "' />")
  181. }
  182. }
  183. })
  184. }
  185. function resizeSeekerbar() {
  186. $("#seeker-bar").width(((getTimeElapsed() / 1000) / Session.get("duration") * 100) + "%");
  187. }
  188. function startSong() {
  189. if (currentSong !== undefined) {
  190. if (_sound !== undefined) _sound.stop();
  191. if (yt_player !== undefined && yt_player.stopVideo !== undefined) yt_player.stopVideo();
  192. if (currentSong.song.type === "soundcloud") {
  193. $("#player").attr("src", "")
  194. getSongInfo(currentSong.song.title);
  195. SC.stream("/tracks/" + currentSong.song.id + "#t=20s", function(sound){
  196. console.log(sound);
  197. _sound = sound;
  198. sound._player._volume = 0.3;
  199. sound.play();
  200. console.log(getTimeElapsed());
  201. var interval = setInterval(function() {
  202. if (sound.getState() === "playing") {
  203. sound.seek(getTimeElapsed());
  204. window.clearInterval(interval);
  205. }
  206. }, 200);
  207. // Session.set("title", currentSong.song.title || "Title");
  208. // Session.set("artist", currentSong.song.artist || "Artist");
  209. Session.set("duration", currentSong.song.duration);
  210. resizeSeekerbar();
  211. });
  212. } else {
  213. if (yt_player === undefined) {
  214. yt_player = new YT.Player("player", {
  215. height: 540,
  216. width: 960,
  217. videoId: currentSong.song.id,
  218. events: {
  219. 'onReady': function(event) {
  220. event.target.seekTo(getTimeElapsed() / 1000);
  221. event.target.playVideo();
  222. resizeSeekerbar();
  223. },
  224. 'onStateChange': function(event){
  225. if (event.data == YT.PlayerState.PAUSED) {
  226. event.target.seekTo(getTimeElapsed() / 1000);
  227. event.target.playVideo();
  228. }
  229. }
  230. }
  231. });
  232. } else {
  233. yt_player.loadVideoById(currentSong.song.id);
  234. }
  235. // Session.set("title", currentSong.song.title || "Title");
  236. // Session.set("artist", currentSong.song.artist || "Artist");
  237. getSongInfo(currentSong.song.title);
  238. Session.set("duration", currentSong.song.duration);
  239. }
  240. }
  241. }
  242. Meteor.subscribe("history");
  243. Meteor.setInterval(function() {
  244. var data = undefined;
  245. var dataCursor = History.find({type: "edm"});
  246. dataCursor.map(function(doc) {
  247. if (data === undefined) {
  248. data = doc;
  249. }
  250. });
  251. if (data.history.length > size) {
  252. currentSong = data.history[data.history.length-1];
  253. size = data.history.length;
  254. startSong();
  255. }
  256. }, 1000);
  257. Meteor.setInterval(function() {
  258. resizeSeekerbar();
  259. }, 50);
  260. });
  261. }
  262. if (Meteor.isServer) {
  263. Meteor.startup(function() {
  264. reCAPTCHA.config({
  265. privatekey: '6LcVxg0TAAAAAI2fgIEEWHFxwNXeVIs8mzq5cfRM'
  266. });
  267. });
  268. Meteor.users.deny({update: function () { return true; }});
  269. Meteor.users.deny({insert: function () { return true; }});
  270. Meteor.users.deny({remove: function () { return true; }});
  271. var startedAt = Date.now();
  272. var songs = [
  273. // {id: "YqeW9_5kURI", title: "Lean On", artist: "Major Lazer", duration: 178, type: "youtube"}
  274. {id: 172055891, title: "Immortals", artist: "Fall Out Boy", duration: 244, type: "soundcloud"}
  275. ];
  276. var currentSong = 0;
  277. addToHistory(songs[currentSong], startedAt);
  278. function addToHistory(song, startedAt) {
  279. History.update({type: "edm"}, {$push: {history: {song: song, started: startedAt}}});
  280. }
  281. function skipSong() {
  282. if (currentSong < (songs.length - 1)) {
  283. currentSong++;
  284. } else currentSong = 0;
  285. songTimer();
  286. addToHistory(songs[currentSong], startedAt);
  287. }
  288. function songTimer() {
  289. startedAt = Date.now();
  290. Meteor.setTimeout(function() {
  291. skipSong();
  292. }, songs[currentSong].duration * 1000);
  293. }
  294. ServiceConfiguration.configurations.remove({
  295. service: "facebook"
  296. });
  297. ServiceConfiguration.configurations.insert({
  298. service: "facebook",
  299. appId: "1496014310695890",
  300. secret: "9a039f254a08a1488c08bb0737dbd2a6"
  301. });
  302. ServiceConfiguration.configurations.remove({
  303. service: "github"
  304. });
  305. ServiceConfiguration.configurations.insert({
  306. service: "github",
  307. clientId: "dcecd720f47c0e4001f7",
  308. secret: "375939d001ef1a0ca67c11dbf8fb9aeaa551e01b"
  309. });
  310. songTimer();
  311. Meteor.publish("history", function() {
  312. return History.find({type: "edm"})
  313. });
  314. Meteor.methods({
  315. createUserMethod: function(formData, captchaData) {
  316. var verifyCaptchaResponse = reCAPTCHA.verifyCaptcha(this.connection.clientAddress, captchaData);
  317. if (!verifyCaptchaResponse.success) {
  318. console.log('reCAPTCHA check failed!', verifyCaptchaResponse);
  319. throw new Meteor.Error(422, 'reCAPTCHA Failed: ' + verifyCaptchaResponse.error);
  320. } else {
  321. console.log('reCAPTCHA verification passed!');
  322. Accounts.createUser({
  323. username: formData.username,
  324. email: formData.email,
  325. password: formData.password
  326. });
  327. }
  328. return true;
  329. },
  330. addToPlaylist: function(songObj){
  331. songs.push(songObj);
  332. }
  333. });
  334. }
  335. Router.route("/", {
  336. template: "home"
  337. });
  338. Router.route("/:type", {
  339. template: "room"
  340. });