UNPKG

social-link-parser

Version:

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

71 lines 2.76 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 = ['t.me', 'telegram.me']; const subdomains = []; const DOMAIN_PATTERN = createDomainPattern(domains, subdomains); export const telegram = { id: Platforms.Telegram, name: 'Telegram', color: '#0088CC', domains: domains, subdomains: subdomains, patterns: { profile: new RegExp(`^https?://${DOMAIN_PATTERN}/([A-Za-z0-9_]{5,32})/?${QUERY_HASH}$`, 'i'), handle: /^[A-Za-z0-9_]{5,32}$/, content: { channel: new RegExp(`^https?://${DOMAIN_PATTERN}/s/([A-Za-z0-9_]{5,32})/?${QUERY_HASH}$`, 'i'), post: new RegExp(`^https?://${DOMAIN_PATTERN}/([A-Za-z0-9_]{5,32})/(\\d+)/?${QUERY_HASH}$`, 'i'), join: new RegExp(`^https?://${DOMAIN_PATTERN}/joinchat/([A-Za-z0-9_-]{10,})/?${QUERY_HASH}$`, 'i'), }, }, detect(url) { if (!this.domains.some(d => url.includes(d))) return false; return this.patterns.profile.test(url) || !!(this.patterns.content?.channel?.test(url)) || !!(this.patterns.content?.post?.test(url)) || !!(this.patterns.content?.join?.test(url)); }, extract(url, result) { const joinMatch = this.patterns.content?.join?.exec(url); if (joinMatch) { result.ids.joinCode = joinMatch[1]; result.metadata.isJoin = true; result.metadata.contentType = 'join'; return; } const channelMatch = this.patterns.content?.channel?.exec(url); if (channelMatch) { result.ids.channelName = channelMatch[1]; result.metadata.isChannel = true; result.metadata.contentType = 'channel'; return; } const postMatch = this.patterns.content?.post?.exec(url); if (postMatch) { result.ids.channelName = postMatch[1]; result.ids.postId = postMatch[2]; result.metadata.isPost = true; result.metadata.contentType = 'post'; 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://t.me/${username}`; }, normalizeUrl(url) { return normalize(url); }, }; //# sourceMappingURL=index.js.map