app.js 15 KB

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