queueSongs.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. 'use strict';
  2. const db = require('../db');
  3. const utils = require('../utils');
  4. const notifications = require('../notifications');
  5. const async = require('async');
  6. const config = require('config');
  7. const request = require('request');
  8. notifications.subscribe("queue.newSong", function(songId) {
  9. io.to('admin.queue').emit("event:song.new", { songId });
  10. });
  11. notifications.subscribe("queue.removedSong", function(songId) {
  12. io.to('admin.queue').emit("event:song.removed", { songId });
  13. });
  14. notifications.subscribe("queue.updatedSong", function(songId) {
  15. //TODO Retrieve new Song object
  16. io.to('admin.queue').emit("event:song.updated", { songId });
  17. });
  18. module.exports = {
  19. index: (session, cb) => {
  20. //TODO Require admin/login
  21. db.models.queueSong.find({}, (err, songs) => {
  22. if (err) throw err;
  23. cb(songs);
  24. });
  25. },
  26. update: (session, id, updatedSong, cb) => {
  27. //TODO Require admin/login
  28. //TODO Check if id and updatedSong is valid
  29. db.models.queueSong.findOne({ id }, function(err, queueSong) {
  30. if (err) throw err;
  31. //List of properties that are allowed to be changed
  32. const updatableProperties = ["id", "title", "artists", "genres", "thumbnail", "explicit", "duration", "skipDuration"];
  33. //TODO Check if new id, if any, is already in use in queue or on rotation
  34. let updated = false;
  35. for (let prop in queueSong) {
  36. if (updatableProperties.indexOf(prop) !== -1 && updatedSong.hasOwnProperty("prop") && updatedSong[prop] !== queueSong[prop]) {
  37. queueSong[prop] = updatedSong[prop];
  38. updated = true;
  39. }
  40. }
  41. if (!updated) return cb({ status: 'failure', message: 'No properties changed.' });
  42. queueSong.save((err) => {
  43. if (err) return cb({ status: 'failure', message: 'Couldn\'t save to Database.' });
  44. return cb({ status: 'success', message: 'Successfully updated the queueSong object.' });
  45. });
  46. });
  47. },
  48. remove: (session, id, cb) => {
  49. //TODO Require admin/login
  50. db.models.queueSong.find({ id }).remove().exec();
  51. },
  52. add: (session, id, cb) => {
  53. //TODO Require login
  54. //TODO Check if id is valid
  55. //TODO Check if id is already in queue/rotation
  56. // if (!session.logged_in) return cb({ status: 'failure', message: 'You must be logged in to add a song' });
  57. let requestedAt = Date.now();
  58. async.waterfall([
  59. // Get YouTube data from id
  60. (next) => {
  61. console.log(111, id);
  62. const youtubeParams = [
  63. 'part=snippet,contentDetails,statistics,status',
  64. `id=${encodeURIComponent(id)}`,
  65. `key=${config.get('apis.youtube.key')}`
  66. ].join('&');
  67. request(`https://www.googleapis.com/youtube/v3/videos?${youtubeParams}`, (err, res, body) => {
  68. if (err) {
  69. console.error(err);
  70. return next('Failed to find song from YouTube');
  71. }
  72. body = JSON.parse(body);
  73. //TODO Clean up duration converter
  74. console.log(body);
  75. let dur = body.items[0].contentDetails.duration;
  76. dur = dur.replace("PT", "");
  77. let durInSec = 0;
  78. dur = dur.replace(/([\d]*)H/, function(v, v2) {
  79. v2 = Number(v2);
  80. durInSec = (v2 * 60 * 60)
  81. return "";
  82. });
  83. dur = dur.replace(/([\d]*)M/, function(v, v2) {
  84. v2 = Number(v2);
  85. durInSec = (v2 * 60)
  86. return "";
  87. });
  88. dur = dur.replace(/([\d]*)S/, function(v, v2) {
  89. v2 = Number(v2);
  90. durInSec += v2;
  91. return "";
  92. });
  93. let newSong = {
  94. _id: body.items[0].id,
  95. title: body.items[0].snippet.title,
  96. artists: [],
  97. genres: [],
  98. duration: durInSec,
  99. skipDuration: 0,
  100. thumbnail: 'default.png',
  101. explicit: false,
  102. requestedBy: 'temp',
  103. requestedAt: requestedAt
  104. };
  105. next(null, newSong);
  106. });
  107. },
  108. (newSong, next) => {
  109. console.log(222);
  110. const spotifyParams = [
  111. `q=${encodeURIComponent(newSong.title)}`,
  112. `type=track`
  113. ].join('&');
  114. request(`https://api.spotify.com/v1/search?${spotifyParams}`, (err, res, body) => {
  115. if (err) {
  116. console.error(err);
  117. return next('Failed to find song from Spotify');
  118. }
  119. body = JSON.parse(body);
  120. durationArtistLoop:
  121. for (let i in body) {
  122. let items = body[i].items;
  123. for (let j in items) {
  124. let item = items[j];
  125. let hasArtist = false;
  126. for (let k = 0; k < item.artists.length; k++) {
  127. let artist = item.artists[k];
  128. if (newSong.title.indexOf(artist.name) !== -1) {
  129. hasArtist = true;
  130. }
  131. }
  132. if (hasArtist && newSong.title.indexOf(item.name) !== -1) {
  133. newSong.duration = item.duration_ms / 1000;
  134. newSong.artists = item.map(artist => {
  135. return artist.name;
  136. });
  137. newSong.title = item.name;
  138. newSong.explicit = item.explicit;
  139. newSong.thumbnail = item.album.images[1].url;
  140. break durationArtistLoop;
  141. }
  142. }
  143. }
  144. next(null, newSong);
  145. });
  146. },
  147. (newSong, next) => {
  148. console.log(333);
  149. const song = new db.models.queueSong(newSong);
  150. song.save(err => {
  151. if (err) {
  152. console.error(err);
  153. return next('Failed to add song to database.');
  154. }
  155. //stations.getStation(station).playlist.push(newSong);
  156. next(null, newSong);
  157. });
  158. }
  159. ],
  160. (err, newSong) => {
  161. console.log(444, err);
  162. if (err) {
  163. return cb({ status: 'failure', message: err });
  164. }
  165. //TODO Emit to Redis
  166. notifications.emit("queue.newSong", newSong._id);
  167. return cb({ status: 'success', message: 'Successfully added that song to the queue.' });
  168. });
  169. }
  170. };