UNPKG

social-link-parser

Version:

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

78 lines 2.94 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.twitch = 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 = ['twitch.tv']; const subdomains = ['clips']; const DOMAIN_PATTERN = (0, url_2.createDomainPattern)(domains, subdomains); exports.twitch = { id: types_1.Platforms.Twitch, name: 'Twitch', color: '#9146FF', domains: domains, subdomains: subdomains, patterns: { profile: new RegExp(`^https?://${DOMAIN_PATTERN}/([A-Za-z0-9_]{4,25})/?${constants_1.QUERY_HASH}$`, 'i'), handle: /^[A-Za-z0-9_]{4,25}$/, content: { video: new RegExp(`^https?://${DOMAIN_PATTERN}/videos/(\\d+)/?${constants_1.QUERY_HASH}$`, 'i'), clip: new RegExp(`^https?://(?:www\\.)?clips\\.twitch\\.tv/([A-Za-z0-9_-]+)/?${constants_1.QUERY_HASH}$`, 'i'), collection: new RegExp(`^https?://${DOMAIN_PATTERN}/collections/([A-Za-z0-9]{2,})/?${constants_1.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 clipMatch = this.patterns.content?.clip?.exec(url); if (clipMatch) { result.ids.clipName = clipMatch[1]; result.metadata.isClip = true; result.metadata.contentType = 'clip'; 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 collectionMatch = this.patterns.content?.collection?.exec(url); if (collectionMatch) { result.ids.collectionId = collectionMatch[1]; result.metadata.isCollection = true; result.metadata.contentType = 'collection'; 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); }, buildProfileUrl(username) { return `https://twitch.tv/${username}`; }, normalizeUrl(url) { return (0, url_1.normalize)(url); }, }; //# sourceMappingURL=index.js.map