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-27 03:58:38 +00:00
|
|
|
const moment = require('moment');
|
2019-04-07 23:49:45 +00:00
|
|
|
|
2019-11-27 03:58:38 +00:00
|
|
|
const { fetchSites } = require('../sites');
|
|
|
|
const { cookieToData } = require('../utils/cookies');
|
2019-04-07 23:49:45 +00:00
|
|
|
const { matchTags } = require('../tags');
|
|
|
|
|
2019-11-16 23:45:31 +00:00
|
|
|
function getThumbs(scene) {
|
|
|
|
if (scene.images.poster) {
|
|
|
|
return scene.images.poster.map(image => image.xl.url);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (scene.images.card_main_rect) {
|
|
|
|
return scene.images.card_main_rect
|
|
|
|
.concat(scene.images.card_secondary_rect || [])
|
|
|
|
.map(image => image.xl.url.replace('.thumb', ''));
|
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2019-11-27 03:58:38 +00:00
|
|
|
async function scrapeLatest(items, site) {
|
|
|
|
return Promise.all(items.map(async (data) => {
|
|
|
|
const { id: entryId, title, description } = data;
|
2019-11-16 23:45:31 +00:00
|
|
|
const url = `https://www.realitykings.com/scene/${entryId}/`;
|
2019-11-27 03:58:38 +00:00
|
|
|
const date = new Date(data.dateReleased);
|
|
|
|
const actors = data.actors.map(actor => actor.name);
|
2019-11-09 00:22:50 +00:00
|
|
|
|
2019-11-27 03:58:38 +00:00
|
|
|
const rawTags = data.tags.map(tag => tag.name);
|
2019-11-09 00:22:50 +00:00
|
|
|
const tags = await matchTags(rawTags);
|
|
|
|
|
2019-11-27 03:58:38 +00:00
|
|
|
const [poster, ...photos] = getThumbs(data);
|
|
|
|
const trailer = data.videos.mediabook && (data.videos.mediabook.files['720p'] || data.videos.mediabook.files['320p']);
|
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,
|
2019-11-09 00:22:50 +00:00
|
|
|
tags,
|
|
|
|
poster,
|
|
|
|
photos,
|
2019-11-27 03:58:38 +00:00
|
|
|
trailer: trailer && {
|
|
|
|
src: trailer.urls.view,
|
|
|
|
quality: parseInt(trailer.format, 10),
|
2019-11-09 00:22:50 +00:00
|
|
|
},
|
2019-11-27 03:58:38 +00:00
|
|
|
date,
|
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) {
|
2019-11-27 03:58:38 +00:00
|
|
|
const { id: entryId, title, description } = data;
|
2019-04-07 23:49:45 +00:00
|
|
|
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 rawTags = data.tags.map(tag => tag.name);
|
|
|
|
|
2019-11-16 23:45:31 +00:00
|
|
|
const [poster, ...photos] = getThumbs(data);
|
2019-11-27 03:58:38 +00:00
|
|
|
const trailer = data.videos.mediabook && (data.videos.mediabook.files['720p'] || data.videos.mediabook.files['320p']);
|
|
|
|
|
|
|
|
const siteName = data.collections[0].name;
|
|
|
|
const siteId = data.collections[0].id;
|
|
|
|
const siteSlug = siteName.replace(/\s+/g, '').toLowerCase();
|
|
|
|
const siteUrl = `https://www.realitykings.com/scenes?site=${siteId}`;
|
|
|
|
|
|
|
|
const [[channelSite], tags] = await Promise.all([
|
|
|
|
site.isFallback
|
|
|
|
? fetchSites({
|
|
|
|
slug: siteSlug,
|
|
|
|
name: siteName,
|
|
|
|
url: siteUrl,
|
|
|
|
})
|
|
|
|
: [site],
|
|
|
|
matchTags(rawTags),
|
|
|
|
]);
|
2019-11-09 00:22:50 +00:00
|
|
|
|
2019-04-07 23:49:45 +00:00
|
|
|
return {
|
|
|
|
url,
|
|
|
|
entryId,
|
|
|
|
title,
|
|
|
|
description,
|
|
|
|
actors,
|
|
|
|
tags,
|
2019-11-09 00:22:50 +00:00
|
|
|
poster,
|
|
|
|
photos,
|
2019-11-27 03:58:38 +00:00
|
|
|
trailer: trailer && {
|
|
|
|
src: trailer.urls.view,
|
|
|
|
quality: parseInt(trailer.format, 10),
|
2019-11-09 00:22:50 +00:00
|
|
|
},
|
2019-11-27 03:58:38 +00:00
|
|
|
date,
|
|
|
|
site: channelSite,
|
2019-04-07 23:49:45 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-11-27 03:58:38 +00:00
|
|
|
function getUrl(site) {
|
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+/)) {
|
2019-11-27 03:58:38 +00:00
|
|
|
return site.url;
|
2019-11-09 00:22:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (site.parameters && site.parameters.siteId) {
|
2019-11-27 03:58:38 +00:00
|
|
|
return `https://www.realitykings.com/scenes?site=${site.parameters.siteId}`;
|
2019-11-09 00:22:50 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2019-11-27 03:58:38 +00:00
|
|
|
async function fetchLatest(site, page = 1) {
|
|
|
|
const url = getUrl(site);
|
|
|
|
const { search } = new URL(url);
|
|
|
|
const siteId = new URLSearchParams(search).get('site');
|
|
|
|
|
|
|
|
const cookieJar = new CookieJar();
|
|
|
|
const session = bhttp.session({ cookieJar });
|
|
|
|
|
|
|
|
await session.get(url);
|
|
|
|
|
|
|
|
const cookieString = await cookieJar.getCookieStringAsync(url);
|
|
|
|
const { instance_token: instanceToken } = cookieToData(cookieString);
|
|
|
|
|
|
|
|
const beforeDate = moment().add('1', 'day').format('YYYY-MM-DD');
|
|
|
|
const limit = 10;
|
|
|
|
const res = await session.get(`https://site-api.project1service.com/v2/releases?collectionId=${siteId}&dateReleased=<${beforeDate}&limit=${limit}&offset=${limit * (page - 1)}&orderBy=-dateReleased&type=scene`, {
|
|
|
|
headers: {
|
|
|
|
Instance: instanceToken,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return scrapeLatest(res.body.result, site);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
2019-11-27 03:58:38 +00:00
|
|
|
const cookieString = await cookieJar.getCookieStringAsync(url);
|
|
|
|
const { instance_token: instanceToken } = cookieToData(cookieString);
|
2019-04-07 23:49:45 +00:00
|
|
|
|
|
|
|
const res = await session.get(`https://site-api.project1service.com/v2/releases/${entryId}`, {
|
|
|
|
headers: {
|
|
|
|
Instance: instanceToken,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2019-11-27 03:58:38 +00:00
|
|
|
return scrapeScene(res.body.result, url, site);
|
2019-04-07 23:49:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
fetchLatest,
|
|
|
|
fetchScene,
|
|
|
|
};
|