'use strict'; const config = require('config'); const fetch = require('node-fetch'); const UrlPattern = require('url-pattern'); const cheerio = require('cheerio'); const mime = require('mime-types'); const logger = require('../logger')(__filename); const pattern = new UrlPattern('https\\://(www.)vidble.com/:id(_med)(.:ext)'); async function vidbleAlbum(host, post) { const res = await fetch(`https://www.vidble.com/album/${host.id}`); if (res.status !== 200) { throw new Error(`Could not fetch info for vidble album '${host.id}': '${res.error}'`); } const html = await res.text(); const $ = cheerio.load(html); const title = $('h2').text(); const imgUrls = $('img.img2').toArray().map(img => `https://vidble.com${img.attribs.src || img.attribs['data-original']}`); const extract = config.library.extractSingleAlbumItem && imgUrls.length === 1; if (extract) { logger.verbose(`Extracting single item from album '${post.title}' - ${res.link}`); } return { album: extract ? null : { id: host.id, url: post ? post.url : null, title: post ? post.title : title, datetime: post ? post.datetime : null, }, items: imgUrls.map((url) => { const components = pattern.match(url); const id = components.id.replace('_med', ''); const mimetype = mime.lookup(components.ext); return { extracted: extract, id, url: `https://vidble.com/${id}.${components.ext}`, type: mimetype, datetime: post ? post.datetime : null, }; }), }; } module.exports = vidbleAlbum;