'use strict'; const Promise = require('bluebird'); const bhttp = require('bhttp'); const cheerio = require('cheerio'); const { JSDOM } = require('jsdom'); const moment = require('moment'); const { heightToCm } = require('../utils/convert'); async function fetchPhotos(url) { const res = await bhttp.get(url); return res.body.toString(); } function scrapePhotos(html) { 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 return [ src.replace('thumbs/', 'photos/'), src.replace('thumbs/', '1024watermarked/'), src, ]; }); return photos; } async function getPhotos(entryId, site, page = 1) { const albumUrl = `https://www.julesjordan.com/trial/gallery.php?id=${entryId}&type=highres&page=${page}`; const html = await fetchPhotos(albumUrl); const $ = cheerio.load(html, { normalizeWhitespace: true }); const photos = scrapePhotos(html); const pagesString = $('.page_totals').text().trim(); const pages = pagesString.length > 0 ? Number($('.page_totals').text().trim().match(/\d+$/)[0]) : null; const otherPhotos = pages ? await Promise.map(Array.from({ length: pages - 1 }), async (val, index) => { const pageUrl = `https://www.julesjordan.com/trial/gallery.php?id=${entryId}&type=highres&page=${index + 2}`; const pageHtml = await fetchPhotos(pageUrl); return scrapePhotos(pageHtml); }, { concurrency: 2, }) : []; return photos.concat(otherPhotos.flat()); } function scrapeLatest(html, site) { const $ = cheerio.load(html, { normalizeWhitespace: true }); const scenesElements = $('.update_details').toArray(); return scenesElements.map((element) => { const photoElement = $(element).find('a img.thumbs'); const photoCount = Number(photoElement.attr('cnt')); const [poster, ...photos] = Array.from({ length: photoCount }, (value, index) => { const src = photoElement.attr(`src${index}_1x`) || photoElement.attr(`src${index}`); if (!src) return null; if (src.match(/^http/)) return src; return `${site.url}${src}`; }).filter(photoUrl => photoUrl); const sceneLinkElement = $(element).children('a').eq(1); const url = sceneLinkElement.attr('href'); const title = sceneLinkElement.text(); const entryId = $(element).attr('data-setid'); 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, entryId, title, actors, date, poster, photos, site, }; }); } 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 trailer = videoScript.slice(videoScript.indexOf('https://'), videoScript.indexOf('.mp4') + 4); return { url: null, entryId, title, date, actors, poster, trailer: { src: trailer, }, rating: null, site, }; }); } async function scrapeScene(html, url, site) { const $ = cheerio.load(html, { normalizeWhitespace: true }); const title = $('.title_bar_hilite').text().trim(); const entryId = $('.suggest_tags a').attr('href').match(/\d+/)[0]; const date = moment .utc($('.update_date').text(), 'MM/DD/YYYY') .toDate(); const actors = $('.backgroundcolor_info > .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 infoLines = $('script:contains("useimage")') .html() .split('\n'); const posterPath = infoLines.find(line => line.match('useimage')).replace('useimage = "', '').slice(0, -2); const poster = posterPath.match(/^http/) ? posterPath : `${site.url}${posterPath}`; const trailerLine = infoLines.find(line => line.match('movie["Trailer_720"]')); const trailer = trailerLine.slice(trailerLine.indexOf('path:"') + 6, trailerLine.indexOf('",movie')); const photos = await getPhotos(entryId, site); const tags = $('.update_tags a').map((tagIndex, tagElement) => $(tagElement).text()).toArray(); const movie = $('.update_dvds a').attr('href'); return { url, entryId, title, date, actors, description, poster, photos, movie, trailer: { src: trailer, quality: 720, }, rating: { stars, }, tags, site, }; } 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 src = avatarEl.innerHTML.slice(avatarEl.innerHTML.indexOf('src') + 5, avatarEl.innerHTML.indexOf('set.jpg') + 7).trim(); const src0 = avatarEl.innerHTML.slice(avatarEl.innerHTML.indexOf('src0') + 6, avatarEl.innerHTML.indexOf('set.jpg') + 7).trim(); const src1 = avatarEl.innerHTML.slice(avatarEl.innerHTML.indexOf('src0_1x') + 9, avatarEl.innerHTML.indexOf('1x.jpg') + 6).trim(); const src2 = avatarEl.innerHTML.slice(avatarEl.innerHTML.indexOf('src0_2x') + 9, avatarEl.innerHTML.indexOf('2x.jpg') + 6).trim(); const src3 = avatarEl.innerHTML.slice(avatarEl.innerHTML.indexOf('src0_3x') + 9, avatarEl.innerHTML.indexOf('3x.jpg') + 6).trim(); const avatar = src3 || src2 || src1 || src0 || src; if (avatar) profile.avatar = avatar; } 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 res = await bhttp.get(`${site.url}/trial/categories/movies_${page}_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); } async function fetchProfile(actorName) { const actorSlugA = actorName.toLowerCase().replace(/\s+/g, '-'); const actorSlugB = actorName.toLowerCase().replace(/\s+/g, ''); 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, fetchProfile, fetchUpcoming, fetchScene, };