'use strict'; /* eslint-disable newline-per-chained-call */ const bhttp = require('bhttp'); const cheerio = require('cheerio'); const moment = require('moment'); function scrapeLatest(html, site) { const $ = cheerio.load(html, { normalizeWhitespace: true }); const stateScript = $('script:contains("INITIAL_STATE")').html(); const { videos: scenes } = JSON.parse(stateScript.slice(stateScript.indexOf('{'), stateScript.indexOf('};') + 1)); return scenes.map((scene) => { const entryId = String(scene.newId); const { title, models: actors, } = scene; const url = `${site.url}${scene.targetUrl}`; const date = moment.utc(scene.releaseDateFormatted, 'MMMM DD, YYYY').toDate(); const stars = Number(scene.textRating) / 2; // largest thumbnail. poster is the same image but bigger, too large for storage space efficiency const poster = scene.images.listing.slice(-1)[0].src; const teaser = scene.previews.listing.slice(-1)[0]; return { url, entryId, title, actors, date, poster, teaser: { src: teaser.src, type: teaser.type, quality: teaser.height, }, rating: { stars, }, site, }; }); } function scrapeUpcoming(html, site) { const statePrefix = html.indexOf('__INITIAL_STATE__'); const stateString = html.slice(html.indexOf('{', statePrefix), html.indexOf('};', statePrefix) + 1); const data = JSON.parse(stateString); const scene = data.page.data['/'].data?.nextScene; if (!scene) return null; const release = {}; release.title = scene.targetUrl .slice(1) .split('-') .map(component => `${component.charAt(0).toUpperCase()}${component.slice(1)}`) .join(' '); release.date = moment.utc(scene.releaseDate).toDate(); release.url = `${site.url}${scene.targetUrl}`; release.actors = scene.models; release.poster = scene.images.poster .filter(image => /landscape/i.test(image.name)) .sort((imageA, imageB) => imageB.height - imageA.height) .map((image) => { const sources = [image.src, image.highdpi?.['2x'], image.highdpi?.['3x']]; // high DPI images for full HD source are huge, only prefer for smaller fallback sources return image.height === 1080 ? sources : sources.reverse(); }) .flat(); release.teaser = scene.previews.poster .filter(teaser => /landscape/i.test(teaser.name)) .map(teaser => ({ src: teaser.src, type: teaser.type, quality: Number(String(teaser.height).replace('353', '360')), })); release.entryId = (release.poster[0] || release.teaser[0])?.match(/\/(\d+)/)?.[1]; return [release]; } async function scrapeScene(html, url, site) { const $ = cheerio.load(html, { normalizeWhitespace: true }); const stateObject = $('script:contains("INITIAL_STATE")'); const data = JSON.parse(stateObject.html().trim().slice(27, -1)); const pageData = data.page.data[data.location.pathname].data; const entryId = pageData.video; const scene = data.videos.find(video => video.newId === entryId); console.log(scene, data, pageData); const poster = scene.rotatingThumbsUrlSizes[0]['1040w']; const photos = pageData.pictureset.map(photo => photo.main[0].src); const trailer = scene.previews.listing.find(preview => preview.height === 353) || null; const { title, description, models: actors, totalRateVal: stars, runLength: duration, directorNames: director, tags, } = scene; const date = new Date(scene.releaseDate); return { url, entryId, title, description, actors, director, date, duration, tags, photos, poster, trailer: trailer && { src: trailer.src, type: trailer.type, quality: 353, }, rating: { stars, }, site, }; } async function fetchLatest(site, page = 1) { const url = `${site.url}/videos?page=${page}&size=7`; const res = await bhttp.get(url); if (res.statusCode === 200) { return scrapeLatest(res.body.toString(), site); } return res.statusCode; } async function fetchUpcoming(site) { const res = await bhttp.get(site.url); if (res.statusCode === 200) { return scrapeUpcoming(res.body.toString(), site); } return res.statusCode; } async function fetchScene(url, site) { const res = await bhttp.get(url); if (res.statusCode === 200) { return scrapeScene(res.body.toString(), url, site); } throw new Error(`Vixen response not OK for scene (${url}): ${res.statusCode}`); } module.exports = { fetchLatest, fetchUpcoming, fetchScene, };