UNPKG

social-link-parser

Version:

Extract usernames, IDs, and metadata from social media URLs across 100+ platforms

96 lines 3.67 kB
import { Platforms } from '../../core/types.js'; import { normalize } from '../../utils/url.js'; import { createDomainPattern } from '../../utils/url.js'; import { QUERY_HASH } from '../../utils/constants.js'; const domains = ['soundcloud.com']; const subdomains = ['w']; const DOMAIN_PATTERN = createDomainPattern(domains, subdomains); export const soundcloud = { id: Platforms.SoundCloud, name: 'SoundCloud', color: '#FF5500', domains: domains, subdomains: subdomains, patterns: { profile: new RegExp(`^https?://(?:(?:www\\.)?soundcloud\\.com)/([A-Za-z0-9_-]{2,25})/?${QUERY_HASH}$`, 'i'), handle: /^[A-Za-z0-9_-]{2,25}$/, content: { track: new RegExp(`^https?://${DOMAIN_PATTERN}/([A-Za-z0-9_-]{2,25})/(?!sets/)([A-Za-z0-9_-]+)/?${QUERY_HASH}$`, 'i'), set: new RegExp(`^https?://${DOMAIN_PATTERN}/([A-Za-z0-9_-]{2,25})/sets/([A-Za-z0-9_-]{1,})/?${QUERY_HASH}$`, 'i'), embed: new RegExp(`^https?://w\\.soundcloud\\.com/player/\\?url=.+`, 'i'), }, }, detect(url) { if (!this.domains.some(domain => url.includes(domain))) return false; if (this.patterns.profile.test(url)) return true; if (this.patterns.content) { for (const pattern of Object.values(this.patterns.content)) { if (pattern && pattern.test(url)) return true; } } return false; }, extract(url, result) { if (this.patterns.content?.embed?.test(url)) { result.metadata.contentType = 'embed'; result.metadata.isEmbed = true; return; } const setMatch = this.patterns.content?.set?.exec(url); if (setMatch) { result.username = setMatch[1]; result.ids.setId = setMatch[2]; result.metadata.contentType = 'set'; result.metadata.isSet = true; return; } const trackMatch = this.patterns.content?.track?.exec(url); if (trackMatch) { result.username = trackMatch[1]; result.ids.trackId = trackMatch[2]; result.metadata.contentType = 'track'; result.metadata.isTrack = true; 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://soundcloud.com/${username}`; }, buildContentUrl(contentType, id) { if (contentType === 'track') { return `https://soundcloud.com/track/${id}`; } return `https://soundcloud.com/${id}`; }, normalizeUrl(url) { return normalize(url.replace(/[?&]utm_[^&]+/g, '')); }, generateEmbedUrl(contentId) { return `https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/${contentId}`; }, getEmbedInfo(url, parsed) { if (url.includes('w.soundcloud.com/player')) { return { embedUrl: url, isEmbedAlready: true }; } const id = parsed.ids.trackId; if (id) { const embedUrl = this.generateEmbedUrl ? this.generateEmbedUrl(id) : `https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/${id}`; return { embedUrl, type: 'iframe' }; } return null; }, }; //# sourceMappingURL=index.js.map