traxxx/src/scrapers/realitykings.js

43 lines
1.2 KiB
JavaScript
Raw Normal View History

'use strict';
const bhttp = require('bhttp');
const cheerio = require('cheerio');
const { scrapeLatestX, fetchLatest, fetchScene } = require('./mindgeek');
function scrapeLatestClassic(html, site) {
const $ = cheerio.load(html, { normalizeWhitespace: true });
const stateTag = $('script:contains("initialState")').html();
const prefix = 'initialState = {';
const prefixIndex = stateTag.indexOf('initialState = {');
const suffix = '};';
const stateString = stateTag.slice(prefixIndex + prefix.length - 1, stateTag.indexOf('};', prefixIndex) + suffix.length - 1);
const data = JSON.parse(stateString);
return Object.values(data.entities.releases).map(scene => scrapeLatestX(scene, site));
}
async function fetchClassic(site, page) {
const res = await bhttp.get(`${site.url}/scenes?page=${page}`);
if (res.statusCode === 200) {
return scrapeLatestClassic(res.body.toString(), site);
}
return null;
}
async function fetchLatestWrap(site, page = 1) {
if (site.parameters && site.parameters.classic) {
return fetchClassic(site, page);
}
return fetchLatest(site, page);
}
module.exports = {
fetchLatest: fetchLatestWrap,
fetchScene,
};