UNPKG

social-link-parser

Version:

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

68 lines 2.45 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 = ['opensea.io']; const subdomains = []; const DOMAIN_PATTERN = createDomainPattern(domains, subdomains); export const opensea = { id: Platforms.OpenSea, name: 'OpenSea', color: '#2081E2', domains: domains, subdomains: subdomains, patterns: { profile: new RegExp(`^https?://${DOMAIN_PATTERN}/([A-Za-z0-9_.-]{3,40})/?${QUERY_HASH}$`, 'i'), handle: /^[A-Za-z0-9_.-]{3,40}$/, content: { collection: new RegExp(`^https?://${DOMAIN_PATTERN}/collection/([A-Za-z0-9_-]+)/?${QUERY_HASH}$`, 'i'), asset: new RegExp(`^https?://${DOMAIN_PATTERN}/assets/([^/]+)/([^/]+)/([^/]+)/?${QUERY_HASH}$`, 'i'), }, }, detect(url) { if (!this.domains.some(domain => url.includes(domain))) return false; return this.patterns.profile.test(url) || !!(this.patterns.content?.collection?.test(url)) || !!(this.patterns.content?.asset?.test(url)); }, extract(url, res) { const asset = this.patterns.content?.asset?.exec(url); if (asset) { res.ids.chain = asset[1]; res.ids.contract = asset[2]; res.ids.tokenId = asset[3]; res.metadata.isAsset = true; res.metadata.contentType = 'asset'; return; } const col = this.patterns.content?.collection?.exec(url); if (col) { res.ids.collectionName = col[1]; res.metadata.isCollection = true; res.metadata.contentType = 'collection'; return; } const prof = this.patterns.profile.exec(url); if (prof) { res.username = prof[1]; res.metadata.isProfile = true; res.metadata.contentType = 'profile'; } }, validateHandle(handle) { return this.patterns.handle.test(handle); }, buildProfileUrl(username) { return `https://opensea.io/${username}`; }, buildContentUrl(contentType, id) { if (contentType === 'collection') return `https://opensea.io/collection/${id}`; return ''; }, normalizeUrl(url) { return normalize(url); }, }; //# sourceMappingURL=index.js.map