2019-04-07 23:49:45 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/* eslint-disable newline-per-chained-call */
|
|
|
|
const Promise = require('bluebird');
|
|
|
|
const bhttp = require('bhttp');
|
|
|
|
const { CookieJar } = Promise.promisifyAll(require('tough-cookie'));
|
2019-11-09 00:22:50 +00:00
|
|
|
const { JSDOM } = require('jsdom');
|
2019-04-07 23:49:45 +00:00
|
|
|
|
|
|
|
const { matchTags } = require('../tags');
|
|
|
|
|
2019-11-09 00:22:50 +00:00
|
|
|
async function scrapeLatest(html, site) {
|
|
|
|
const { document } = new JSDOM(html).window;
|
2019-04-07 23:49:45 +00:00
|
|
|
|
2019-11-09 00:22:50 +00:00
|
|
|
const scriptString = document.querySelector('script').textContent;
|
|
|
|
const prefix = 'window.__JUAN.initialState = {';
|
2019-11-04 04:47:37 +00:00
|
|
|
|
2019-11-09 00:22:50 +00:00
|
|
|
const dataStart = scriptString.slice(scriptString.indexOf(prefix) + prefix.length - 1);
|
|
|
|
const dataString = dataStart.slice(0, dataStart.indexOf('};') + 1);
|
|
|
|
const data = JSON.parse(dataString);
|
2019-04-07 23:49:45 +00:00
|
|
|
|
2019-11-09 00:22:50 +00:00
|
|
|
const actorsMap = data.entities.actors;
|
|
|
|
const tagsMap = data.entities.tags;
|
|
|
|
const scenes = Object.values(data.entities.releases);
|
2019-04-07 23:49:45 +00:00
|
|
|
|
2019-11-09 00:22:50 +00:00
|
|
|
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;
|
2019-11-04 04:47:37 +00:00
|
|
|
|
2019-04-07 23:49:45 +00:00
|
|
|
return {
|
|
|
|
url,
|
|
|
|
entryId,
|
|
|
|
title,
|
2019-11-09 00:22:50 +00:00
|
|
|
description,
|
2019-04-07 23:49:45 +00:00
|
|
|
actors,
|
|
|
|
date,
|
2019-11-09 00:22:50 +00:00
|
|
|
tags,
|
|
|
|
duration,
|
|
|
|
poster,
|
|
|
|
photos,
|
|
|
|
trailer: {
|
|
|
|
src: trailer720p || trailer360p,
|
|
|
|
quality: trailer720p ? 720 : 360,
|
|
|
|
},
|
|
|
|
rating: { likes, dislikes },
|
2019-04-07 23:49:45 +00:00
|
|
|
site,
|
|
|
|
};
|
2019-11-09 00:22:50 +00:00
|
|
|
}));
|
2019-04-07 23:49:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function scrapeScene(data, url, site) {
|
|
|
|
const {
|
|
|
|
id: entryId,
|
|
|
|
title,
|
|
|
|
description,
|
|
|
|
} = data;
|
|
|
|
|
|
|
|
const date = new Date(data.dateReleased);
|
2019-11-09 00:22:50 +00:00
|
|
|
const actors = data.actors.map(actor => actor.name);
|
2019-04-07 23:49:45 +00:00
|
|
|
|
|
|
|
const { likes, dislikes } = data.stats;
|
2019-11-04 04:47:37 +00:00
|
|
|
|
2019-04-07 23:49:45 +00:00
|
|
|
const rawTags = data.tags.map(tag => tag.name);
|
|
|
|
const tags = await matchTags(rawTags);
|
|
|
|
|
2019-11-09 00:22:50 +00:00
|
|
|
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;
|
|
|
|
|
2019-04-07 23:49:45 +00:00
|
|
|
return {
|
|
|
|
url,
|
|
|
|
entryId,
|
|
|
|
title,
|
|
|
|
description,
|
|
|
|
actors,
|
|
|
|
date,
|
|
|
|
duration,
|
|
|
|
tags,
|
2019-11-09 00:22:50 +00:00
|
|
|
poster,
|
|
|
|
photos,
|
|
|
|
trailer: {
|
|
|
|
src: trailer720p || trailer360p,
|
|
|
|
quality: trailer720p ? 720 : 360,
|
|
|
|
},
|
2019-04-07 23:49:45 +00:00
|
|
|
rating: {
|
|
|
|
likes,
|
|
|
|
dislikes,
|
|
|
|
},
|
|
|
|
site,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async function fetchLatest(site, page = 1) {
|
2019-11-09 00:22:50 +00:00
|
|
|
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);
|
|
|
|
}
|
2019-04-07 23:49:45 +00:00
|
|
|
|
2019-11-09 00:22:50 +00:00
|
|
|
throw new Error(`Reality Kings site '${site.name}' (${site.url}) not supported`);
|
2019-04-07 23:49:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function fetchScene(url, site) {
|
2019-11-16 22:37:33 +00:00
|
|
|
const entryId = url.match(/\d+/)[0];
|
2019-04-07 23:49:45 +00:00
|
|
|
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2019-04-10 01:42:20 +00:00
|
|
|
return scrapeScene(res.body.result.parent || res.body.result, url, site);
|
2019-04-07 23:49:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
fetchLatest,
|
|
|
|
fetchScene,
|
|
|
|
};
|