mirror of
https://github.com/20kaushik02/spotify-manager.git
synced 2025-12-06 13:44:06 +00:00
some more fixes, URI stuff
This commit is contained in:
parent
f067320a7f
commit
b79170aafd
@ -13,9 +13,9 @@ const scopes = {
|
|||||||
ViewRecentlyPlayed: 'user-read-recently-played',
|
ViewRecentlyPlayed: 'user-read-recently-played',
|
||||||
ViewPlaybackPosition: 'user-read-playback-position',
|
ViewPlaybackPosition: 'user-read-playback-position',
|
||||||
ViewTop: 'user-top-read',
|
ViewTop: 'user-top-read',
|
||||||
|
ViewPrivatePlaylists: 'playlist-read-private',
|
||||||
IncludeCollaborative: 'playlist-read-collaborative',
|
IncludeCollaborative: 'playlist-read-collaborative',
|
||||||
ModifyPublicPlaylists: 'playlist-modify-public',
|
ModifyPublicPlaylists: 'playlist-modify-public',
|
||||||
ViewPrivatePlaylists: 'playlist-read-private',
|
|
||||||
ModifyPrivatePlaylists: 'playlist-modify-private',
|
ModifyPrivatePlaylists: 'playlist-modify-private',
|
||||||
ControlRemotePlayback: 'app-remote-control',
|
ControlRemotePlayback: 'app-remote-control',
|
||||||
ModifyLibrary: 'user-library-modify',
|
ModifyLibrary: 'user-library-modify',
|
||||||
|
|||||||
@ -48,8 +48,8 @@ const callback = async (req, res) => {
|
|||||||
logger.error('state mismatch');
|
logger.error('state mismatch');
|
||||||
return res.redirect(409, '/');
|
return res.redirect(409, '/');
|
||||||
} else if (error) {
|
} else if (error) {
|
||||||
logger.error('callback error', { authError: error });
|
logger.error('callback error', { error });
|
||||||
return res.status(401).send({ message: `Auth callback error` });
|
return res.status(401).send("Auth callback error");
|
||||||
} else {
|
} else {
|
||||||
// get auth tokens
|
// get auth tokens
|
||||||
res.clearCookie(stateKey);
|
res.clearCookie(stateKey);
|
||||||
@ -86,12 +86,10 @@ const callback = async (req, res) => {
|
|||||||
/** @type {typedefs.User} */
|
/** @type {typedefs.User} */
|
||||||
req.session.user = {
|
req.session.user = {
|
||||||
username: userResponse.data.display_name,
|
username: userResponse.data.display_name,
|
||||||
id: userResponse.data.id,
|
uri: userResponse.data.uri,
|
||||||
};
|
};
|
||||||
|
|
||||||
return res.status(200).send({
|
return res.sendStatus(200);
|
||||||
message: "Login successful",
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error('callback', { error });
|
logger.error('callback', { error });
|
||||||
@ -121,9 +119,7 @@ const refresh = async (req, res) => {
|
|||||||
req.session.cookie.maxAge = 7 * 24 * 60 * 60 * 1000 // 1 week
|
req.session.cookie.maxAge = 7 * 24 * 60 * 60 * 1000 // 1 week
|
||||||
|
|
||||||
logger.info(`Access token refreshed${(response.data.refresh_token !== null) ? ' and refresh token updated' : ''}.`);
|
logger.info(`Access token refreshed${(response.data.refresh_token !== null) ? ' and refresh token updated' : ''}.`);
|
||||||
return res.status(200).send({
|
return res.sendStatus(200);
|
||||||
message: "New access token obtained",
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
logger.error('refresh failed', { statusCode: response.status });
|
logger.error('refresh failed', { statusCode: response.status });
|
||||||
return res.status(response.status).send('Error: Refresh token flow failed.');
|
return res.status(response.status).send('Error: Refresh token flow failed.');
|
||||||
|
|||||||
@ -2,6 +2,7 @@ const logger = require("../utils/logger")(module);
|
|||||||
|
|
||||||
const typedefs = require("../typedefs");
|
const typedefs = require("../typedefs");
|
||||||
const { axiosInstance } = require('../utils/axios');
|
const { axiosInstance } = require('../utils/axios');
|
||||||
|
const { parseSpotifyUri, parseSpotifyLink } = require("../utils/spotifyUriTransformer");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve list of all of user's playlists
|
* Retrieve list of all of user's playlists
|
||||||
@ -10,11 +11,11 @@ const { axiosInstance } = require('../utils/axios');
|
|||||||
*/
|
*/
|
||||||
const getUserPlaylists = async (req, res) => {
|
const getUserPlaylists = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
let playlists = {};
|
let userPlaylists = {};
|
||||||
|
|
||||||
// get first 50
|
// get first 50
|
||||||
const response = await axiosInstance.get(
|
const response = await axiosInstance.get(
|
||||||
`/users/${req.session.user.id}/playlists`,
|
`/users/${parseSpotifyUri(req.session.user.uri).id}/playlists`,
|
||||||
{
|
{
|
||||||
params: {
|
params: {
|
||||||
offset: 0,
|
offset: 0,
|
||||||
@ -26,49 +27,53 @@ const getUserPlaylists = async (req, res) => {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
if (response.status === 401) {
|
if (response.status >= 400 && response.status < 500)
|
||||||
return res.status(401).send(response.data);
|
return res.status(response.status).send(response.data);
|
||||||
}
|
|
||||||
|
userPlaylists.total = response.data.total;
|
||||||
|
|
||||||
/** @type {typedefs.SimplifiedPlaylist[]} */
|
/** @type {typedefs.SimplifiedPlaylist[]} */
|
||||||
playlists.items = response.data.items.map((playlist) => {
|
userPlaylists.items = response.data.items.map((playlist) => {
|
||||||
return {
|
return {
|
||||||
|
uri: playlist.uri,
|
||||||
|
images: playlist.images,
|
||||||
name: playlist.name,
|
name: playlist.name,
|
||||||
id: playlist.id
|
total: playlist.tracks.total
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
playlists.total = response.data.total;
|
userPlaylists.next = response.data.next;
|
||||||
playlists.next = response.data.next;
|
|
||||||
|
|
||||||
// keep getting batches of 50 till exhausted
|
// keep getting batches of 50 till exhausted
|
||||||
while (playlists.next) {
|
while (userPlaylists.next) {
|
||||||
const nextResponse = await axiosInstance.get(
|
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: {
|
headers: {
|
||||||
...req.authHeader
|
...req.authHeader
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
if (response.status === 401)
|
if (response.status >= 400 && response.status < 500)
|
||||||
return res.status(401).send(response.data);
|
return res.status(response.status).send(response.data);
|
||||||
|
|
||||||
playlists.items.push(
|
userPlaylists.items.push(
|
||||||
...nextResponse.data.items.map((playlist) => {
|
...nextResponse.data.items.map((playlist) => {
|
||||||
return {
|
return {
|
||||||
|
uri: playlist.uri,
|
||||||
|
images: playlist.images,
|
||||||
name: playlist.name,
|
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) {
|
} catch (error) {
|
||||||
logger.error('getUserPlaylists', { error });
|
logger.error('getUserPlaylists', { error });
|
||||||
return res.sendStatus(500);
|
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.Req} req
|
||||||
* @param {typedefs.Res} res
|
* @param {typedefs.Res} res
|
||||||
*/
|
*/
|
||||||
@ -84,36 +89,57 @@ const getPlaylistDetails = async (req, res) => {
|
|||||||
try {
|
try {
|
||||||
/** @type {typedefs.Playlist} */
|
/** @type {typedefs.Playlist} */
|
||||||
let 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(
|
const response = await axiosInstance.get(
|
||||||
"/playlists/" + req.query.playlist_id,
|
`/playlists/${uri.id}/`,
|
||||||
{
|
{
|
||||||
|
params: {
|
||||||
|
fields: initialFields.join()
|
||||||
|
},
|
||||||
headers: { ...req.authHeader }
|
headers: { ...req.authHeader }
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
if (response.status === 401)
|
if (response.status >= 400 && response.status < 500)
|
||||||
return res.status(401).send(response.data);
|
return res.status(response.status).send(response.data);
|
||||||
|
|
||||||
// TODO: this whole section needs to be DRYer
|
// TODO: this whole section needs to be DRYer
|
||||||
// look into serializr
|
// look into serializr
|
||||||
playlist.uri = response.data.uri
|
playlist.name = response.data.name;
|
||||||
playlist.name = response.data.name
|
playlist.description = response.data.description;
|
||||||
playlist.description = response.data.description
|
playlist.collaborative = response.data.collaborative;
|
||||||
let { display_name, uri, id, ...rest } = response.data.owner
|
playlist.public = response.data.public;
|
||||||
playlist.owner = { display_name, uri, id }
|
playlist.images = { ...response.data.images };
|
||||||
playlist.followers = response.data.followers
|
playlist.owner = { ...response.data.owner };
|
||||||
|
playlist.snapshot_id = response.data.snapshot_id;
|
||||||
playlist.total = response.data.tracks.total;
|
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 {
|
return {
|
||||||
added_at: playlist_track.added_at,
|
is_local: playlist_item.is_local,
|
||||||
track: {
|
track: {
|
||||||
uri: playlist_track.track.uri,
|
name: playlist_item.track.name,
|
||||||
name: playlist_track.track.name,
|
type: playlist_item.track.type,
|
||||||
artists: playlist_track.track.artists.map((artist) => { return { name: artist.name } }),
|
uri: playlist_item.track.uri
|
||||||
album: { name: playlist_track.track.album.name },
|
|
||||||
is_local: playlist_track.track.is_local,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -122,26 +148,25 @@ const getPlaylistDetails = async (req, res) => {
|
|||||||
// keep getting batches of 50 till exhausted
|
// keep getting batches of 50 till exhausted
|
||||||
while (playlist.next) {
|
while (playlist.next) {
|
||||||
const nextResponse = await axiosInstance.get(
|
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: {
|
headers: {
|
||||||
...req.authHeader
|
...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(
|
playlist.tracks.push(
|
||||||
...nextResponse.data.items.map((playlist_track) => {
|
...nextResponse.data.items.map((playlist_item) => {
|
||||||
return {
|
return {
|
||||||
added_at: playlist_track.added_at,
|
is_local: playlist_item.is_local,
|
||||||
track: {
|
track: {
|
||||||
uri: playlist_track.track.uri,
|
name: playlist_item.track.name,
|
||||||
name: playlist_track.track.name,
|
type: playlist_item.track.type,
|
||||||
artists: playlist_track.track.artists.map((artist) => { return { name: artist.name } }),
|
uri: playlist_item.track.uri
|
||||||
album: { name: playlist_track.track.album.name },
|
|
||||||
is_local: playlist_track.track.is_local,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -150,6 +175,8 @@ const getPlaylistDetails = async (req, res) => {
|
|||||||
playlist.next = nextResponse.data.next;
|
playlist.next = nextResponse.data.next;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
delete playlist.next;
|
||||||
|
|
||||||
return res.status(200).send(playlist);
|
return res.status(200).send(playlist);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error('getPlaylistDetails', { error });
|
logger.error('getPlaylistDetails', { error });
|
||||||
|
|||||||
@ -10,7 +10,7 @@ const logger = require("../utils/logger")(module);
|
|||||||
const isAuthenticated = (req, res, next) => {
|
const isAuthenticated = (req, res, next) => {
|
||||||
if (req.session.accessToken) {
|
if (req.session.accessToken) {
|
||||||
req.authHeader = { 'Authorization': `Bearer ${req.session.accessToken}` };
|
req.authHeader = { 'Authorization': `Bearer ${req.session.accessToken}` };
|
||||||
next()
|
next();
|
||||||
} else {
|
} else {
|
||||||
const delSession = req.session.destroy((err) => {
|
const delSession = req.session.destroy((err) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
|||||||
@ -6,7 +6,7 @@ const { getPlaylistDetailsValidator } = require('../validators/playlists');
|
|||||||
const validator = require("../validators");
|
const validator = require("../validators");
|
||||||
|
|
||||||
router.get(
|
router.get(
|
||||||
"/user",
|
"/me",
|
||||||
isAuthenticated,
|
isAuthenticated,
|
||||||
validator.validate,
|
validator.validate,
|
||||||
getUserPlaylists
|
getUserPlaylists
|
||||||
|
|||||||
26
typedefs.js
26
typedefs.js
@ -8,13 +8,23 @@
|
|||||||
* @typedef {import('winston').Logger} Logger
|
* @typedef {import('winston').Logger} Logger
|
||||||
*
|
*
|
||||||
* @typedef {{
|
* @typedef {{
|
||||||
|
* type: string,
|
||||||
|
* is_local: boolean,
|
||||||
|
* id: string,
|
||||||
|
* artist?: string,
|
||||||
|
* album?: string,
|
||||||
|
* title?: string,
|
||||||
|
* duration?: number
|
||||||
|
* }} UriObject
|
||||||
|
*
|
||||||
|
* @typedef {{
|
||||||
* display_name: string,
|
* display_name: string,
|
||||||
* id: string
|
* uri: string
|
||||||
* }} User
|
* }} User
|
||||||
*
|
*
|
||||||
* @typedef {{
|
* @typedef {{
|
||||||
* name: string,
|
* name: string,
|
||||||
* id: string,
|
* uri: string,
|
||||||
* }} SimplifiedPlaylist
|
* }} SimplifiedPlaylist
|
||||||
*
|
*
|
||||||
* @typedef {{
|
* @typedef {{
|
||||||
@ -39,13 +49,19 @@
|
|||||||
* }} PlaylistTrack
|
* }} PlaylistTrack
|
||||||
*
|
*
|
||||||
* @typedef {{
|
* @typedef {{
|
||||||
|
* url: string,
|
||||||
|
* height: number,
|
||||||
|
* width: number
|
||||||
|
* }} ImageObject
|
||||||
|
*
|
||||||
|
* @typedef {{
|
||||||
* uri: string,
|
* uri: string,
|
||||||
* name: string,
|
* name: string,
|
||||||
* description: string,
|
* description: string,
|
||||||
|
* collaborative: boolean,
|
||||||
|
* public: boolean,
|
||||||
* owner: User,
|
* owner: User,
|
||||||
* followers: {
|
* images: ImageObject[],
|
||||||
* total: number
|
|
||||||
* },
|
|
||||||
* tracks: PlaylistTrack[],
|
* tracks: PlaylistTrack[],
|
||||||
* }} Playlist
|
* }} Playlist
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -32,7 +32,12 @@ axiosInstance.interceptors.request.use(request => {
|
|||||||
axiosInstance.interceptors.response.use(
|
axiosInstance.interceptors.response.use(
|
||||||
(response) => response,
|
(response) => response,
|
||||||
(error) => {
|
(error) => {
|
||||||
if (error.response) {
|
if (error.response && error.response.status === 429) {
|
||||||
|
// Rate limiting
|
||||||
|
logger.warn("Spotify API: Too many requests");
|
||||||
|
return error.response;
|
||||||
|
}
|
||||||
|
else if (error.response) {
|
||||||
// Server has responded
|
// Server has responded
|
||||||
logger.error(
|
logger.error(
|
||||||
"Spotify API: Error", {
|
"Spotify API: Error", {
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
* Returns a single string of the values of all keys in the given JSON object, even nested ones.
|
* Returns a single string of the values of all keys in the given JSON object, even nested ones.
|
||||||
*
|
*
|
||||||
* @param {*} obj
|
* @param {*} obj
|
||||||
* @returns
|
* @returns {string}
|
||||||
*/
|
*/
|
||||||
const getNestedValuesString = (obj) => {
|
const getNestedValuesString = (obj) => {
|
||||||
let values = [];
|
let values = [];
|
||||||
|
|||||||
@ -10,12 +10,14 @@ const getLabel = (callingModule) => {
|
|||||||
return path.join(parts[parts.length - 2], parts.pop());
|
return path.join(parts[parts.length - 2], parts.pop());
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const allowedErrorKeys = ["name", "message", "stack"];
|
||||||
|
|
||||||
const logMetaReplacer = (key, value) => {
|
const logMetaReplacer = (key, value) => {
|
||||||
if (key === "error") {
|
if (key === "error") {
|
||||||
return {
|
return {
|
||||||
name: value.name,
|
name: value.name,
|
||||||
message: value.message,
|
message: value.message,
|
||||||
stack: value.stack
|
stack: value.stack,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
@ -28,11 +30,10 @@ const metaFormat = (meta) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const logFormat = printf(({ level, message, label, timestamp, ...meta }) => {
|
const logFormat = printf(({ level, message, label, timestamp, ...meta }) => {
|
||||||
if (meta.error) {
|
if (meta.error) { // if the error was passed
|
||||||
for (const key in meta.error) {
|
for (const key in meta.error) {
|
||||||
const allowedErrorKeys = ["name", "message", "stack"]
|
if (!allowedErrorKeys.includes(key)) {
|
||||||
if (typeof key !== "symbol" && !allowedErrorKeys.includes(key)) {
|
delete meta.error[key];
|
||||||
delete meta.error[key]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
108
utils/spotifyUriTransformer.js
Normal file
108
utils/spotifyUriTransformer.js
Normal file
@ -0,0 +1,108 @@
|
|||||||
|
const typedefs = require("../typedefs");
|
||||||
|
|
||||||
|
/** @type {RegExp} */
|
||||||
|
const base62Pattern = /^[A-Za-z0-9]+$/;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns type and ID from a Spotify URI
|
||||||
|
* @see {@link https://developer.spotify.com/documentation/web-api/concepts/spotify-uris-ids|Spotify URIs and IDs}
|
||||||
|
* @param {string} uri Spotify URI - can be of an album, track, playlist, user, episode, etc.
|
||||||
|
* @returns {typedefs.UriObject}
|
||||||
|
* @throws {TypeError} If the input is not a valid Spotify URI
|
||||||
|
*/
|
||||||
|
const parseSpotifyUri = (uri) => {
|
||||||
|
const parts = uri.split(":");
|
||||||
|
|
||||||
|
if (parts[0] !== "spotify") {
|
||||||
|
throw new TypeError(`${uri} is not a valid Spotify URI`);
|
||||||
|
}
|
||||||
|
|
||||||
|
let type = parts[1];
|
||||||
|
|
||||||
|
if (type === "local") {
|
||||||
|
// Local file format: spotify:local:<artist>:<album>:<title>:<duration>
|
||||||
|
let idParts = parts.slice(2);
|
||||||
|
if (idParts.length < 4) {
|
||||||
|
throw new TypeError(`${uri} is not a valid local file URI`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// URL decode artist, album, and title
|
||||||
|
const artist = decodeURIComponent(idParts[0] || '');
|
||||||
|
const album = decodeURIComponent(idParts[1] || '');
|
||||||
|
const title = decodeURIComponent(idParts[2]);
|
||||||
|
const duration = parseInt(idParts[3], 10);
|
||||||
|
|
||||||
|
if (isNaN(duration)) {
|
||||||
|
throw new TypeError(`${uri} has an invalid duration`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return { type: "track", is_local: true, artist, album, title, duration };
|
||||||
|
} else {
|
||||||
|
// Not a local file
|
||||||
|
if (parts.length !== 3) {
|
||||||
|
throw new TypeError(`${uri} is not a valid Spotify URI`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const id = parts[2];
|
||||||
|
|
||||||
|
if (!base62Pattern.test(id)) {
|
||||||
|
throw new TypeError(`${uri} has an invalid ID`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return { type, is_local: false, id };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns type and ID from a Spotify link
|
||||||
|
* @see {@link https://developer.spotify.com/documentation/web-api/concepts/spotify-uris-ids|Spotify URIs and IDs}
|
||||||
|
* @param {string} link Spotify URL - can be of an album, track, playlist, user, episode, etc.
|
||||||
|
* @returns {typedefs.UriObject}
|
||||||
|
* @throws {TypeError} If the input is not a valid Spotify link
|
||||||
|
*/
|
||||||
|
const parseSpotifyLink = (link) => {
|
||||||
|
const localPattern = /^https:\/\/open\.spotify\.com\/local\/([^\/]*)\/([^\/]*)\/([^\/]+)\/(\d+)$/;
|
||||||
|
const standardPattern = /^https:\/\/open\.spotify\.com\/([^\/]+)\/([^\/?]+)/;
|
||||||
|
|
||||||
|
if (localPattern.test(link)) {
|
||||||
|
// Local file format: https://open.spotify.com/local/artist/album/title/duration
|
||||||
|
const matches = link.match(localPattern);
|
||||||
|
if (!matches) {
|
||||||
|
throw new TypeError(`${link} is not a valid Spotify local file link`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// URL decode artist, album, and title
|
||||||
|
const artist = decodeURIComponent(matches[1] || '');
|
||||||
|
const album = decodeURIComponent(matches[2] || '');
|
||||||
|
const title = decodeURIComponent(matches[3]);
|
||||||
|
const duration = parseInt(matches[4], 10);
|
||||||
|
|
||||||
|
if (isNaN(duration)) {
|
||||||
|
throw new TypeError(`${link} has an invalid duration`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return { type: "track", is_local: true, artist, album, title, duration };
|
||||||
|
} else if (standardPattern.test(link)) {
|
||||||
|
// Not a local file
|
||||||
|
const matches = link.match(standardPattern);
|
||||||
|
if (!matches || matches.length < 3) {
|
||||||
|
throw new TypeError(`${link} is not a valid Spotify link`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const type = matches[1];
|
||||||
|
const id = matches[2];
|
||||||
|
|
||||||
|
if (!base62Pattern.test(id)) {
|
||||||
|
throw new TypeError(`${link} has an invalid ID`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return { type, is_local: false, id };
|
||||||
|
} else {
|
||||||
|
throw new TypeError(`${link} is not a valid Spotify link`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
parseSpotifyUri,
|
||||||
|
parseSpotifyLink
|
||||||
|
}
|
||||||
@ -8,11 +8,11 @@ const typedefs = require("../typedefs");
|
|||||||
* @param {typedefs.Next} next
|
* @param {typedefs.Next} next
|
||||||
*/
|
*/
|
||||||
const getPlaylistDetailsValidator = async (req, res, next) => {
|
const getPlaylistDetailsValidator = async (req, res, next) => {
|
||||||
await query("playlist_id")
|
await query("playlist_link")
|
||||||
.notEmpty()
|
.notEmpty()
|
||||||
.withMessage("playlist_id not defined in query")
|
.withMessage("playlist_link not defined in query")
|
||||||
.isAlphanumeric()
|
.isURL()
|
||||||
.withMessage("playlist_id must be alphanumeric (base-62)")
|
.withMessage("playlist_link must be a valid link")
|
||||||
.run(req);
|
.run(req);
|
||||||
next();
|
next();
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user