'use strict'; const bhttp = require('bhttp'); const cheerio = require('cheerio'); const moment = require('moment'); const tagMap = { Anal: 'anal', 'Ass Licking': 'ass licking', 'Ass To Mouth': 'ATM', 'Big Ass': 'big butt', 'Big Tits': 'big boobs', Black: 'big black cock', Blonde: 'blonde', Blowjob: 'blowjob', 'Blowjob (double)': 'double blowjob', Brunette: 'brunette', 'Cum Swallowing': 'swallowing', Cumshot: 'cumshot', Deepthroat: 'deepthroat', 'Double Penetration (DP)': 'DP', Ebony: 'ebony', Facial: 'facial', Gangbang: 'gangbang', Gonzo: 'gonzo', Hardcore: 'hardcore', Interracial: 'interracial', Latina: 'latina', Petite: 'petite', 'Pussy Licking': 'pussy licking', Rimjob: 'ass licking', 'Rough Sex': 'rough', 'Small Tits': 'small boobs', Threesome: 'threesome', }; function scrape(html, site) { const $ = cheerio.load(html, { normalizeWhitespace: true }); const scenesElements = $('.sceneInfo').toArray(); return scenesElements.map((element) => { const sceneLinkElement = $(element).find('.sceneTitle a'); const url = `${site.url}${sceneLinkElement.attr('href')}`; const title = sceneLinkElement.attr('title'); const date = moment .utc($(element).find('.sceneDate').text(), 'MM-DD-YYYY') .toDate(); const actors = $(element).find('.sceneActors a') .map((actorIndex, actorElement) => $(actorElement).attr('title')) .toArray(); const [likes, dislikes] = $(element).find('.value') .toArray() .map(value => Number($(value).text())); return { url, title, actors, date, rating: { likes, dislikes, }, site, }; }); } function scrapeSceneFallback($, url, site) { const title = $('h1.title').text(); const date = moment.utc($('.updatedDate').text(), 'MM-DD-YYYY').toDate(); const actors = $('.sceneColActors a').map((actorIndex, actorElement) => $(actorElement).text()).toArray(); const description = ($('.sceneDesc').text() || '').replace(/Video Description:/g, ' ').trim(); const stars = $('.currentRating').text().split('/')[0] / 2; const rawTags = $('.sceneColCategories > a').map((tagIndex, tagElement) => $(tagElement).text()).toArray(); const tags = rawTags.reduce((accTags, tag) => (tagMap[tag] ? [...accTags, tagMap[tag]] : accTags), []); return { url, title, date, actors, description, tags, rating: { stars, }, site, }; } function scrapeScene(html, url, site) { const $ = cheerio.load(html, { normalizeWhitespace: true }); const json = $('script[type="application/ld+json"]').html(); if (!json) { return scrapeSceneFallback($, url, site); } const data = JSON.parse(json)[0]; const title = data.isPartOf.name; const date = moment.utc(data.dateCreated, 'YYYY-MM-DD').toDate(); const actors = data.actor .sort(({ genderA }, { genderB }) => { if (genderA === 'female' && genderB === 'male') return 1; if (genderA === 'male' && genderB === 'female') return -1; return 0; }) .map(actor => actor.name); const description = data.description || undefined; const stars = (data.aggregateRating.ratingValue / data.aggregateRating.bestRating) * 5; const duration = moment.duration(data.duration.slice(2).split(':')).asSeconds(); const rawTags = data.keywords.split(', '); const tags = rawTags.reduce((accTags, tag) => (tagMap[tag] ? [...accTags, tagMap[tag]] : accTags), []); return { url, title, date, actors, description, duration, tags, rating: { stars, }, site, }; } async function fetchLatest(site) { const res = await bhttp.get(`${site.url}/en/videos`); return scrape(res.body.toString(), site); } async function fetchUpcoming(site) { const res = await bhttp.get(`${site.url}/en/videos/AllCategories/0/1/upcoming`); return scrape(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, fetchUpcoming, fetchScene, };