Added support for RedGifs and Reddit albums. Improved command line logger. Added rate limiters for reddit and host URLs.
This commit is contained in:
32
src/methods/redditAlbum.js
Normal file
32
src/methods/redditAlbum.js
Normal file
@@ -0,0 +1,32 @@
|
||||
'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;
|
||||
Reference in New Issue
Block a user