traxxx/src/scrapers/realitykings.js

160 lines
4.6 KiB
JavaScript
Raw Normal View History

'use strict';
/* eslint-disable newline-per-chained-call */
const Promise = require('bluebird');
const bhttp = require('bhttp');
const { CookieJar } = Promise.promisifyAll(require('tough-cookie'));
const { JSDOM } = require('jsdom');
const { matchTags } = require('../tags');
async function scrapeLatest(html, site) {
const { document } = new JSDOM(html).window;
const scriptString = document.querySelector('script').textContent;
const prefix = 'window.__JUAN.initialState = {';
const dataStart = scriptString.slice(scriptString.indexOf(prefix) + prefix.length - 1);
const dataString = dataStart.slice(0, dataStart.indexOf('};') + 1);
const data = JSON.parse(dataString);
const actorsMap = data.entities.actors;
const tagsMap = data.entities.tags;
const scenes = Object.values(data.entities.releases);
return Promise.all(scenes.map(async (scene) => {
const {
id: entryId,
title,
description,
} = scene;
const url = `https://www.realitykings.com/scene/${entryId}`;
const date = new Date(scene.dateReleased);
const actors = scene.actors.map(actorId => actorsMap[actorId].name);
const rawTags = scene.tags.map(tagId => tagsMap[tagId].name);
const tags = await matchTags(rawTags);
if (!scene.images.poster) {
console.log(site.name, site.id);
console.log(scene);
console.log(title, url, scene.images);
}
const [poster, ...photos] = scene.images.poster.map(image => image.xl.url);
const duration = scene.videos.mediabook.length;
const trailer720p = scene.videos.mediabook.files['720p'] && scene.videos.mediabook.files['720p'].urls.view;
const trailer360p = scene.videos.mediabook.files['360p'] && scene.videos.mediabook.files['360p'].urls.view;
const { likes, dislikes } = scene.stats;
return {
url,
entryId,
title,
description,
actors,
date,
tags,
duration,
poster,
photos,
trailer: {
src: trailer720p || trailer360p,
quality: trailer720p ? 720 : 360,
},
rating: { likes, dislikes },
site,
};
}));
}
async function scrapeScene(data, url, site) {
const {
id: entryId,
title,
description,
} = data;
const date = new Date(data.dateReleased);
const actors = data.actors.map(actor => actor.name);
const { likes, dislikes } = data.stats;
const rawTags = data.tags.map(tag => tag.name);
const tags = await matchTags(rawTags);
const [poster, ...photos] = data.images.poster.map(image => image.xl.url);
const duration = data.videos.mediabook.length;
const trailer720p = data.videos.mediabook.files['720p'] && data.videos.mediabook.files['720p'].urls.view;
const trailer360p = data.videos.mediabook.files['360p'] && data.videos.mediabook.files['360p'].urls.view;
return {
url,
entryId,
title,
description,
actors,
date,
duration,
tags,
poster,
photos,
trailer: {
src: trailer720p || trailer360p,
quality: trailer720p ? 720 : 360,
},
rating: {
likes,
dislikes,
},
site,
};
}
async function fetchLatest(site, page = 1) {
const { hostname, search } = new URL(site.url);
if (hostname.match(/(www\.)?realitykings\.com/) && search.match(/\?site=\d+/)) {
const res = await bhttp.get(`${site.url}&page=${page}`);
return scrapeLatest(res.body.toString(), site);
}
if (site.parameters && site.parameters.siteId) {
const res = await bhttp.get(`https://www.realitykings.com/scenes?site=${site.parameters.siteId}&page=${page}`);
return scrapeLatest(res.body.toString(), site);
}
throw new Error(`Reality Kings site '${site.name}' (${site.url}) not supported`);
}
async function fetchScene(url, site) {
const entryId = url.match(/\d+/)[0];
const cookieJar = new CookieJar();
const session = bhttp.session({ cookieJar });
await session.get(url);
const cookies = await cookieJar.getCookieStringAsync(url);
const instanceToken = cookies.split(';')[0].split('=')[1];
const res = await session.get(`https://site-api.project1service.com/v2/releases/${entryId}`, {
headers: {
Instance: instanceToken,
},
});
return scrapeScene(res.body.result.parent || res.body.result, url, site);
}
module.exports = {
fetchLatest,
fetchScene,
};