Added support for fetching content directly from host. Improved pattern interpolation. Refactored content modules.

This commit is contained in:
2024-09-11 05:16:57 +02:00
parent 20cb522689
commit b9a7e4b83a
26 changed files with 572 additions and 440 deletions

View File

@@ -1,55 +1,52 @@
'use strict';
'use strict';
const util = require('util');
const config = require('config');
const fetch = require('node-fetch');
const urlPattern = require('url-pattern');
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)');
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}'`);
}
async function vidbleAlbum(host, post) {
const res = await fetch(`https://www.vidble.com/album/${host.id}`);
return res.text();
}).then(res => {
const $ = cheerio.load(res);
if (res.status !== 200) {
throw new Error(`Could not fetch info for vidble album '${host.id}': '${res.error}'`);
}
const title = $('h2').text();
const imgUrls = $('img.img2').toArray().map(img => `https://vidble.com${img.attribs.src || img.attribs['data-original']}`);
const html = await res.text();
const $ = cheerio.load(html);
const extract = config.library.album.extractSingleItem && imgUrls.length === 1;
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) {
console.log('\x1b[36m%s\x1b[0m', `Extracting single item from album '${post.title}' - ${res.link}`);
}
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 {
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: id,
url: `https://vidble.com/${id}.${components.ext}`,
type: mimetype,
datetime: post.datetime
};
})
};
});
};
return {
extracted: extract,
id,
url: `https://vidble.com/${id}.${components.ext}`,
type: mimetype,
datetime: post.datetime,
};
}),
};
}
module.exports = vidbleAlbum;