queueSongs.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. 'use strict';
  2. const db = require('../db');
  3. const utils = require('../utils');
  4. const notifications = require('../notifications');
  5. const cache = require('../cache');
  6. const async = require('async');
  7. const config = require('config');
  8. const request = require('request');
  9. const hooks = require('./hooks');
  10. notifications.subscribe('queue.newSong', songId => {
  11. io.to('admin.queue').emit('event:song.new', { songId });
  12. });
  13. notifications.subscribe('queue.removedSong', songId => {
  14. io.to('admin.queue').emit('event:song.removed', { songId });
  15. });
  16. notifications.subscribe('queue.updatedSong', songId => {
  17. //TODO Retrieve new Song object
  18. io.to('admin.queue').emit('event:song.updated', { songId });
  19. });
  20. module.exports = {
  21. index: hooks.adminRequired((session, cb) => {
  22. db.models.queueSong.find({}, (err, songs) => {
  23. if (err) throw err;
  24. cb(songs);
  25. });
  26. }),
  27. update: hooks.adminRequired((session, _id, updatedSong, cb) => {
  28. //TODO Check if id and updatedSong is valid
  29. db.models.queueSong.findOne({ _id }, (err, currentSong) => {
  30. if (err) throw err;
  31. // TODO Check if new id, if any, is already in use in queue or on rotation
  32. let updated = false;
  33. for (let prop in updatedSong) if (updatedSong[prop] !== currentSong[prop]) currentSong[prop] = updatedSong[prop]; updated = true;
  34. if (!updated) return cb({ status: 'error', message: 'No properties changed' });
  35. else {
  36. currentSong.save(err => {
  37. if (err) throw err;
  38. return cb({ status: 'success', message: 'Successfully updated the queued song' });
  39. });
  40. }
  41. });
  42. }),
  43. remove: hooks.adminRequired((session, _id, cb) => {
  44. // TODO Require admin/login
  45. db.models.queueSong.remove({ _id });
  46. return cb({ status: 'success', message: 'Song was removed successfully' });
  47. }),
  48. add: hooks.loginRequired((session, id, cb, userId) => {
  49. //TODO Check if id is valid
  50. //TODO Check if id is already in queue/rotation
  51. let requestedAt = Date.now();
  52. async.waterfall([
  53. // Get YouTube data from id
  54. (next) => {
  55. utils.getSongFromYouTube(id, (song) => {
  56. song.artists = [];
  57. song.genres = [];
  58. song.skipDuration = 0;
  59. song.thumbnail = 'empty';
  60. song.explicit = false;
  61. song.requestedBy = userId;
  62. song.requestedAt = requestedAt;
  63. next(null, song);
  64. });
  65. },
  66. (newSong, next) => {
  67. const spotifyParams = [
  68. `q=${encodeURIComponent(newSong.title)}`,
  69. `type=track`
  70. ].join('&');
  71. request(`https://api.spotify.com/v1/search?${spotifyParams}`, (err, res, body) => {
  72. if (err) {
  73. console.error(err);
  74. return next('Failed to find song from Spotify');
  75. }
  76. body = JSON.parse(body);
  77. durationArtistLoop:
  78. for (let i in body) {
  79. let items = body[i].items;
  80. for (let j in items) {
  81. let item = items[j];
  82. let hasArtist = false;
  83. for (let k = 0; k < item.artists.length; k++) {
  84. let artist = item.artists[k];
  85. if (newSong.title.indexOf(artist.name) !== -1) {
  86. hasArtist = true;
  87. }
  88. }
  89. if (hasArtist && newSong.title.indexOf(item.name) !== -1) {
  90. newSong.duration = item.duration_ms / 1000;
  91. newSong.artists = item.artists.map(artist => {
  92. return artist.name;
  93. });
  94. newSong.title = item.name;
  95. newSong.explicit = item.explicit;
  96. newSong.thumbnail = item.album.images[1].url;
  97. break durationArtistLoop;
  98. }
  99. }
  100. }
  101. next(null, newSong);
  102. });
  103. },
  104. (newSong, next) => {
  105. const song = new db.models.queueSong(newSong);
  106. // check if song already exists
  107. song.save(err => {
  108. if (err) {
  109. console.error(err);
  110. return next('Failed to add song to database');
  111. }
  112. //stations.getStation(station).playlist.push(newSong);
  113. next(null, newSong);
  114. });
  115. }
  116. ],
  117. (err, newSong) => {
  118. if (err) return cb({ status: 'error', message: err });
  119. cache.pub('queue.newSong', newSong._id);
  120. return cb({ status: 'success', message: 'Successfully added that song to the queue' });
  121. });
  122. })
  123. };