queueSongs.js 4.6 KB

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