social-link-parser
Version:
Extract usernames, IDs, and metadata from social media URLs across 100+ platforms
118 lines • 4.6 kB
JavaScript
import { Platforms } from '../../core/types.js';
import { normalize } from '../../utils/url.js';
import { QUERY_HASH } from '../../utils/constants.js';
const domains = ['spotify.com'];
const subdomains = ['open'];
const DOMAIN_PATTERN = 'open\\.spotify\\.com';
export const spotify = {
id: Platforms.Spotify,
name: 'Spotify',
color: '#1DB954',
domains: domains,
subdomains: subdomains,
patterns: {
profile: new RegExp(`^https?://${DOMAIN_PATTERN}/user/([A-Za-z0-9._-]{2,32})/?${QUERY_HASH}$`, 'i'),
handle: /^[A-Za-z0-9._-]{2,32}$/,
content: {
artist: new RegExp(`^https?://${DOMAIN_PATTERN}/artist/([A-Za-z0-9]{20,22})/?${QUERY_HASH}$`, 'i'),
track: new RegExp(`^https?://${DOMAIN_PATTERN}/track/([A-Za-z0-9]{20,22})/?${QUERY_HASH}$`, 'i'),
album: new RegExp(`^https?://${DOMAIN_PATTERN}/album/([A-Za-z0-9]{20,22})/?${QUERY_HASH}$`, 'i'),
playlist: new RegExp(`^https?://${DOMAIN_PATTERN}/playlist/([A-Za-z0-9]{20,22})/?${QUERY_HASH}$`, 'i'),
embed: new RegExp(`^https?://${DOMAIN_PATTERN}/embed/(track|album|playlist|artist)/([A-Za-z0-9]{20,22})/?${QUERY_HASH}$`, 'i'),
},
},
detect(url) {
if (!this.domains.some(d => url.includes(d)))
return false;
if (!url.includes('open.spotify.com'))
return false;
if (this.patterns.content?.embed?.test(url))
return true;
if (this.patterns.content?.artist?.test(url))
return true;
if (this.patterns.content?.track?.test(url))
return true;
if (this.patterns.content?.album?.test(url))
return true;
if (this.patterns.content?.playlist?.test(url))
return true;
if (this.patterns.profile.test(url))
return true;
return false;
},
extract(url, result) {
const embedMatch = this.patterns.content?.embed?.exec(url);
if (embedMatch) {
const [, type, id] = embedMatch;
result.ids[`${type}Id`] = id;
result.metadata.contentType = type;
result.metadata.isEmbed = true;
return;
}
const artistMatch = this.patterns.content?.artist?.exec(url);
if (artistMatch) {
result.ids.artistId = artistMatch[1];
result.metadata.contentType = 'artist';
return;
}
const trackMatch = this.patterns.content?.track?.exec(url);
if (trackMatch) {
result.ids.trackId = trackMatch[1];
result.metadata.contentType = 'track';
return;
}
const albumMatch = this.patterns.content?.album?.exec(url);
if (albumMatch) {
result.ids.albumId = albumMatch[1];
result.metadata.contentType = 'album';
return;
}
const playlistMatch = this.patterns.content?.playlist?.exec(url);
if (playlistMatch) {
result.ids.playlistId = playlistMatch[1];
result.metadata.contentType = 'playlist';
return;
}
const profileMatch = this.patterns.profile.exec(url);
if (profileMatch) {
result.username = profileMatch[1];
result.metadata.isProfile = true;
result.metadata.contentType = 'profile';
return;
}
},
validateHandle(handle) {
return this.patterns.handle.test(handle);
},
buildProfileUrl(username) {
return `https://open.spotify.com/user/${username}`;
},
buildContentUrl(contentType, id) {
return `https://open.spotify.com/${contentType}/${id}`;
},
generateEmbedUrl(contentType, id) {
return `https://open.spotify.com/embed/${contentType}/${id}`;
},
getEmbedInfo(url, parsed) {
if (url.includes('/embed/')) {
return { embedUrl: url, isEmbedAlready: true };
}
const types = [
['track', parsed.ids.trackId],
['album', parsed.ids.albumId],
['playlist', parsed.ids.playlistId],
['artist', parsed.ids.artistId],
];
for (const [type, id] of types) {
if (id) {
const embedUrl = this.generateEmbedUrl ? this.generateEmbedUrl(type, id) : `https://open.spotify.com/embed/${type}/${id}`;
return { embedUrl, type: 'iframe' };
}
}
return null;
},
normalizeUrl(url) {
return normalize(url.replace(/[?&](si|utm_[^&]+)=[^&]+/g, ''));
},
};
//# sourceMappingURL=index.js.map