31 lines
1.1 KiB
JavaScript
31 lines
1.1 KiB
JavaScript
import cheerio from 'cheerio'
|
|
|
|
/**
|
|
*
|
|
* @param {String} roomUrl example: https://chaturbate.com/projektmelody
|
|
* @returns {Object} initialRoomDossier
|
|
*/
|
|
export async function getInitialRoomDossier(roomUrl) {
|
|
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
|
|
}
|
|
}
|