UNPKG

social-link-parser

Version:

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

113 lines 4.68 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 = ['facebook.com', 'fb.com']; const subdomains = ['m', 'mobile']; const DOMAIN_PATTERN = createDomainPattern(domains, subdomains); export const facebook = { id: Platforms.Facebook, name: 'Facebook', color: '#1877F2', domains: domains, subdomains: subdomains, patterns: { profile: new RegExp(`^https?://${DOMAIN_PATTERN}/([A-Za-z0-9.]{5,})/?${QUERY_HASH}$`, 'i'), handle: /^[A-Za-z0-9.]{5,}$/, content: { profileId: new RegExp(`^https?://${DOMAIN_PATTERN}/profile\\.php\\?id=(\\d+)(?:&.*)?${QUERY_HASH}$`, 'i'), page: new RegExp(`^https?://${DOMAIN_PATTERN}/pages/[^/]+/(\\d{2,})/?${QUERY_HASH}$`, 'i'), post: new RegExp(`^https?://${DOMAIN_PATTERN}/[A-Za-z0-9.]+/posts/(\\d+)/?${QUERY_HASH}$`, 'i'), video: new RegExp(`^https?://${DOMAIN_PATTERN}/watch/\\?v=(\\d+)(?:&.*)?${QUERY_HASH}$`, 'i'), group: new RegExp(`^https?://${DOMAIN_PATTERN}/groups/([A-Za-z0-9._-]{3,})/?${QUERY_HASH}$`, 'i'), event: new RegExp(`^https?://${DOMAIN_PATTERN}/events/(\\d+)/?${QUERY_HASH}$`, 'i'), live: new RegExp(`^https?://${DOMAIN_PATTERN}/([A-Za-z0-9.]{5,})/live/?${QUERY_HASH}$`, 'i'), }, }, detect(url) { if (!this.domains.some(domain => url.includes(domain))) return false; if (this.patterns.profile.test(url)) return true; for (const p of Object.values(this.patterns.content || {})) { if (p && p.test(url)) return true; } return false; }, extract(url, result) { const profileIdMatch = this.patterns.content?.profileId?.exec(url); if (profileIdMatch) { result.ids.profileId = profileIdMatch[1]; result.metadata.isProfile = true; result.metadata.contentType = 'profile'; return; } const pageMatch = this.patterns.content?.page?.exec(url); if (pageMatch) { result.ids.pageId = pageMatch[1]; result.metadata.isPage = true; result.metadata.contentType = 'page'; return; } const videoMatch = this.patterns.content?.video?.exec(url); if (videoMatch) { result.ids.videoId = videoMatch[1]; result.metadata.isVideo = true; result.metadata.contentType = 'video'; return; } const groupMatch = this.patterns.content?.group?.exec(url); if (groupMatch) { result.ids.groupName = groupMatch[1]; result.metadata.isGroup = true; result.metadata.contentType = 'group'; return; } const eventMatch = this.patterns.content?.event?.exec(url); if (eventMatch) { result.ids.eventId = eventMatch[1]; result.metadata.isEvent = true; result.metadata.contentType = 'event'; return; } const postMatch = this.patterns.content?.post?.exec(url); if (postMatch) { result.ids.postId = postMatch[1]; result.metadata.isPost = true; result.metadata.contentType = 'post'; return; } const liveMatch = this.patterns.content?.live?.exec(url); if (liveMatch) { result.username = liveMatch[1]; result.metadata.isLive = true; result.metadata.contentType = 'live'; return; } const reserved = ['/posts/', '/videos/', '/watch/', '/groups/', '/pages/', '/events/', '/profile.php']; const profileMatch = this.patterns.profile.exec(url); if (profileMatch && !reserved.some(r => url.includes(r))) { result.username = profileMatch[1]; result.metadata.isProfile = true; result.metadata.contentType = 'profile'; } }, validateHandle(handle) { return this.patterns.handle.test(handle); }, buildProfileUrl(username) { return `https://facebook.com/${username}`; }, buildContentUrl(contentType, id) { if (contentType === 'video') { return `https://facebook.com/watch/?v=${id}`; } return `https://facebook.com/story.php?story_fbid=${id}`; }, normalizeUrl(url) { url = url.replace(/[?&](mibextid|ref|refsrc|_rdc|_rdr|sfnsn|hc_ref)=[^&]+/g, ''); return normalize(url); }, }; //# sourceMappingURL=index.js.map