mirror of
https://github.com/20kaushik02/spotify-manager.git
synced 2026-01-25 14:14:06 +00:00
some more fixes, URI stuff
This commit is contained in:
@@ -2,6 +2,7 @@ const logger = require("../utils/logger")(module);
|
||||
|
||||
const typedefs = require("../typedefs");
|
||||
const { axiosInstance } = require('../utils/axios');
|
||||
const { parseSpotifyUri, parseSpotifyLink } = require("../utils/spotifyUriTransformer");
|
||||
|
||||
/**
|
||||
* Retrieve list of all of user's playlists
|
||||
@@ -10,11 +11,11 @@ const { axiosInstance } = require('../utils/axios');
|
||||
*/
|
||||
const getUserPlaylists = async (req, res) => {
|
||||
try {
|
||||
let playlists = {};
|
||||
let userPlaylists = {};
|
||||
|
||||
// get first 50
|
||||
const response = await axiosInstance.get(
|
||||
`/users/${req.session.user.id}/playlists`,
|
||||
`/users/${parseSpotifyUri(req.session.user.uri).id}/playlists`,
|
||||
{
|
||||
params: {
|
||||
offset: 0,
|
||||
@@ -26,49 +27,53 @@ const getUserPlaylists = async (req, res) => {
|
||||
}
|
||||
);
|
||||
|
||||
if (response.status === 401) {
|
||||
return res.status(401).send(response.data);
|
||||
}
|
||||
if (response.status >= 400 && response.status < 500)
|
||||
return res.status(response.status).send(response.data);
|
||||
|
||||
userPlaylists.total = response.data.total;
|
||||
|
||||
/** @type {typedefs.SimplifiedPlaylist[]} */
|
||||
playlists.items = response.data.items.map((playlist) => {
|
||||
userPlaylists.items = response.data.items.map((playlist) => {
|
||||
return {
|
||||
uri: playlist.uri,
|
||||
images: playlist.images,
|
||||
name: playlist.name,
|
||||
id: playlist.id
|
||||
total: playlist.tracks.total
|
||||
}
|
||||
});
|
||||
|
||||
playlists.total = response.data.total;
|
||||
playlists.next = response.data.next;
|
||||
userPlaylists.next = response.data.next;
|
||||
|
||||
// keep getting batches of 50 till exhausted
|
||||
while (playlists.next) {
|
||||
while (userPlaylists.next) {
|
||||
const nextResponse = await axiosInstance.get(
|
||||
playlists.next, // absolute URL from previous response which has offset and limit params
|
||||
userPlaylists.next, // absolute URL from previous response which has params
|
||||
{
|
||||
headers: {
|
||||
...req.authHeader
|
||||
}
|
||||
}
|
||||
);
|
||||
if (response.status === 401)
|
||||
return res.status(401).send(response.data);
|
||||
if (response.status >= 400 && response.status < 500)
|
||||
return res.status(response.status).send(response.data);
|
||||
|
||||
playlists.items.push(
|
||||
userPlaylists.items.push(
|
||||
...nextResponse.data.items.map((playlist) => {
|
||||
return {
|
||||
uri: playlist.uri,
|
||||
images: playlist.images,
|
||||
name: playlist.name,
|
||||
id: playlist.id
|
||||
total: playlist.tracks.total
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
playlists.next = nextResponse.data.next;
|
||||
userPlaylists.next = nextResponse.data.next;
|
||||
}
|
||||
|
||||
delete playlists.next;
|
||||
delete userPlaylists.next;
|
||||
|
||||
return res.status(200).send(playlists);
|
||||
return res.status(200).send(userPlaylists);
|
||||
} catch (error) {
|
||||
logger.error('getUserPlaylists', { error });
|
||||
return res.sendStatus(500);
|
||||
@@ -76,7 +81,7 @@ const getUserPlaylists = async (req, res) => {
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve single playlist
|
||||
* Retrieve an entire playlist
|
||||
* @param {typedefs.Req} req
|
||||
* @param {typedefs.Res} res
|
||||
*/
|
||||
@@ -84,36 +89,57 @@ const getPlaylistDetails = async (req, res) => {
|
||||
try {
|
||||
/** @type {typedefs.Playlist} */
|
||||
let playlist = {};
|
||||
/** @type {typedefs.UriObject} */
|
||||
let uri;
|
||||
let initialFields = ["collaborative", "description", "images", "name", "owner(uri,display_name)", "public",
|
||||
"snapshot_id", "tracks(next,total,items(is_local,track(name,uri)))"];
|
||||
let mainFields = ["next,items(is_local,track(name,uri))"];
|
||||
|
||||
try {
|
||||
uri = parseSpotifyLink(req.query.playlist_link)
|
||||
if (uri.type !== "playlist") {
|
||||
return res.status(400).send("Not a playlist link");
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error("parseSpotifyLink", { error });
|
||||
return res.sendStatus(400);
|
||||
}
|
||||
|
||||
const response = await axiosInstance.get(
|
||||
"/playlists/" + req.query.playlist_id,
|
||||
`/playlists/${uri.id}/`,
|
||||
{
|
||||
params: {
|
||||
fields: initialFields.join()
|
||||
},
|
||||
headers: { ...req.authHeader }
|
||||
}
|
||||
);
|
||||
if (response.status === 401)
|
||||
return res.status(401).send(response.data);
|
||||
if (response.status >= 400 && response.status < 500)
|
||||
return res.status(response.status).send(response.data);
|
||||
|
||||
// TODO: this whole section needs to be DRYer
|
||||
// look into serializr
|
||||
playlist.uri = response.data.uri
|
||||
playlist.name = response.data.name
|
||||
playlist.description = response.data.description
|
||||
let { display_name, uri, id, ...rest } = response.data.owner
|
||||
playlist.owner = { display_name, uri, id }
|
||||
playlist.followers = response.data.followers
|
||||
playlist.name = response.data.name;
|
||||
playlist.description = response.data.description;
|
||||
playlist.collaborative = response.data.collaborative;
|
||||
playlist.public = response.data.public;
|
||||
playlist.images = { ...response.data.images };
|
||||
playlist.owner = { ...response.data.owner };
|
||||
playlist.snapshot_id = response.data.snapshot_id;
|
||||
playlist.total = response.data.tracks.total;
|
||||
playlist.next = response.data.tracks.next;
|
||||
|
||||
playlist.tracks = response.data.tracks.items.map((playlist_track) => {
|
||||
// previous fields get carried over to the next URL, but most of these fields are not present in the new endpoint
|
||||
// API shouldn't be returning such URLs, the problem's in the API ig...
|
||||
playlist.next = new URL(response.data.tracks.next);
|
||||
playlist.next.searchParams.set("fields", mainFields.join());
|
||||
playlist.next = playlist.next.href;
|
||||
playlist.tracks = response.data.tracks.items.map((playlist_item) => {
|
||||
return {
|
||||
added_at: playlist_track.added_at,
|
||||
is_local: playlist_item.is_local,
|
||||
track: {
|
||||
uri: playlist_track.track.uri,
|
||||
name: playlist_track.track.name,
|
||||
artists: playlist_track.track.artists.map((artist) => { return { name: artist.name } }),
|
||||
album: { name: playlist_track.track.album.name },
|
||||
is_local: playlist_track.track.is_local,
|
||||
name: playlist_item.track.name,
|
||||
type: playlist_item.track.type,
|
||||
uri: playlist_item.track.uri
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -122,26 +148,25 @@ const getPlaylistDetails = async (req, res) => {
|
||||
// keep getting batches of 50 till exhausted
|
||||
while (playlist.next) {
|
||||
const nextResponse = await axiosInstance.get(
|
||||
playlist.next, // absolute URL from previous response which has offset and limit params
|
||||
playlist.next, // absolute URL from previous response which has params
|
||||
{
|
||||
headers: {
|
||||
...req.authHeader
|
||||
}
|
||||
}
|
||||
);
|
||||
if (nextResponse.status === 401)
|
||||
return res.status(401).send(nextResponse.data);
|
||||
|
||||
if (nextResponse.status >= 400 && nextResponse.status < 500)
|
||||
return res.status(nextResponse.status).send(nextResponse.data);
|
||||
|
||||
playlist.tracks.push(
|
||||
...nextResponse.data.items.map((playlist_track) => {
|
||||
...nextResponse.data.items.map((playlist_item) => {
|
||||
return {
|
||||
added_at: playlist_track.added_at,
|
||||
is_local: playlist_item.is_local,
|
||||
track: {
|
||||
uri: playlist_track.track.uri,
|
||||
name: playlist_track.track.name,
|
||||
artists: playlist_track.track.artists.map((artist) => { return { name: artist.name } }),
|
||||
album: { name: playlist_track.track.album.name },
|
||||
is_local: playlist_track.track.is_local,
|
||||
name: playlist_item.track.name,
|
||||
type: playlist_item.track.type,
|
||||
uri: playlist_item.track.uri
|
||||
}
|
||||
}
|
||||
})
|
||||
@@ -150,6 +175,8 @@ const getPlaylistDetails = async (req, res) => {
|
||||
playlist.next = nextResponse.data.next;
|
||||
}
|
||||
|
||||
delete playlist.next;
|
||||
|
||||
return res.status(200).send(playlist);
|
||||
} catch (error) {
|
||||
logger.error('getPlaylistDetails', { error });
|
||||
|
||||
Reference in New Issue
Block a user