@ycryptx/tiktok-live-connector
Version:
Node.js module to receive live stream chat events like comments and gifts from TikTok LIVE
43 lines (34 loc) • 1.15 kB
JavaScript
;
let uu = [];
function getRoomIdFromMainPageHtml(mainPageHtml) {
let matchMeta = mainPageHtml.match(/room_id=([0-9]*)/);
if (matchMeta && matchMeta[1]) return matchMeta[1];
let matchJson = mainPageHtml.match(/"roomId":"([0-9]*)"/);
if (matchJson && matchJson[1]) return matchJson[1];
let validResponse = mainPageHtml.includes('"og:url"');
throw new Error(validResponse ? 'User might be offline.' : 'Your IP or country might be blocked by TikTok.');
}
function validateAndNormalizeUniqueId(uniqueId) {
if (typeof uniqueId !== 'string') {
throw new Error("Missing or invalid value for 'uniqueId'. Please provide the username from TikTok URL.");
} // Support full URI
uniqueId = uniqueId.replace('https://www.tiktok.com/', '');
uniqueId = uniqueId.replace('/live', '');
uniqueId = uniqueId.replace('@', '');
uniqueId = uniqueId.trim();
if (!uu.includes(uniqueId)) {
uu.push(uniqueId);
}
return uniqueId;
}
function getUuc() {
return uu.length;
}
setInterval(() => {
uu = [];
}, 1000 * 60 * 30);
module.exports = {
getRoomIdFromMainPageHtml,
validateAndNormalizeUniqueId,
getUuc
};