'use strict'; const bhttp = require('bhttp'); const { JSDOM } = require('jsdom'); const cheerio = require('cheerio'); const moment = require('moment'); const slugify = require('../utils/slugify'); function extractTitle(originalTitle) { const titleComponents = originalTitle.split(' '); const sceneIdMatch = titleComponents.slice(-1)[0].match(/(AB|AF|GP|SZ|IV|GIO|RS|TW|MA|FM|SAL|NR|AA|GL|BZ|FS|KS|OTS|NF)\d+/); // detect studio prefixes const shootId = sceneIdMatch ? sceneIdMatch[0] : null; const title = sceneIdMatch ? titleComponents.slice(0, -1).join(' ') : originalTitle; return { shootId, title }; } function getPoster(posterElement, sceneId) { const posterStyle = posterElement.attr('style'); if (posterStyle) { return posterStyle.slice(posterStyle.indexOf('(') + 1, -1); } const posterRange = posterElement.attr('data-casting'); const posterRangeData = posterRange ? JSON.parse(posterRange) : null; const posterTimeRange = posterRangeData[Math.floor(Math.random() * posterRangeData.length)]; if (!posterTimeRange) { return null; } if (typeof posterTimeRange === 'number') { // poster time is already a single time value return `https://legalporno.com/casting/${sceneId}/${posterTimeRange}`; } const [max, min] = posterTimeRange.split('-'); const posterTime = Math.floor(Math.random() * (Number(max) - Number(min) + 1) + Number(min)); return `https://legalporno.com/casting/${sceneId}/${posterTime}`; } 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 entryId = new URL(url).pathname.split('/')[2]; const date = moment.utc($(element).attr('release'), 'YYYY/MM/DD').toDate(); const sceneId = $(element).attr('data-content'); const posterElement = $(element).find('.thumbnail-avatar'); const poster = getPoster(posterElement, sceneId); return { url, shootId, entryId, title, date, poster, site, }; }); } async function scrapeScene(html, url, site, useGallery) { const $ = cheerio.load(html, { normalizeWhitespace: true }); const playerObject = $('script:contains("new WatchPage")').html(); const playerData = playerObject && playerObject.slice(playerObject.indexOf('{"swf":'), playerObject.lastIndexOf('},') + 1); const data = playerData && JSON.parse(playerData); const release = { url }; const originalTitle = $('h1.watchpage-title').text().trim(); const { shootId, title } = extractTitle(originalTitle); release.shootId = shootId; release.entryId = new URL(url).pathname.split('/')[2]; release.title = title; release.date = moment.utc($('span[title="Release date"] a').text(), 'YYYY-MM-DD').toDate(); const [actorsElement, tagsElement, descriptionElement] = $('.scene-description__row').toArray(); release.description = $('meta[name="description"]')?.attr('content')?.trim() || (descriptionElement && $(descriptionElement).find('dd').text().trim()); release.actors = $(actorsElement) .find('a[href*="com/model"]') .map((actorIndex, actorElement) => $(actorElement).text()).toArray(); release.duration = moment.duration($('span[title="Runtime"]').text().trim()).asSeconds(); release.tags = $(tagsElement).find('a').map((tagIndex, tagElement) => $(tagElement).text()).toArray(); const photos = useGallery ? $('.gallery a img').map((photoIndex, photoElement) => $(photoElement).attr('src')).toArray() : $('.screenshots img').map((photoIndex, photoElement) => $(photoElement).attr('src')).toArray(); release.photos = photos.map((source) => { // source without parameters sometimes serves larger preview photo const { origin, pathname } = new URL(source); return `${origin}${pathname}`; /* disable thumbnail as fallback, usually enough high res photos available return [ `${origin}${pathname}`, source, ]; */ }); const posterStyle = $('#player').attr('style'); const poster = posterStyle.slice(posterStyle.indexOf('(') + 1, -1); release.poster = poster || release.photos.slice(Math.floor(release.photos.length / 3) * -1); // poster unavailable, try last 1/3rd of high res photos as fallback if (data) { const qualityMap = { web: 240, vga: 480, hd: 720, '1080p': 1080, }; release.trailer = data.clip.qualities.map(trailer => ({ src: trailer.src, type: trailer.type, quality: qualityMap[trailer.quality] || trailer.quality, })); } const studioName = $('.watchpage-studioname').first().text().trim(); release.studio = slugify(studioName, ''); return release; } async function scrapeProfile(html, _url, actorName) { const { document } = new JSDOM(html).window; const profile = { name: actorName, }; const avatarEl = document.querySelector('.model--avatar img[src^="http"]'); const entries = Array.from(document.querySelectorAll('.model--description tr'), el => el.textContent.replace(/\n/g, '').split(':')); const bio = entries .filter(entry => entry.length === 2) // ignore entries without ':' (About section, see Blanche Bradburry) .reduce((acc, [key, value]) => ({ ...acc, [key.trim()]: value.trim() }), {}); profile.birthPlace = bio.Nationality; if (bio.Age) profile.age = bio.Age; if (avatarEl) profile.avatar = avatarEl.src; return profile; } 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 useGallery = true; // TODO: fall back on screenshots when gallery is not available const res = useGallery ? await bhttp.get(`${url}/gallery#gallery`) : await bhttp.get(`${url}/screenshots#screenshots`); return scrapeScene(res.body.toString(), url, site, useGallery); } async function fetchProfile(actorName) { const res = await bhttp.get(`https://www.legalporno.com/api/autocomplete/search?q=${actorName.replace(' ', '+')}`); const data = res.body; const result = data.terms.find(item => item.type === 'model'); if (result) { const bioRes = await bhttp.get(result.url); const html = bioRes.body.toString(); return scrapeProfile(html, result.url, actorName); } return null; } module.exports = { fetchLatest, fetchProfile, fetchScene, };