'use strict'; /* eslint-disable newline-per-chained-call */ const bhttp = require('bhttp'); const cheerio = require('cheerio'); const moment = require('moment'); const knex = require('../knex'); const { matchTags } = require('../tags'); async function getPhotos(shootId) { const res = await bhttp.get(`https://www.private.com/gallery.php?type=highres&id=${shootId}`); const html = res.body.toString(); const $ = cheerio.load(html, { normalizeWhitespace: true }); const photos = $('a.fakethumb').map((photoIndex, photoElement) => $(photoElement).attr('data-src') || $(photoElement).attr('href')).toArray(); return photos; } function scrapeLatest(html, site) { const $ = cheerio.load(html, { normalizeWhitespace: true }); const sceneElements = $('.content-wrapper.container .scene').toArray(); return sceneElements.map((element) => { const sceneLinkElement = $(element).find('a[data-track="TITLE_LINK"]'); const url = sceneLinkElement.attr('href'); const title = sceneLinkElement.text(); const shootId = url.split('/').slice(-1)[0]; const thumbnailElement = $(element).find('img.thumbs_onhover'); const photoCount = Number(thumbnailElement.attr('thumbs_num')); const poster = thumbnailElement.attr('src'); const photos = Array.from({ length: photoCount }, (val, index) => thumbnailElement.attr(`src${index + 1}`)); const date = moment.utc($(element).find('.scene-date'), 'MM/DD/YYYY').toDate(); const actors = $(element).find('a[data-track="PORNSTAR_LINK"]').map((actorIndex, actorElement) => $(actorElement).text()).toArray(); const likes = Number($(element).find('.scene-votes').text()); return { url, shootId, title, actors, date, poster, photos, rating: { likes, }, site, }; }); } async function scrapeScene(html, url, site) { const $ = cheerio.load(html, { normalizeWhitespace: true }); const shootId = url.split('/').slice(-1)[0]; const title = $('.video-wrapper meta[itemprop="name"]').attr('content'); const date = moment.utc($('.video-wrapper meta[itemprop="uploadDate"]').attr('content'), 'MM/DD/YYYY').toDate(); const actors = $('.content-wrapper .scene-models-list a').map((actorIndex, actorElement) => $(actorElement).text()).toArray(); const description = $('.video-wrapper meta[itemprop="description"]').attr('content'); const [minutes, seconds] = $('.video-wrapper meta[itemprop="duration"]').attr('content').match(/\d+/g); const duration = Number(minutes) * 60 + Number(seconds); const poster = $('meta[property="og:image"]').attr('content'); const trailer = $('meta[property="og:video"]').attr('content'); const photos = await getPhotos(shootId); const likes = Number($('.content-desc #social-actions #likes').text()); const siteElement = $('.content-wrapper .logos-sites a'); const siteUrl = siteElement.attr('href').slice(0, -1); const siteName = siteElement.text(); const rawTags = $('.content-desc .scene-tags a').map((tagIndex, tagElement) => $(tagElement).text()).toArray(); const [tags, channelSite] = await Promise.all([ matchTags(rawTags), knex('sites') .where({ url: siteUrl }) .orWhere({ name: siteName }) .first(), ]); return { url, shootId, title, date, actors, description, duration, tags, poster, photos, trailer: { src: trailer, }, rating: { likes, }, site: channelSite || site, }; } async function fetchLatest(site, page = 1) { const res = await bhttp.get(`${site.url}/${page}/`); 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, };