33 lines
1.3 KiB
JavaScript
33 lines
1.3 KiB
JavaScript
|
import cheerio from 'cheerio'
|
||
|
|
||
|
/**
|
||
|
*
|
||
|
* @param {Object} limiter An instance of node-rate-limiter, see https://github.com/jhurliman/node-rate-limiter
|
||
|
* @param {String} roomUrl example: https://chaturbate.com/projektmelody
|
||
|
* @returns {Object} initialRoomDossier
|
||
|
*/
|
||
|
export async function getInitialRoomDossier(limiter, roomUrl) {
|
||
|
await limiter.removeTokens(1);
|
||
|
try {
|
||
|
const res = await fetch(roomUrl, {
|
||
|
headers: {
|
||
|
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36',
|
||
|
}
|
||
|
});
|
||
|
const body = await res.text()
|
||
|
const $ = cheerio.load(body);
|
||
|
let rawScript = $('script:contains(window.initialRoomDossier)').html();
|
||
|
if (!rawScript) {
|
||
|
throw new Error('window.initialRoomDossier is null. This could mean the channel is in password mode');
|
||
|
}
|
||
|
let rawDossier = rawScript.slice(rawScript.indexOf('"'), rawScript.lastIndexOf('"') + 1);
|
||
|
let dossier = JSON.parse(JSON.parse(rawDossier));
|
||
|
|
||
|
return dossier;
|
||
|
} catch (error) {
|
||
|
// Handle the error gracefully
|
||
|
console.log(`Error fetching initial room dossier: ${error.message}`);
|
||
|
return null; // Or any other appropriate action you want to take
|
||
|
}
|
||
|
}
|