'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(); console.log(sceneElements); 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(); console.log(date, actors, title); 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; console.log(data); 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, }, }); return scrapeScene(res.body.result.parent || res.body.result, url, site); } module.exports = { fetchLatest, fetchScene, };