queueSongs.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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", "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. requestedBy: '',
  87. requestedAt: requestedAt
  88. };
  89. next(null, newSong);
  90. });
  91. },
  92. (newSong, next) => {
  93. const spotifyParams = [
  94. `q=${encodeURIComponent(newSong.title)}`,
  95. `type=track`
  96. ].join('&');
  97. request(`https://api.spotify.com/v1/search?${spotifyParams}`, (err, res, body) => {
  98. if (err) {
  99. console.error(err);
  100. return next('Failed to find song from Spotify');
  101. }
  102. body = JSON.parse(body);
  103. durationArtistLoop:
  104. for (let i in body) {
  105. let items = body[i].items;
  106. for (let j in items) {
  107. let item = items[j];
  108. let hasArtist = false;
  109. for (let k = 0; k < item.artists.length; k++) {
  110. let artist = item.artists[k];
  111. if (newSong.title.indexOf(artist.name) !== -1) {
  112. hasArtist = true;
  113. }
  114. }
  115. if (hasArtist && newSong.title.indexOf(item.name) !== -1) {
  116. newSong.duration = item.duration_ms / 1000;
  117. newSong.artists = item.map(artist => {
  118. return artist.name;
  119. });
  120. newSong.title = item.name;
  121. break durationArtistLoop;
  122. }
  123. }
  124. }
  125. next(null, newSong);
  126. });
  127. },
  128. (newSong, next) => {
  129. const song = new db.models.queueSong(newSong);
  130. song.save(err => {
  131. if (err) {
  132. console.error(err);
  133. return next('Failed to add song to database.');
  134. }
  135. //stations.getStation(station).playlist.push(newSong);
  136. next(null);
  137. });
  138. }
  139. ],
  140. (err) => {
  141. if (err) {
  142. return cb({ status: 'failure', message: err });
  143. }
  144. return cb({ status: 'success', message: 'Successfully added that song to the queue.' });
  145. });
  146. }
  147. };