Refactored Mike Adriano scraper. Changed logo and favicon. Added style methods to qu.

This commit is contained in:
DebaucheryLibrarian
2020-07-16 03:47:07 +02:00
parent 6584a46d53
commit 6adfded074
63 changed files with 577 additions and 332 deletions

View File

@@ -68,7 +68,7 @@ async function fetchActorReleases({ query }, url, remainingPages, actorName, acc
return accReleases.concat(releases);
}
async function scrapeProfile({ query, window }, actorName, url, include) {
async function scrapeProfile({ query }, actorName, url, include) {
const profile = {};
profile.avatar = {
@@ -85,7 +85,7 @@ async function scrapeProfile({ query, window }, actorName, url, include) {
if (include.releases) {
const availablePages = query.all('.pageboxdropdown option', 'value');
profile.releases = await fetchActorReleases(qu.init(query.q('#episodes > table'), window), url, availablePages.slice(1), actorName);
profile.releases = await fetchActorReleases(qu.init(query.q('#episodes > table')), url, availablePages.slice(1), actorName);
}
return profile;
@@ -105,7 +105,9 @@ async function fetchScene(url, channel) {
async function fetchProfile(actorName, entity, include) {
const url = `http://killergram.com/episodes.asp?page=episodes&model=${encodeURI(actorName)}&ct=model`;
const res = await qu.get(url, '#content');
const res = await qu.get(url, '#content', null, {
followRedirects: false,
});
return res.ok ? scrapeProfile(res.item, actorName, url, include) : res.status;
}

View File

@@ -1,237 +1,88 @@
'use strict';
/* eslint-disable newline-per-chained-call */
const { JSDOM } = require('jsdom');
const cheerio = require('cheerio');
const moment = require('moment');
// const bhttp = require('bhttp');
const qu = require('../utils/qu');
const { get } = require('../utils/http');
const descriptionTags = {
'anal cream pie': 'anal creampie',
'ass to mouth': 'ass to mouth',
'cream pie in her ass': 'anal creampie',
'eats ass': 'ass eating',
facial: 'facial',
gaped: 'gaping',
gapes: 'gaping',
gape: 'gaping',
'rectal cream pie': 'anal creampie',
rimming: 'ass eating',
};
function deriveTagsFromDescription(description) {
const matches = (description || '').toLowerCase().match(new RegExp(Object.keys(descriptionTags).join('|'), 'g'));
return matches
? matches.map(match => descriptionTags[match])
: [];
}
async function scrapeLatestA(html, site) {
const { document } = new JSDOM(html).window;
const sceneElements = document.querySelectorAll('.content-item-large, .content-item');
return Promise.all(Array.from(sceneElements, async (element) => {
const $ = cheerio.load(element.innerHTML, { normalizeWhitespace: true });
const titleElement = element.querySelector('h3.title a');
const title = titleElement.textContent;
const url = titleElement.href;
const entryId = url.split('/').slice(-2)[0];
const descriptionElement = element.querySelector('.desc');
const description = descriptionElement && descriptionElement.textContent.trim();
const date = moment(element.querySelector('.date, time').textContent, 'Do MMM YYYY').toDate();
const actors = Array.from(element.querySelectorAll('h4.models a'), actorElement => actorElement.textContent);
const durationString = element.querySelector('.total-time').textContent.trim();
// timestamp is sometimes 00:00, sometimes 0:00:00
const duration = durationString.split(':').length === 3
? moment.duration(durationString).asSeconds()
: moment.duration(`00:${durationString}`).asSeconds();
const ratingElement = element.querySelector('.rating');
const stars = ratingElement && ratingElement.dataset.rating;
const [poster, ...primaryPhotos] = Array.from(element.querySelectorAll('img'), imageElement => imageElement.src);
const secondaryPhotos = $('.thumb-top, .thumb-bottom')
.map((photoIndex, photoElement) => $(photoElement).css()['background-image'])
.toArray()
.map(photoUrl => photoUrl.slice(photoUrl.indexOf('http'), photoUrl.indexOf('.jpg') + 4));
const photos = [...primaryPhotos, ...secondaryPhotos];
const tags = deriveTagsFromDescription(description);
const scene = {
url,
entryId,
title,
description,
actors,
director: 'Mike Adriano',
date,
duration,
tags,
poster,
photos,
rating: {
stars,
},
site,
};
return scene;
}));
}
async function scrapeLatestB(html) {
const { document } = new JSDOM(html).window;
const sceneElements = document.querySelectorAll('.content-border');
return Promise.all(Array.from(sceneElements, async (element) => {
const $ = cheerio.load(element.innerHTML, { normalizeWhitespace: true });
async function scrapeAll(scenes) {
return scenes.map(({ query }) => {
const release = {
director: 'Mike Adriano',
};
const titleElement = element.querySelector('.content-title-wrap a');
release.title = titleElement.title || titleElement.textContent.trim();
release.url = titleElement.href;
release.entryId = release.url.split('/').slice(-2)[0];
release.title = query.cnt('h3.title a, .content-title-wrap a');
release.url = query.url('h3.title a, .content-title-wrap a');
release.entryId = new URL(release.url).pathname.match(/\/view\/(\d+)/)[1];
release.description = element.querySelector('.content-description').textContent.trim();
release.date = (moment(element.querySelector('.mobile-date').textContent, 'MM/DD/YYYY')
|| moment(element.querySelector('.date').textContent, 'Do MMM YYYY')).toDate();
release.actors = Array.from(element.querySelectorAll('.content-models a'), actorElement => actorElement.textContent);
release.description = query.cnt('.desc, .content-description');
release.date = query.date('.date, time, .hide', 'Do MMM YYYY');
const durationString = element.querySelector('.total-time').textContent.trim();
// timestamp is somethines 00:00, sometimes 0:00:00
release.duration = durationString.split(':').length === 3
? moment.duration(durationString).asSeconds()
: moment.duration(`00:${durationString}`).asSeconds();
release.actors = query.cnts('h4.models a, .content-models a');
release.duration = query.dur('.total-time');
const [poster, ...primaryPhotos] = Array.from(element.querySelectorAll('a img'), imageElement => imageElement.src);
const secondaryPhotos = $('.thumb-mouseover')
.map((photoIndex, photoElement) => $(photoElement).css()['background-image'])
.toArray()
.map(photoUrl => photoUrl.slice(photoUrl.indexOf('http'), photoUrl.indexOf('.jpg') + 4));
const [poster, ...primaryPhotos] = query.imgs('a img');
const secondaryPhotos = query.styles('.thumb-top, .thumb-bottom, .thumb-mouseover', 'background-image').map(style => style.match(/url\((.*)\)/)[1]);
release.poster = poster;
release.photos = [...primaryPhotos, ...secondaryPhotos];
release.photos = primaryPhotos.concat(secondaryPhotos);
release.tags = deriveTagsFromDescription(release.description);
return release;
}));
});
}
async function scrapeSceneA(html, url) {
const { document } = new JSDOM(html).window;
const element = document.querySelector('.content-page-info');
const release = {
url,
director: 'Mike Adriano',
};
function scrapeScene({ query }, url) {
const release = { director: 'Mike Adriano' };
release.entryId = url.split('/').slice(-2)[0];
release.title = element.querySelector('.title').textContent.trim();
release.description = element.querySelector('.desc').textContent.trim();
release.date = moment(element.querySelector('.post-date').textContent.trim(), 'Do MMM YYYY').toDate();
if (query.exists('a[href*="stackpath.com"]')) {
throw new Error('URL blocked by StackPath');
}
release.actors = Array.from(element.querySelectorAll('.models a'), actorElement => actorElement.textContent);
release.entryId = new URL(url).pathname.match(/\/view\/(\d+)/)[1];
const durationString = element.querySelector('.total-time').textContent.trim();
// timestamp is sometimes 00:00, sometimes 0:00:00
release.duration = durationString.split(':').length === 3
? moment.duration(durationString).asSeconds()
: moment.duration(`00:${durationString}`).asSeconds();
release.title = query.cnt('.content-page-info .title');
release.description = query.cnt('.content-page-info .desc');
release.date = query.date('.content-page-info .date, .content-page-info .hide', 'Do MMM YYYY');
const { poster } = document.querySelector('.content-page-header video');
const { src, type } = document.querySelector('.content-page-header source');
release.actors = query.cnts('.content-page-info .models a');
release.duration = query.dur('.content-page-info .total-time:last-child');
release.poster = poster;
release.trailer = { src, type };
release.poster = query.poster('.content-page-header video, .content-page-header-inner video');
release.tags = deriveTagsFromDescription(release.description);
const trailerEl = query.q('.content-page-header source, .content-page-header-inner source');
if (trailerEl) {
release.trailer = {
src: trailerEl.src,
type: trailerEl.type,
};
}
return release;
}
async function scrapeSceneB(html, url, site) {
const { document } = new JSDOM(html).window;
const element = document.querySelector('.content-page-info');
const entryId = url.split('/').slice(-2)[0];
const title = element.querySelector('.title').textContent.trim();
const description = element.querySelector('.desc').textContent.trim();
const date = moment(element.querySelector('.date').textContent.trim(), 'Do MMM YYYY').toDate();
const actors = Array.from(element.querySelectorAll('.models a'), actorElement => actorElement.textContent);
const durationString = element.querySelector('.total-time').textContent.trim();
// timestamp is somethines 00:00, sometimes 0:00:00
const duration = durationString.split(':').length === 3
? moment.duration(durationString).asSeconds()
: moment.duration(`00:${durationString}`).asSeconds();
const { poster } = document.querySelector('.content-page-header-inner video');
const { src, type } = document.querySelector('.content-page-header-inner source');
const tags = deriveTagsFromDescription(description);
const scene = {
url,
entryId,
title,
description,
actors,
director: 'Mike Adriano',
date,
duration,
tags,
poster,
trailer: {
src,
type,
},
site,
};
return scene;
}
async function fetchLatest(site, page = 1) {
const { host } = new URL(site.url);
async function fetchLatest(channel, page = 1) {
const { host } = new URL(channel.url);
const url = `https://tour.${host}/videos?page=${page}`;
const res = await get(url);
const res = await qu.get(url);
if (res.code === 200) {
if (host === 'trueanal.com' || host === 'swallowed.com') {
return scrapeLatestA(res.html, site);
if (res.ok) {
if (res.item.query.exists('a[href*="stackpath.com"]')) {
throw new Error('URL blocked by StackPath');
}
return scrapeLatestB(res.html, site);
return scrapeAll(qu.initAll(res.item.el, '.content-item-large, .content-item, .content-border'), channel);
}
return res.code;
return res.status;
}
async function fetchScene(url, site) {
const { host } = new URL(site.url);
const res = await get(url);
async function fetchScene(url, channel) {
const res = await qu.get(url);
if (res.code === 200) {
if (host === 'trueanal.com' || host === 'swallowed.com') {
return scrapeSceneA(res.body.toString(), url, site);
}
return scrapeSceneB(res.body.toString(), url, site);
if (res.ok) {
return scrapeScene(res.item, url, channel);
}
return res.code;
return res.status;
}
/* API protected

View File

@@ -78,7 +78,7 @@ function scrapeAll(scenes, site, origin) {
release.url = `${site?.url || origin}${scene.targetUrl}`;
release.date = moment.utc(scene.releaseDate).toDate();
release.shootDate = moment.utc(scene.shootDate).toDate();
release.datePrecision = 'minute';
release.actors = scene.models;
release.stars = Number(scene.textRating) / 2;
@@ -104,7 +104,7 @@ function scrapeUpcoming(scene, site) {
release.url = `${site.url}${scene.targetUrl}`;
release.date = moment.utc(scene.releaseDate).toDate();
release.shootDate = moment.utc(scene.shootDate).toDate();
release.datePrecision = 'minute';
release.actors = scene.models;
@@ -133,7 +133,8 @@ async function scrapeScene(data, url, site, baseRelease) {
release.entryId = scene.newId;
release.date = moment.utc(scene.releaseDate).toDate();
release.shootDate = moment.utc(scene.shootDate).toDate();
release.productionDate = moment.utc(scene.shootDate).toDate();
release.datePrecision = 'minute';
release.actors = baseRelease?.actors || scene.models;

View File

@@ -31,6 +31,7 @@ function curateReleaseEntry(release, batchId, existingRelease) {
shoot_id: release.shootId || null,
url: release.url,
date: Number(release.date) ? release.date : null,
production_date: Number(release.productionDate) ? release.productionDate : null,
date_precision: release.datePrecision,
slug,
description: release.description,

17
src/utils/jsdom-url.js Normal file
View File

@@ -0,0 +1,17 @@
'use strict';
const { JSDOM } = require('jsdom');
const el = new JSDOM(`
<span id="url" style="background-image: url(https://w.wallhaven.cc/full/dg/wallhaven-dg7y23.jpg);">
<span id="urlQuotes" style="background-image: url('https://w.wallhaven.cc/full/dg/wallhaven-dg7y23.jpg');">
<span id="color" style="color: rgb(255, 0, 0);">
<span id="urlSpaces" style="background-image: url( https://w.wallhaven.cc/full/md/wallhaven-mdvmrm.jpg );">
<span id="colorSpaces" style="color: rgb( 255, 0, 0 );">
`).window.document;
console.log(el.querySelector('#url').style.backgroundImage);
console.log(el.querySelector('#urlQuotes').style.backgroundImage);
console.log(el.querySelector('#color').style.color);
console.log(el.querySelector('#urlSpaces').style.backgroundImage);
console.log(el.querySelector('#colorSpaces').style.color);

View File

@@ -4,9 +4,11 @@ const { JSDOM } = require('jsdom');
const moment = require('moment');
const http = require('./http');
const { window: globalWindow } = new JSDOM('');
function trim(str) {
if (typeof str !== 'string') {
return null;
return str;
}
return str.trim().replace(/\s+/g, ' ');
@@ -55,9 +57,9 @@ function q(context, selector, attrArg, applyTrim = true) {
if (attr) {
const value = selector
? context.querySelector(selector)?.[attr] || context.querySelector(selector)?.attributes[attr]?.value
: context[attr] || context.attributes[attr]?.value;
: context[attr] || context.getAttribute(attr);
return applyTrim && value ? trim(value) : value;
return applyTrim && typeof value === 'string' ? trim(value) : value;
}
return selector ? context.querySelector(selector) : context;
@@ -77,6 +79,14 @@ function exists(context, selector) {
return !!q(context, selector);
}
function content(context, selector, applyTrim = true) {
return q(context, selector, 'textContent', applyTrim);
}
function contents(context, selector, applyTrim) {
return all(context, selector, 'textContent', applyTrim);
}
function html(context, selector) {
const el = q(context, selector, null, true);
@@ -103,6 +113,33 @@ function text(context, selector, applyTrim = true) {
return applyTrim ? trim(textValue) : textValue;
}
function removeStyleFunctionSpaces(el) {
// jsdom appears to have a bug where it ignores inline CSS attributes set to a function() containing spaces, e.g. url( image.png )
el.setAttribute('style', el.getAttribute('style').replace(/\(\s+(.*)\s+\)/g, (match, cssArgs) => `(${cssArgs})`));
}
function style(context, selector, styleAttr) {
const el = q(context, selector);
if (el) {
removeStyleFunctionSpaces(el);
return styleAttr ? el.style[styleAttr] : el.style;
}
return null;
}
function styles(context, selector, styleAttr) {
const elStyles = Array.from(context.querySelectorAll(selector), (el) => {
removeStyleFunctionSpaces(el);
return styleAttr ? el.style[styleAttr] : el.style;
});
return elStyles;
}
function number(context, selector, attr = true) {
const value = q(context, selector, attr);
@@ -236,6 +273,10 @@ const legacyFuncs = {
const quFuncs = {
all,
html,
content,
contents,
cnt: content,
cnts: contents,
date,
dur: duration,
duration,
@@ -250,6 +291,8 @@ const quFuncs = {
num: number,
poster,
q,
style,
styles,
text,
texts,
trailer: video,
@@ -265,7 +308,7 @@ function init(element, window) {
const legacyContextFuncs = Object.entries(legacyFuncs) // dynamically attach methods with context
.reduce((acc, [key, func]) => ({
...acc,
[key]: (...args) => (window && args[0] instanceof window.HTMLElement // allow for different context
[key]: (...args) => (args[0] instanceof globalWindow.HTMLElement // allow for different context
? func(...args)
: func(element, ...args)),
}), {});
@@ -273,7 +316,7 @@ function init(element, window) {
const quContextFuncs = Object.entries(quFuncs) // dynamically attach methods with context
.reduce((acc, [key, func]) => ({
...acc,
[key]: (...args) => (window && args[0] instanceof window.HTMLElement // allow for different context
[key]: (...args) => (args[0] instanceof globalWindow.HTMLElement // allow for different context
? func(...args)
: func(element, ...args)),
}), {});
@@ -319,7 +362,7 @@ function extractAll(htmlValue, selector) {
}
async function get(urlValue, selector, headers, options, queryAll = false) {
const res = await http.get(urlValue, headers);
const res = await http.get(urlValue, headers, options);
if (res.statusCode === 200) {
const item = queryAll