'use strict'; /* eslint-disable newline-per-chained-call */ const bhttp = require('bhttp'); const cheerio = require('cheerio'); const moment = require('moment'); const { matchTags } = require('../tags'); function scrapeLatest(html, site) { const $ = cheerio.load(html, { normalizeWhitespace: true }); const stateObject = $('script:contains("INITIAL_STATE")'); const { videos: scenes } = JSON.parse(stateObject.html().trim().slice(27, -1)); return scenes.map((scene) => { const shootId = String(scene.newId); const { title } = scene; const url = `${site.url}${scene.targetUrl}`; const date = moment.utc(scene.releaseDateFormatted, 'MMMM DD, YYYY').toDate(); const actors = scene.models; const stars = Number(scene.textRating) / 2; return { url, shootId, title, actors, date, rating: { stars, }, site, }; }); } async function scrapeScene(html, url, site) { const $ = cheerio.load(html, { normalizeWhitespace: true }); const { pathname, search } = new URL(url); const stateObject = $('script:contains("INITIAL_STATE")'); const data = JSON.parse(stateObject.html().trim().slice(27, -1)); const scene = data.page.data[`${pathname}${search}`].data.video; const shootId = String(scene.newId); const { title } = scene; const date = new Date(scene.releaseDate); const actors = scene.models; const stars = scene.totalRateVal; const rawTags = scene.tags; const tags = await matchTags(rawTags); const duration = scene.runLength; const director = scene.directorNames; return { url, shootId, title, actors, director, date, duration, tags, rating: { stars, }, site, }; } async function fetchLatest(site, page = 1) { const res = await bhttp.get(`${site.url}/videos?page=${page}&size=7`); return scrapeLatest(res.body.toString(), site); } async function fetchScene(url, site) { const res = await bhttp.get(url); return scrapeScene(res.body.toString(), url, site); } module.exports = { fetchLatest, fetchScene, };