33 lines
843 B
JavaScript
33 lines
843 B
JavaScript
|
'use strict';
|
||
|
|
||
|
const mime = require('mime');
|
||
|
const bhttp = require('bhttp');
|
||
|
const { JSDOM } = require('jsdom');
|
||
|
|
||
|
async function redditAlbum(host, post) {
|
||
|
const res = await bhttp.get(host.url);
|
||
|
|
||
|
if (res.statusCode !== 200) {
|
||
|
throw new Error(res.body.toString());
|
||
|
}
|
||
|
|
||
|
const { document } = new JSDOM(res.body.toString(), { runScripts: 'dangerously' }).window;
|
||
|
const items = Array.from(document.querySelectorAll('li a'), el => el.href);
|
||
|
|
||
|
return {
|
||
|
album: {
|
||
|
id: host.id,
|
||
|
url: host.url,
|
||
|
title: post.title,
|
||
|
},
|
||
|
items: items.map(url => ({
|
||
|
id: new URL(url).pathname.match(/\/(.*).jpg/)[1],
|
||
|
url,
|
||
|
datetime: post.datetime,
|
||
|
type: mime.getType(url) || 'image/jpeg',
|
||
|
})),
|
||
|
};
|
||
|
}
|
||
|
|
||
|
module.exports = redditAlbum;
|