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'));
|
|
|
|
const cheerio = require('cheerio');
|
|
|
|
const moment = require('moment');
|
|
|
|
|
|
|
|
const { matchTags } = require('../tags');
|
|
|
|
|
|
|
|
function scrapeLatest(html, site) {
|
|
|
|
const $ = cheerio.load(html, { normalizeWhitespace: true });
|
|
|
|
const sceneElements = $('.card.card--release').toArray();
|
|
|
|
|
2019-11-04 04:47:37 +00:00
|
|
|
console.log(sceneElements);
|
|
|
|
|
2019-04-07 23:49:45 +00:00
|
|
|
return sceneElements.map((element) => {
|
|
|
|
const sceneLinkElement = $(element).find('.card-info__title a');
|
|
|
|
const title = sceneLinkElement.attr('title');
|
|
|
|
const url = `${site.url}${sceneLinkElement.attr('href')}`;
|
|
|
|
const entryId = url.split('/').slice(-3)[0];
|
|
|
|
|
|
|
|
const date = moment.utc($(element).find('.card-info__meta-date').text(), 'MMMM DD, YYYY').toDate();
|
|
|
|
const actors = $(element).find('.card-info__cast a').map((actorIndex, actorElement) => $(actorElement).text().trim()).toArray();
|
|
|
|
|
2019-11-04 04:47:37 +00:00
|
|
|
console.log(date, actors, title);
|
|
|
|
|
2019-04-07 23:49:45 +00:00
|
|
|
return {
|
|
|
|
url,
|
|
|
|
entryId,
|
|
|
|
title,
|
|
|
|
actors,
|
|
|
|
date,
|
|
|
|
rating: null,
|
|
|
|
site,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function scrapeScene(data, url, site) {
|
|
|
|
const {
|
|
|
|
id: entryId,
|
|
|
|
title,
|
|
|
|
description,
|
|
|
|
} = data;
|
|
|
|
|
|
|
|
const date = new Date(data.dateReleased);
|
|
|
|
const actors = data.actors
|
|
|
|
.sort(({ gender: genderA }, { gender: genderB }) => {
|
|
|
|
if (genderA === 'female' && genderB === 'male') return -1;
|
|
|
|
if (genderA === 'male' && genderB === 'female') return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
})
|
|
|
|
.map(actor => actor.name);
|
|
|
|
|
|
|
|
const { likes, dislikes } = data.stats;
|
|
|
|
const duration = data.videos.mediabook.length;
|
|
|
|
|
2019-11-04 04:47:37 +00:00
|
|
|
console.log(data);
|
|
|
|
|
2019-04-07 23:49:45 +00:00
|
|
|
const rawTags = data.tags.map(tag => tag.name);
|
|
|
|
const tags = await matchTags(rawTags);
|
|
|
|
|
|
|
|
return {
|
|
|
|
url,
|
|
|
|
entryId,
|
|
|
|
title,
|
|
|
|
description,
|
|
|
|
actors,
|
|
|
|
date,
|
|
|
|
duration,
|
|
|
|
tags,
|
|
|
|
rating: {
|
|
|
|
likes,
|
|
|
|
dislikes,
|
|
|
|
},
|
|
|
|
site,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async function fetchLatest(site, page = 1) {
|
|
|
|
const res = await bhttp.get(`https://www.realitykings.com/tour/videos/${site.name.replace(/\s+/g, '-').toLowerCase()}/all-categories/all-time/recent/${page}`);
|
|
|
|
|
|
|
|
return scrapeLatest(res.body.toString(), site);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function fetchScene(url, site) {
|
|
|
|
if (site.isFallback || (site.parameters && site.parameters.altLayout)) {
|
|
|
|
throw new Error('Cannot fetch scene details from this resource');
|
|
|
|
}
|
|
|
|
|
|
|
|
const entryId = url.split('/').slice(-1)[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,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
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,
|
|
|
|
};
|