Source: lib/playlist.js

  1. var Queue = require('./queue')
  2. var SpotifyRequestHandler = require('./spotify')
  3. var Track = require('./track')
  4. /**
  5. * Create playlist entry.
  6. * @constructor
  7. * @param {SpotifyRequestHandler} spotify - Spotify request handler.
  8. * @param {string} entry - The playlist to search for.
  9. * @param {string} [id] - The Spotify ID, if known.
  10. * @param {string} [limit] - The number of tracks to fetch.
  11. */
  12. function Playlist (spotify, entry, id, owner, limit) {
  13. /**
  14. * Entry string.
  15. */
  16. this.entry = ''
  17. /**
  18. * The ID of the playlist.
  19. */
  20. this.id = ''
  21. /**
  22. * Playlist tracks.
  23. */
  24. this.items = []
  25. /**
  26. * Number of tracks to fetch.
  27. */
  28. this.limit = null
  29. /**
  30. * Spotify request handler.
  31. */
  32. this.spotify = null
  33. /**
  34. * The user who owns the playlist.
  35. */
  36. this.owner = {}
  37. /**
  38. * Spotify URI
  39. * (a string on the form `spotify:user:xxxxxxxx:playlist:xxxxxxxxxxxxxxxxxxxxxx`).
  40. */
  41. this.uri = ''
  42. this.entry = entry.trim()
  43. this.id = id || this.id
  44. this.limit = limit
  45. this.owner.id = owner || this.owner.id
  46. this.spotify = spotify || new SpotifyRequestHandler()
  47. this.uri = (this.owner.id && this.id) ? ('spotify:user:' + this.owner.id + ':playlist:' + this.id) : this.uri
  48. }
  49. /**
  50. * Clone a JSON response.
  51. * @param {Object} response - The response.
  52. */
  53. Playlist.prototype.clone = function (response) {
  54. for (var prop in response) {
  55. if (response.hasOwnProperty(prop) &&
  56. prop !== 'limit') {
  57. this[prop] = response[prop] || this[prop]
  58. }
  59. }
  60. }
  61. /**
  62. * Create a queue of tracks.
  63. * @param {JSON} response - A JSON response object.
  64. * @return {Promise | Queue} A queue of tracks.
  65. */
  66. Playlist.prototype.createQueue = function () {
  67. var self = this
  68. var tracks = this.items.map(function (item) {
  69. var track = new Track(self.spotify, self.entry)
  70. track.clone(item.track)
  71. return track
  72. })
  73. var queue = new Queue(tracks)
  74. if (self.limit) {
  75. queue = queue.slice(0, self.limit)
  76. }
  77. return queue
  78. }
  79. /**
  80. * Dispatch entry.
  81. * @return {Promise | Queue} A queue of tracks.
  82. */
  83. Playlist.prototype.dispatch = function () {
  84. var self = this
  85. return this.searchForPlaylist().then(function () {
  86. return self.fetchPlaylist()
  87. }).then(function () {
  88. return self.createQueue()
  89. })
  90. }
  91. /**
  92. * Fetch playlist tracks.
  93. * @return {Promise | JSON} A JSON response.
  94. */
  95. Playlist.prototype.fetchPlaylist = function (id, owner) {
  96. id = id || this.id
  97. owner = owner || (this.owner && this.owner.id)
  98. var self = this
  99. return this.spotify.getPlaylist(id, owner).then(function (response) {
  100. self.clone(response)
  101. return self
  102. })
  103. }
  104. /**
  105. * Search for playlist.
  106. * @return {Promise | JSON} A JSON response, or `null` if not found.
  107. */
  108. Playlist.prototype.searchForPlaylist = function () {
  109. var self = this
  110. if (this.id && this.owner && this.owner.id) {
  111. return Promise.resolve(this)
  112. } else {
  113. return this.spotify.searchForPlaylist(this.entry).then(function (response) {
  114. if (response &&
  115. response.playlists &&
  116. response.playlists.items &&
  117. response.playlists.items[0]) {
  118. response = response.playlists.items[0]
  119. self.clone(response)
  120. return Promise.resolve(self)
  121. } else {
  122. return Promise.reject(response)
  123. }
  124. }).catch(function () {
  125. // console.log('COULD NOT FIND ' + self.entry)
  126. return Promise.reject(null)
  127. })
  128. }
  129. }
  130. module.exports = Playlist