'use strict'; const util = require('util'); const Promise = require('bluebird'); const bhttp = require('bhttp'); const cheerio = require('cheerio'); const { JSDOM } = require('jsdom'); const moment = require('moment'); const logger = require('../logger')(__filename); const { get, geta, ctxa } = require('../utils/q'); const { heightToCm } = require('../utils/convert'); const slugify = require('../utils/slugify'); async function fetchPhotos(url) { const res = await bhttp.get(url); return res.body.toString(); } function scrapePhotos(html, type) { const $ = cheerio.load(html, { normalizeWhitespace: true }); const photos = $('.photo_gallery_thumbnail_wrapper .thumbs') .toArray() .map((photoElement) => { const src = $(photoElement).attr('src'); // high res often available in alternative directories, but not always, provide original as fallback if (type === 'caps') { return [ src.replace('capthumbs/', 'caps/'), src, ]; } return [ src.replace('thumbs/', 'photos/'), src.replace('thumbs/', '1600watermarked/'), src.replace('thumbs/', '1280watermarked/'), src.replace('thumbs/', '1024watermarked/'), src, ]; }); return photos; } async function getPhotosLegacy(entryId, site, type = 'highres', page = 1) { const albumUrl = `${site.url}/trial/gallery.php?id=${entryId}&type=${type}&page=${page}`; logger.warn(`Jules Jordan is using legacy photo scraper for ${albumUrl} (page ${page})`); const html = await fetchPhotos(albumUrl); const $ = cheerio.load(html, { normalizeWhitespace: true }); // don't add first URL to pages to prevent unnecessary duplicate request const photos = scrapePhotos(html, type); const pages = Array.from(new Set($('.page_numbers a').toArray().map(el => $(el).attr('href')))); const otherPhotos = pages ? await Promise.map(pages, async (pageX) => { const pageUrl = `https://www.julesjordan.com/trial/${pageX}`; const pageHtml = await fetchPhotos(pageUrl); return scrapePhotos(pageHtml, type); }, { concurrency: 2, }) : []; const allPhotos = photos.concat(otherPhotos.flat()); if (allPhotos.length === 0 && type === 'highres') { // photos not available, try for screencaps instead return getPhotosLegacy(entryId, site, 'caps', 1); } return allPhotos; } async function getPhotos(entryId, site, type = 'highres', page = 1) { const albumUrl = `${site.parameters?.photos || `${site.url}/gallery.php`}?id=${entryId}&type=${type}&page=${page}`; const res = await bhttp.get(albumUrl); const html = res.body.toString(); const sourceLines = html.split(/\n/).filter(line => line.match(/ptx\["\w+"\]/)); const sources = sourceLines.reduce((acc, sourceLine) => { const quality = sourceLine.match(/\["\w+"\]/)[0].slice(2, -2); const sourceStart = sourceLine.match(/\/trial|\/tour|\/content/); if (!sourceStart) return acc; const source = sourceLine.slice(sourceStart.index, sourceLine.indexOf('.jpg') + 4); if (!source) return acc; if (!acc[quality]) acc[quality] = []; acc[quality].push(`${site.url}${source}`); return acc; }, {}); if (type === 'highres') { if (sources['1600'] && sources['1600'].length > 0) return sources['1600']; if (sources['1280'] && sources['1280'].length > 0) return sources['1280']; if (sources['1024'] && sources['1024'].length > 0) return sources['1024']; if (sources.Thumbs && sources.Thumbs.length > 0) return sources.Thumbs; // no photos available, try for screencaps instead return getPhotos(entryId, site, 'caps', 1); } if (sources.jpg && sources.jpg.length > 0) return sources.jpg; if (sources['Video Cap Thumbs'] && sources['Video Cap Thumbs'].length > 0) return sources['Video Cap Thumbs']; // no screencaps available either, try legacy scraper just in case return getPhotosLegacy(entryId, site, 'highres', 1); } function getEntryId(html) { const entryId = html.match(/showtagform\((\d+)\)/); if (entryId) { return entryId[1]; } const setIdIndex = html.indexOf('setid:"'); if (setIdIndex) { return html.slice(setIdIndex, html.indexOf(',', setIdIndex)).match(/\d+/)[0]; } return null; } function scrapeAll(scenes, site) { return scenes.map(({ qu }) => { const release = {}; release.entryId = qu.el.dataset.setid || qu.q('.rating_box')?.dataset.id; release.url = qu.url('.update_title, .dvd_info > a, a ~ a'); release.title = qu.q('.update_title, .dvd_info > a, a ~ a', true); release.date = qu.date('.update_date', 'MM/DD/YYYY'); release.actors = qu.all('.update_models a', true); const dvdPhotos = qu.imgs('.dvd_preview_thumb'); const photoCount = Number(qu.q('a img.thumbs', 'cnt')) || 1; [release.poster, ...release.photos] = dvdPhotos.length ? dvdPhotos : Array.from({ length: photoCount }).map((value, index) => { const src = qu.img('a img.thumbs', `src${index}_1x`) || qu.img('a img.thumbs', `src${index}`) || qu.img('a img.thumbs'); return src ? { src: /^http/.test(src) ? src : `${site.url}${src}`, referer: site.url, } : null; }).filter(Boolean); const teaserScript = qu.content('script'); if (teaserScript) { const src = teaserScript.slice(teaserScript.indexOf('http'), teaserScript.indexOf('.mp4') + 4); if (src) release.teaser = { src }; } return release; }); } function scrapeUpcoming(html, site) { const $ = cheerio.load(html, { normalizeWhitespace: true }); const scenesElements = $('#coming_soon_carousel').find('.table').toArray(); return scenesElements.map((element) => { const entryId = $(element).find('.upcoming_updates_thumb').attr('id').match(/\d+/)[0]; 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(); const photoElement = $(element).find('a img.thumbs'); const posterPath = photoElement.attr('src'); const poster = posterPath.match(/^http/) ? posterPath : `${site.url}${posterPath}`; const videoClass = $(element).find('.update_thumbnail div').attr('class'); const videoScript = $(element).find(`script:contains(${videoClass})`).html(); const teaser = videoScript.slice(videoScript.indexOf('https://'), videoScript.indexOf('.mp4') + 4); return { url: null, entryId, title, date, actors, poster, teaser: { src: teaser, }, rating: null, site, }; }); } async function scrapeScene({ qu }, url, site) { const release = { url, site }; release.entryId = getEntryId(qu.html); release.title = qu.q('.title_bar_hilite', true); release.description = qu.q('.update_description', true); release.date = qu.date('.update_date', 'MM/DD/YYYY', null, 'innerHTML'); release.actors = qu.all('.backgroundcolor_info > .update_models a, .item .update_models a', true); const posterPath = qu.html.match(/useimage = "(.*)"/)?.[1]; if (posterPath) { const poster = /^http/.test(posterPath) ? posterPath : `${site.url}${posterPath}`; if (poster) { release.poster = { src: poster, referer: site.url, }; } } if (site.slug !== 'manuelferrara') { const trailerLines = qu.html.split('\n').filter(line => /movie\["trailer\w*"\]\[/i.test(line)); if (trailerLines.length) { release.trailer = trailerLines.map((trailerLine) => { const src = trailerLine.match(/path:"([\w:/.&=?%]+)"/)?.[1]; const quality = trailerLine.match(/movie_height:'(\d+)/)?.[1]; return src && { src: /^http/.test(src) ? src : `${site.url}${src}`, quality: quality && Number(quality.replace('558', '540')), }; }).filter(Boolean); } } release.photos = await getPhotos(release.entryId, site); release.tags = qu.all('.update_tags a', true); if (qu.exists('.update_dvds a')) { release.movie = { url: qu.url('.update_dvds a'), title: qu.q('.update_dvds a', true), }; } const stars = Number(qu.q('.avg_rating', true)?.replace(/[\s|Avg Rating:]/g, '')); if (stars) release.stars = stars; return release; } function scrapeMovie({ el, q, qus }, url, site) { const movie = { url, site }; movie.entryId = q('.dvd_details_overview .rating_box').dataset.id; movie.title = q('.title_bar span', true); movie.covers = qus('#dvd-cover-flip > a'); movie.channel = q('.update_date a', true); // movie.releases = Array.from(document.querySelectorAll('.cell.dvd_info > a'), el => el.href); const sceneQs = ctxa(el, '.dvd_details'); const scenes = scrapeAll(sceneQs, site); const curatedScenes = scenes .map(scene => ({ ...scene, movie })) .sort((sceneA, sceneB) => sceneA.date - sceneB.date); movie.date = curatedScenes[0].date; return { ...movie, scenes: curatedScenes, }; } function scrapeProfile(html, url, actorName) { const { document } = new JSDOM(html).window; const bio = document.querySelector('.model_bio').textContent; const avatarEl = document.querySelector('.model_bio_pic'); const profile = { name: actorName, }; const heightString = bio.match(/\d+ feet \d+ inches/); const ageString = bio.match(/Age:\s*\d{2}/); const measurementsString = bio.match(/\w+-\d+-\d+/); if (heightString) profile.height = heightToCm(heightString[0]); if (ageString) profile.age = Number(ageString[0].match(/\d{2}/)[0]); if (measurementsString) [profile.bust, profile.waist, profile.hip] = measurementsString[0].split('-'); if (avatarEl) { const avatarSources = [ avatarEl.innerHTML.slice(avatarEl.innerHTML.indexOf('src0_3x') + 9, avatarEl.innerHTML.indexOf('3x.jpg') + 6).trim(), avatarEl.innerHTML.slice(avatarEl.innerHTML.indexOf('src0_2x') + 9, avatarEl.innerHTML.indexOf('2x.jpg') + 6).trim(), avatarEl.innerHTML.slice(avatarEl.innerHTML.indexOf('src0_1x') + 9, avatarEl.innerHTML.indexOf('1x.jpg') + 6).trim(), avatarEl.innerHTML.slice(avatarEl.innerHTML.indexOf('src0') + 6, avatarEl.innerHTML.indexOf('set.jpg') + 7).trim(), avatarEl.innerHTML.slice(avatarEl.innerHTML.indexOf('src') + 5, avatarEl.innerHTML.indexOf('set.jpg') + 7).trim(), ].filter(Boolean); if (avatarSources.length) profile.avatar = avatarSources; } profile.releases = Array.from(document.querySelectorAll('.category_listing_block .update_details > a:first-child'), el => el.href); return profile; } async function fetchLatest(site, page = 1) { const url = site.parameters?.latest ? util.format(site.parameters.latest, page) : `${site.url}/trial/categories/movies_${page}_d.html`; // const res = await bhttp.get(url); const res = await geta(url, '.update_details'); return res.ok ? scrapeAll(res.items, site) : res.status; } async function fetchUpcoming(site) { if (site.parameters?.upcoming === false) return null; const url = site.parameters?.upcoming ? util.format(site.parameters.upcoming) : `${site.url}/trial/index.php`; const res = await bhttp.get(url); if (res.statusCode === 200) { return scrapeUpcoming(res.body.toString(), site); } return res.statusCode; } async function fetchScene(url, site) { const res = await get(url); return res.ok ? scrapeScene(res.item, url, site) : res.status; } async function fetchMovie(url, site) { const res = await get(url); return res.ok ? scrapeMovie(res.item, url, site) : res.status; } async function fetchProfile(actorName) { const actorSlugA = slugify(actorName, { delimiter: '-' }); const actorSlugB = slugify(actorName, { delimiter: '' }); const urlA = `https://julesjordan.com/trial/models/${actorSlugA}.html`; const urlB = `https://julesjordan.com/trial/models/${actorSlugB}.html`; const resA = await bhttp.get(urlA); if (resA.statusCode === 200) { const profile = scrapeProfile(resA.body.toString(), urlA, actorName); return profile; } const resB = await bhttp.get(urlB); if (resB.statusCode === 200) { const profile = scrapeProfile(resB.body.toString(), urlB, actorName); return profile; } return null; } module.exports = { fetchLatest, fetchMovie, fetchProfile, fetchUpcoming, fetchScene, };