ripunzel/src/methods/erome.js

79 lines
2.2 KiB
JavaScript

'use strict';
const config = require('config');
const fetch = require('node-fetch');
const cheerio = require('cheerio');
const logger = require('../logger')(__filename);
const base = 'https://www.erome.com/';
async function erome(host, post) {
const url = `${base}a/${host.id}`;
const res = await fetch(url);
if (!res.ok) {
throw new Error(`Unable to retrieve info for Erome album '${host.id}' :(`);
}
const html = await res.text();
const $ = cheerio.load(html);
const title = $('meta[property="og:title"]').attr('content') || $('meta[property="twitter:title"]').attr('content');
const items = $('.media-group').toArray().map((mediaItem) => {
const mediaElement = $(mediaItem);
const videoElement = mediaElement.find('.video video');
const id = mediaElement.attr('id');
const itemTitle = mediaElement.find('h2.media-title').text();
if (videoElement.length) {
const sourceHd = videoElement.find('source[label="HD"]');
const sourceSd = videoElement.find('source[label="SD"]');
return {
id,
title: itemTitle,
url: sourceHd.length ? sourceHd.attr('src') : sourceSd.attr('src'),
type: (sourceHd.length ? sourceHd.attr('type') : sourceSd.attr('type')) || 'video/mp4',
};
}
const img = mediaElement.find('.img-front').attr('data-src');
return {
id,
title: itemTitle,
url: img,
};
});
const extract = config.library.extractSingleAlbumItem && (items.length === 1);
if (extract) {
logger.verbose(`Extracting single item from album '${url}' (${post ? post.url : 'no post'})`);
}
return {
album: extract ? null : {
id: host.id,
url,
title,
description: null,
datetime: null,
},
items: items.map(item => ({
extracted: extract,
id: item.id,
url: item.url,
title: item.title || title || null,
description: null,
type: item.type || 'image/jpeg',
datetime: null,
})),
};
}
module.exports = erome;