'use strict'; const bhttp = require('bhttp'); const cheerio = require('cheerio'); const moment = require('moment'); const tagMap = { Anal: 'anal', Asian: 'asian', 'Ass To Mouth': 'ATM', 'Big Cocks': 'big cock', Black: 'BBC', Blondes: 'blonde', Brunettes: 'brunette', Blowjobs: 'blowjob', Creampie: 'creampie', 'Deep Throat': 'deepthroat', Facial: 'facial', Interracial: 'interracial', Lingerie: 'lingerie', Natural: 'natural', 'Red Head': 'readhead', 'School Girl': 'schoolgirl', Tattoo: 'tattoo', Teen: 'teen', }; function scrapeLatest(html, site) { const $ = cheerio.load(html, { normalizeWhitespace: true }); const scenesElements = $('.update_details').toArray(); return scenesElements.map((element) => { const sceneLinkElement = $(element).children('a').eq(1); const url = sceneLinkElement.attr('href'); const title = sceneLinkElement.text(); const date = moment .utc($(element).find('.update_date').text(), 'MM/DD/YYYY') .toDate(); const actors = $(element).find('.update_models a') .map((actorIndex, actorElement) => $(actorElement).text()) .toArray(); return { url, title, actors, date, site, }; }); } function scrapeUpcoming(html, site) { const $ = cheerio.load(html, { normalizeWhitespace: true }); const scenesElements = $('#coming_soon_carousel').find('.table').toArray(); return scenesElements.map((element) => { const details = $(element).find('.update_details_comingsoon') .eq(1) .children() .remove(); const title = details .end() .text() .trim(); const actors = details .text() .trim() .split(', '); const date = moment .utc($(element).find('.update_date_comingsoon').text().slice(7), 'MM/DD/YYYY') .toDate(); return { url: null, title, actors, date, rating: null, site, }; }); } function scrapeScene(html, url, site) { const $ = cheerio.load(html, { normalizeWhitespace: true }); const title = $('.title_bar_hilite').text(); const date = moment .utc($('.update_date').text(), 'MM/DD/YYYY') .toDate(); const actors = $('.update_description + .update_models a') .map((_actorIndex, actorElement) => $(actorElement).text()) .toArray(); const description = $('.update_description').text().trim(); const stars = Number($('.avg_rating').text().trim().replace(/[\s|Avg Rating:]/g, '')); const rawTags = $('.update_tags 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, rating: { stars, }, tags, site, }; } async function fetchLatest(site) { const res = await bhttp.get(`${site.url}/trial/categories/movies_1_d.html`); return scrapeLatest(res.body.toString(), site); } async function fetchUpcoming(site) { const res = await bhttp.get(`${site.url}/trial/index.php`); return scrapeUpcoming(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, };