mirror of
https://github.com/20kaushik02/spotify-manager.git
synced 2026-01-25 14:14:06 +00:00
added playlist retrieval route, redis store
This commit is contained in:
84
controllers/playlists.js
Normal file
84
controllers/playlists.js
Normal file
@@ -0,0 +1,84 @@
|
||||
require('dotenv').config();
|
||||
const logger = require("../utils/logger")(module);
|
||||
|
||||
const typedefs = require("../typedefs");
|
||||
const { axiosInstance } = require('../axios');
|
||||
|
||||
/**
|
||||
* Retrieve list of all of user's playlists
|
||||
* @param {typedefs.Req} req
|
||||
* @param {typedefs.Res} res
|
||||
*/
|
||||
const getUserPlaylists = async (req, res) => {
|
||||
try {
|
||||
// get first 50
|
||||
const response = await axiosInstance.get(
|
||||
"/me/playlists",
|
||||
{
|
||||
params: {
|
||||
offset: 0,
|
||||
limit: 50,
|
||||
},
|
||||
headers: {
|
||||
...req.authHeader
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
let playlists = {};
|
||||
|
||||
playlists.items = response.data.items.map((playlist) => {
|
||||
return {
|
||||
name: playlist.name,
|
||||
description: playlist.description,
|
||||
owner: playlist.owner.display_name,
|
||||
images: playlist.images.map((image) => image.url),
|
||||
link: playlist.external_urls.spotify,
|
||||
collaborative: playlist.collaborative,
|
||||
public: playlist.public,
|
||||
id: playlist.id,
|
||||
}
|
||||
});
|
||||
|
||||
playlists.total = response.data.total;
|
||||
playlists.next = response.data.next;
|
||||
|
||||
// keep getting batches of 50 more till exhausted
|
||||
while (playlists.next) {
|
||||
const nextResponse = await axiosInstance.get(
|
||||
playlists.next, // absolute URL which has params
|
||||
{
|
||||
headers: {
|
||||
...req.authHeader
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
playlists.items.push(
|
||||
...nextResponse.data.items.map((playlist) => {
|
||||
return {
|
||||
name: playlist.name,
|
||||
description: playlist.description,
|
||||
owner: playlist.owner.display_name,
|
||||
images: playlist.images.map((image) => image.url),
|
||||
link: playlist.external_urls.spotify,
|
||||
collaborative: playlist.collaborative,
|
||||
public: playlist.public,
|
||||
id: playlist.id,
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
playlists.next = nextResponse.data.next;
|
||||
}
|
||||
|
||||
return res.status(200).send(playlists);
|
||||
} catch (error) {
|
||||
logger.error('Error', { error });
|
||||
return res.status(500).send({ message: "Server Error. Try again." });
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getUserPlaylists
|
||||
};
|
||||
Reference in New Issue
Block a user