'use strict'; /* eslint-disable */ const bhttp = require('bhttp'); const cheerio = require('cheerio'); const moment = require('moment'); const knex = require('../knex'); const { matchTags } = require('../tags'); function scrapeLatest(html, site) { const $ = cheerio.load(html, { normalizeWhitespace: true }); const sceneElements = $('.content-wrapper.container .scene').toArray(); return sceneElements.map((element) => { const sceneLinkElement = $(element).find('a[data-track="TITLE_LINK"]'); const url = sceneLinkElement.attr('href'); const title = sceneLinkElement.text(); const shootId = url.split('/').slice(-1)[0]; const date = moment.utc($(element).find('.scene-date'), 'MM/DD/YYYY').toDate(); const actors = $(element).find('a[data-track="PORNSTAR_LINK"]').map((actorIndex, actorElement) => $(actorElement).text()).toArray(); const likes = Number($(element).find('.scene-votes').text()); return { url, shootId, title, actors, date, rating: { likes, dislikes: 0, }, site, }; }); } async function scrapeScene(html, url, site) { const $ = cheerio.load(html, { normalizeWhitespace: true }); const shootId = url.split('/').slice(-1)[0]; const title = $('.video-wrapper meta[itemprop="name"]').attr('content'); const date = moment.utc($('.video-wrapper meta[itemprop="uploadDate"]').attr('content'), 'MM/DD/YYYY').toDate() const actors = $('.content-wrapper .scene-models-list a').map((actorIndex, actorElement) => $(actorElement).text()).toArray(); const description = $('.video-wrapper meta[itemprop="description"]').attr('content'); const [minutes, seconds] = $('.video-wrapper meta[itemprop="duration"]').attr('content').match(/\d+/g); const duration = Number(minutes) * 60 + Number(seconds); const likes = Number($('.content-desc #social-actions #likes').text()); const siteElement = $('.content-wrapper .logos-sites a'); const siteUrl = siteElement.attr('href').slice(0, -1); const siteName = siteElement.text(); const channelSite = await knex('sites') .where({ url: siteUrl }) .orWhere({ name: siteName }) .first(); const rawTags = $('.content-desc .scene-tags a').map((tagIndex, tagElement) => $(tagElement).text()).toArray(); const tags = await matchTags(rawTags); return { url, shootId, title, date, actors, description, duration, tags, rating: { likes, dislikes: 0, }, site: channelSite || site, }; } async function fetchLatest(site) { const res = await bhttp.get(`${site.url}/`); 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, };