UNPKG

social-link-parser

Version:

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

67 lines 2.46 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 = ['snapchat.com']; const subdomains = ['story']; const DOMAIN_PATTERN = createDomainPattern(domains, subdomains); export const snapchat = { id: Platforms.Snapchat, name: 'Snapchat', color: '#FFFC00', domains: domains, subdomains: subdomains, patterns: { profile: new RegExp(`^https?://${DOMAIN_PATTERN}/add/([A-Za-z0-9._-]{3,15})/?${QUERY_HASH}$`, 'i'), handle: /^[A-Za-z0-9._-]{3,15}$/, content: { story: new RegExp(`^https?://${DOMAIN_PATTERN}/s/([A-Za-z0-9._-]+)/?${QUERY_HASH}$`, 'i'), spotlight: new RegExp(`^https?://${DOMAIN_PATTERN}/spotlight/([A-Za-z0-9]{2,})/?${QUERY_HASH}$`, 'i'), }, }, detect(url) { if (!this.domains.some(d => url.includes(d))) 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) { const storyMatch = this.patterns.content?.story?.exec(url); if (storyMatch) { result.ids.storyId = storyMatch[1]; result.metadata.isStory = true; result.metadata.contentType = 'story'; return; } const spotlightMatch = this.patterns.content?.spotlight?.exec(url); if (spotlightMatch) { result.ids.spotlightId = spotlightMatch[1]; result.metadata.isSpotlight = true; result.metadata.contentType = 'spotlight'; return; } const profileMatch = this.patterns.profile.exec(url); if (profileMatch) { result.username = profileMatch[1]; result.metadata.isProfile = true; result.metadata.contentType = 'profile'; } }, validateHandle(handle) { return this.patterns.handle.test(handle.replace('@', '')); }, buildProfileUrl(username) { return `https://snapchat.com/add/${username}`; }, normalizeUrl(url) { return normalize(url); }, }; //# sourceMappingURL=index.js.map