forked from DebaucheryLibrarian/traxxx
109 lines
3.2 KiB
JavaScript
109 lines
3.2 KiB
JavaScript
|
'use strict';
|
||
|
|
||
|
const config = require('config');
|
||
|
const moment = require('moment');
|
||
|
|
||
|
const networks = require('../networks.js');
|
||
|
const scrapers = require('./scrapers');
|
||
|
|
||
|
function accumulateIncludedSites() {
|
||
|
return config.include.reduce((acc, network) => {
|
||
|
// network included with specific sites, only include specified sites
|
||
|
if (Array.isArray(network)) {
|
||
|
const [networkId, siteIds] = network;
|
||
|
|
||
|
return [
|
||
|
...acc,
|
||
|
...siteIds.map(siteId => ({
|
||
|
id: siteId,
|
||
|
network: networkId,
|
||
|
...networks[networkId].sites[siteId],
|
||
|
})),
|
||
|
];
|
||
|
}
|
||
|
|
||
|
// network included without further specification, include all sites
|
||
|
return [
|
||
|
...acc,
|
||
|
...Object.entries(networks[network].sites).map(([siteId, site]) => ({
|
||
|
id: siteId,
|
||
|
network,
|
||
|
...site,
|
||
|
})),
|
||
|
];
|
||
|
}, []);
|
||
|
}
|
||
|
|
||
|
function accumulateExcludedSites() {
|
||
|
return Object.entries(networks).reduce((acc, [networkId, network]) => {
|
||
|
const excludedNetwork = config.exclude.find((excludedNetworkX) => {
|
||
|
if (Array.isArray(excludedNetworkX)) {
|
||
|
return excludedNetworkX[0] === networkId;
|
||
|
}
|
||
|
|
||
|
return excludedNetworkX === networkId;
|
||
|
});
|
||
|
|
||
|
// network excluded with specific sites, only exclude specified sites
|
||
|
if (excludedNetwork && Array.isArray(excludedNetwork)) {
|
||
|
const [, excludedSiteIds] = excludedNetwork;
|
||
|
|
||
|
return [
|
||
|
...acc,
|
||
|
...Object.entries(network.sites)
|
||
|
.filter(([siteId]) => !excludedSiteIds.includes(siteId))
|
||
|
.map(([siteId, site]) => ({
|
||
|
id: siteId,
|
||
|
network: networkId,
|
||
|
...site,
|
||
|
})),
|
||
|
];
|
||
|
}
|
||
|
|
||
|
// network excluded without further specification, exclude all its sites
|
||
|
if (excludedNetwork) {
|
||
|
return acc;
|
||
|
}
|
||
|
|
||
|
// network not excluded, include all its sites
|
||
|
return [
|
||
|
...acc,
|
||
|
...Object.entries(network.sites).map(([siteId, site]) => ({
|
||
|
id: siteId,
|
||
|
network: networkId,
|
||
|
...site,
|
||
|
})),
|
||
|
];
|
||
|
}, []);
|
||
|
}
|
||
|
|
||
|
function accumulateSites() {
|
||
|
return config.include ? accumulateIncludedSites() : accumulateExcludedSites();
|
||
|
}
|
||
|
|
||
|
async function fetchReleases() {
|
||
|
const sites = await accumulateSites();
|
||
|
|
||
|
const scenesPerSite = await Promise.all(sites.map(async (site) => {
|
||
|
const scraper = scrapers[site.id] || scrapers[site.network];
|
||
|
|
||
|
if (scraper) {
|
||
|
const [latest, upcoming] = await Promise.all([
|
||
|
scraper.fetchLatest(site),
|
||
|
scraper.fetchUpcoming ? scraper.fetchUpcoming(site) : [],
|
||
|
]);
|
||
|
|
||
|
return [...latest, ...upcoming];
|
||
|
}
|
||
|
|
||
|
return [];
|
||
|
}));
|
||
|
|
||
|
const accumulatedScenes = scenesPerSite.reduce((acc, siteScenes) => ([...acc, ...siteScenes]), []);
|
||
|
const sortedScenes = accumulatedScenes.sort(({ date: dateA }, { date: dateB }) => moment(dateB).diff(dateA));
|
||
|
|
||
|
return sortedScenes;
|
||
|
}
|
||
|
|
||
|
module.exports = fetchReleases;
|