mirror of
https://github.com/20kaushik02/spotify-manager.git
synced 2026-01-25 14:14:06 +00:00
editorconfig
This commit is contained in:
@@ -11,46 +11,46 @@ const base62Pattern = /^[A-Za-z0-9]+$/;
|
||||
* @throws {TypeError} If the input is not a valid Spotify URI
|
||||
*/
|
||||
const parseSpotifyURI = (uri) => {
|
||||
const parts = uri.split(":");
|
||||
const parts = uri.split(":");
|
||||
|
||||
if (parts[0] !== "spotify") {
|
||||
throw new TypeError(`${uri} is not a valid Spotify URI`);
|
||||
}
|
||||
if (parts[0] !== "spotify") {
|
||||
throw new TypeError(`${uri} is not a valid Spotify URI`);
|
||||
}
|
||||
|
||||
let type = parts[1];
|
||||
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`);
|
||||
}
|
||||
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);
|
||||
// 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`);
|
||||
}
|
||||
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`);
|
||||
}
|
||||
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];
|
||||
const id = parts[2];
|
||||
|
||||
if (!base62Pattern.test(id)) {
|
||||
throw new TypeError(`${uri} has an invalid ID`);
|
||||
}
|
||||
if (!base62Pattern.test(id)) {
|
||||
throw new TypeError(`${uri} has an invalid ID`);
|
||||
}
|
||||
|
||||
return { type, is_local: false, id };
|
||||
}
|
||||
return { type, is_local: false, id };
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -60,45 +60,45 @@ const parseSpotifyURI = (uri) => {
|
||||
* @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\/([^\/]+)\/([^\/?]+)/;
|
||||
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`);
|
||||
}
|
||||
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);
|
||||
// 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`);
|
||||
}
|
||||
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`);
|
||||
}
|
||||
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];
|
||||
const type = matches[1];
|
||||
const id = matches[2];
|
||||
|
||||
if (!base62Pattern.test(id)) {
|
||||
throw new TypeError(`${link} has an invalid ID`);
|
||||
}
|
||||
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`);
|
||||
}
|
||||
return { type, is_local: false, id };
|
||||
} else {
|
||||
throw new TypeError(`${link} is not a valid Spotify link`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -107,14 +107,14 @@ const parseSpotifyLink = (link) => {
|
||||
* @returns {string}
|
||||
*/
|
||||
const buildSpotifyURI = (uriObj) => {
|
||||
if (uriObj.is_local) {
|
||||
const artist = encodeURIComponent(uriObj.artist ?? "");
|
||||
const album = encodeURIComponent(uriObj.album ?? "");
|
||||
const title = encodeURIComponent(uriObj.title ?? "");
|
||||
const duration = uriObj.duration ? uriObj.duration.toString() : "";
|
||||
return `spotify:local:${artist}:${album}:${title}:${duration}`;
|
||||
}
|
||||
return `spotify:${uriObj.type}:${uriObj.id}`;
|
||||
if (uriObj.is_local) {
|
||||
const artist = encodeURIComponent(uriObj.artist ?? "");
|
||||
const album = encodeURIComponent(uriObj.album ?? "");
|
||||
const title = encodeURIComponent(uriObj.title ?? "");
|
||||
const duration = uriObj.duration ? uriObj.duration.toString() : "";
|
||||
return `spotify:local:${artist}:${album}:${title}:${duration}`;
|
||||
}
|
||||
return `spotify:${uriObj.type}:${uriObj.id}`;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -123,19 +123,19 @@ const buildSpotifyURI = (uriObj) => {
|
||||
* @returns {string}
|
||||
*/
|
||||
const buildSpotifyLink = (uriObj) => {
|
||||
if (uriObj.is_local) {
|
||||
const artist = encodeURIComponent(uriObj.artist ?? "");
|
||||
const album = encodeURIComponent(uriObj.album ?? "");
|
||||
const title = encodeURIComponent(uriObj.title ?? "");
|
||||
const duration = uriObj.duration ? uriObj.duration.toString() : "";
|
||||
return `https://open.spotify.com/local/${artist}/${album}/${title}/${duration}`;
|
||||
}
|
||||
return `https://open.spotify.com/${uriObj.type}/${uriObj.id}`
|
||||
if (uriObj.is_local) {
|
||||
const artist = encodeURIComponent(uriObj.artist ?? "");
|
||||
const album = encodeURIComponent(uriObj.album ?? "");
|
||||
const title = encodeURIComponent(uriObj.title ?? "");
|
||||
const duration = uriObj.duration ? uriObj.duration.toString() : "";
|
||||
return `https://open.spotify.com/local/${artist}/${album}/${title}/${duration}`;
|
||||
}
|
||||
return `https://open.spotify.com/${uriObj.type}/${uriObj.id}`
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
parseSpotifyURI,
|
||||
parseSpotifyLink,
|
||||
buildSpotifyURI,
|
||||
buildSpotifyLink
|
||||
parseSpotifyURI,
|
||||
parseSpotifyLink,
|
||||
buildSpotifyURI,
|
||||
buildSpotifyLink
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user