social-link-parser
Version:
Extract usernames, IDs, and metadata from social media URLs across 100+ platforms
116 lines • 4.88 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.facebook = void 0;
const types_1 = require("../../core/types");
const url_1 = require("../../utils/url");
const url_2 = require("../../utils/url");
const constants_1 = require("../../utils/constants");
const domains = ['facebook.com', 'fb.com'];
const subdomains = ['m', 'mobile'];
const DOMAIN_PATTERN = (0, url_2.createDomainPattern)(domains, subdomains);
exports.facebook = {
id: types_1.Platforms.Facebook,
name: 'Facebook',
color: '#1877F2',
domains: domains,
subdomains: subdomains,
patterns: {
profile: new RegExp(`^https?://${DOMAIN_PATTERN}/([A-Za-z0-9.]{5,})/?${constants_1.QUERY_HASH}$`, 'i'),
handle: /^[A-Za-z0-9.]{5,}$/,
content: {
profileId: new RegExp(`^https?://${DOMAIN_PATTERN}/profile\\.php\\?id=(\\d+)(?:&.*)?${constants_1.QUERY_HASH}$`, 'i'),
page: new RegExp(`^https?://${DOMAIN_PATTERN}/pages/[^/]+/(\\d{2,})/?${constants_1.QUERY_HASH}$`, 'i'),
post: new RegExp(`^https?://${DOMAIN_PATTERN}/[A-Za-z0-9.]+/posts/(\\d+)/?${constants_1.QUERY_HASH}$`, 'i'),
video: new RegExp(`^https?://${DOMAIN_PATTERN}/watch/\\?v=(\\d+)(?:&.*)?${constants_1.QUERY_HASH}$`, 'i'),
group: new RegExp(`^https?://${DOMAIN_PATTERN}/groups/([A-Za-z0-9._-]{3,})/?${constants_1.QUERY_HASH}$`, 'i'),
event: new RegExp(`^https?://${DOMAIN_PATTERN}/events/(\\d+)/?${constants_1.QUERY_HASH}$`, 'i'),
live: new RegExp(`^https?://${DOMAIN_PATTERN}/([A-Za-z0-9.]{5,})/live/?${constants_1.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 (0, url_1.normalize)(url);
},
};
//# sourceMappingURL=index.js.map