'use strict'; const bhttp = require('bhttp'); const cheerio = require('cheerio'); const moment = require('moment'); const { matchTags } = require('../tags'); function extractTitle(originalTitle) { const titleComponents = originalTitle.split(' '); const sceneIdMatch = titleComponents.slice(-1)[0].match(/(GP|SZ|IV|GIO|RS|TW|MA|FM|SAL|NR|AA|GL|BZ|FS)\d+/); // detect studio prefixes const shootId = sceneIdMatch ? sceneIdMatch[0] : null; const title = sceneIdMatch ? titleComponents.slice(0, -1).join(' ') : originalTitle; return { shootId, title }; } function scrapeLatest(html, site) { const $ = cheerio.load(html, { normalizeWhitespace: true }); const scenesElements = $('.thumbnails > div').toArray(); return scenesElements.map((element) => { const sceneLinkElement = $(element).find('.thumbnail-title a'); const url = sceneLinkElement.attr('href'); const originalTitle = sceneLinkElement.text().trim(); // title attribute breaks when they use \\ escaping const { shootId, title } = extractTitle(originalTitle); const internalId = new URL(url).pathname.split('/')[2]; const date = moment.utc($(element).attr('release'), 'YYYY/MM/DD').toDate(); return { url, shootId: shootId || internalId, title, date, site, }; }); } async function scrapeScene(html, url, site) { const $ = cheerio.load(html, { normalizeWhitespace: true }); const originalTitle = $('h1.watchpage-title').text().trim(); const { shootId, title } = extractTitle(originalTitle); const date = moment.utc($('span[title="Release date"] a').text(), 'YYYY-MM-DD').toDate(); const [actorsElement, tagsElement] = $('.scene-description__row').toArray(); const actors = $(actorsElement) .find('a[href*="com/model"]') .map((actorIndex, actorElement) => $(actorElement).text()).toArray(); const duration = moment.duration($('span[title="Runtime"]').text().trim()).asSeconds(); const rawTags = $(tagsElement).find('a').map((tagIndex, tagElement) => $(tagElement).text()).toArray(); const tags = await matchTags(rawTags); return { url, shootId, title, date, actors, duration, tags, site, }; } async function fetchLatest(site, page = 1) { const res = await bhttp.get(`${site.url}/new-videos/${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, };