56 lines
1.8 KiB
JavaScript
56 lines
1.8 KiB
JavaScript
|
'use strict';
|
||
|
|
||
|
const util = require('util');
|
||
|
const config = require('config');
|
||
|
const fetch = require('node-fetch');
|
||
|
const urlPattern = require('url-pattern');
|
||
|
const cheerio = require('cheerio');
|
||
|
const mime = require('mime-types');
|
||
|
|
||
|
const pattern = new urlPattern('https\\://(www.)vidble.com/:id(_med)(.:ext)');
|
||
|
|
||
|
function vidbleAlbum(post) {
|
||
|
return fetch(`https://www.vidble.com/album/${post.host.id}`).then(res => {
|
||
|
if(res.status !== 200) {
|
||
|
throw new Error(`Could not fetch info for vidble album '${post.host.id}': '${res.error}'`);
|
||
|
}
|
||
|
|
||
|
return res.text();
|
||
|
}).then(res => {
|
||
|
const $ = cheerio.load(res);
|
||
|
|
||
|
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.album.extractSingleItem && imgUrls.length === 1;
|
||
|
|
||
|
if(extract) {
|
||
|
console.log('\x1b[36m%s\x1b[0m', `Extracting single item from album '${post.title}' - ${res.link}`);
|
||
|
}
|
||
|
|
||
|
return {
|
||
|
album: extract ? null : {
|
||
|
id: post.host.id,
|
||
|
url: post.url,
|
||
|
title: post.title,
|
||
|
datetime: post.datetime
|
||
|
},
|
||
|
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: id,
|
||
|
url: `https://vidble.com/${id}.${components.ext}`,
|
||
|
type: mimetype,
|
||
|
datetime: post.datetime
|
||
|
};
|
||
|
})
|
||
|
};
|
||
|
});
|
||
|
};
|
||
|
|
||
|
module.exports = vidbleAlbum;
|