'use strict'; const bhttp = require('bhttp'); const cheerio = require('cheerio'); const moment = require('moment'); const knex = require('../knex'); const { matchTags } = require('../tags'); /* eslint-disable newline-per-chained-call */ function scrapeLatest(html, site) { const $ = cheerio.load(html, { normalizeWhitespace: true }); const sceneElements = $('.card.m-1').toArray(); return sceneElements.map((element) => { const sceneLinkElement = $(element).find('a').first(); const title = sceneLinkElement.attr('title'); const url = `${site.url}${sceneLinkElement.attr('href')}`; const entryId = url.split('/').slice(-1)[0]; const date = moment.utc($(element).find('.card-footer .text-muted').text(), 'MMMM DD, YYYY').toDate(); const actors = $(element).find('.card-subtitle a').map((actorIndex, actorElement) => $(actorElement).text().trim()).toArray().filter(actor => actor); const duration = Number($(element).find('.card-info div:nth-child(2) .card-text').text().slice(0, -4)) * 60; return { url, entryId, title, actors, date, duration, rating: null, site, }; }); } async function scrapeScene(html, url, site) { const $ = cheerio.load(html, { normalizeWhitespace: true }); const entryId = url.split('/').slice(-1)[0]; const title = $('.video-title h1').text(); const description = $('.description-box .box-container').text(); const date = moment.utc($('.video-title .remain time').text(), 'MMMM DD, YYYY').toDate(); const actors = $('.pornstars-box .pornstar-card .card-title a').map((actorIndex, actorElement) => $(actorElement).text()).toArray(); const likes = Number($('.info-panel.likes .likes').text()); const duration = Number($('.info-panel.duration .duration').text().slice(0, -4)) * 60; const { origin } = new URL($('.pornstar-card meta[itemprop="url"]').first().attr('content')); const rawTags = $('#tagsBox .tags a').map((tagIndex, tagElement) => $(tagElement).text()).toArray(); const [channelSite, tags] = await Promise.all([ // don't find site if original is already specific site.isFallback ? knex('sites').where({ url: origin }).first() : site, matchTags(rawTags), ]); return { url: channelSite ? `${channelSite.url}${new URL(url).pathname}` : url, entryId, title, description, actors, date, duration, tags, rating: { likes, }, site: channelSite || site, }; } async function fetchLatest(site, page = 1) { const res = await bhttp.get(`https://ddfnetwork.com/videos/search/latest/ever/${new URL(site.url).hostname}/-/${page}`); return scrapeLatest(res.body.toString(), site); } async function fetchScene(url, site) { const res = await bhttp.get(`https://ddfnetwork.com${new URL(url).pathname}`); return scrapeScene(res.body.toString(), url, site); } module.exports = { fetchLatest, fetchScene, };