app.js 15 KB

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