Bumped to Node 24 and upgraded dependencies.
This commit is contained in:
@@ -1,18 +1,32 @@
|
||||
'use strict';
|
||||
|
||||
const mime = require('mime');
|
||||
const bhttp = require('bhttp');
|
||||
const { JSDOM } = require('jsdom');
|
||||
// reddit gallery images: `s.u` (still) or `s.gif`/`s.mp4` (animated) - URLs come back
|
||||
// HTML-entity-encoded (e.g. '&' instead of '&')
|
||||
function resolveMediaUrl(media) {
|
||||
const url = media?.s?.u || media?.s?.gif || media?.s?.mp4;
|
||||
|
||||
async function redditAlbum(host, post) {
|
||||
const res = await bhttp.get(host.url);
|
||||
return url ? url.replace(/&/g, '&') : null;
|
||||
}
|
||||
|
||||
if (res.statusCode !== 200) {
|
||||
throw new Error(res.body.toString());
|
||||
}
|
||||
async function redditAlbum(host, post, { reddit }) {
|
||||
// fetched through the authenticated/throttled client, not a raw fetch()/bhttp request -
|
||||
// reddit's anonymous-traffic bot challenge blocks plain unauthenticated .json requests
|
||||
const data = await reddit.getSubmission(post.id);
|
||||
|
||||
const { document } = new JSDOM(res.body.toString(), { runScripts: 'dangerously' }).window;
|
||||
const items = Array.from(document.querySelectorAll('li a'), el => el.href);
|
||||
const items = (data.gallery_data?.items || [])
|
||||
.filter((item) => !item.is_deleted)
|
||||
.map((item) => {
|
||||
const media = data.media_metadata?.[item.media_id];
|
||||
const url = resolveMediaUrl(media);
|
||||
|
||||
return url && {
|
||||
id: item.media_id,
|
||||
url,
|
||||
datetime: post.datetime,
|
||||
type: media.m || 'image/jpeg',
|
||||
};
|
||||
})
|
||||
.filter(Boolean);
|
||||
|
||||
return {
|
||||
album: {
|
||||
@@ -20,12 +34,7 @@ async function redditAlbum(host, post) {
|
||||
url: host.url,
|
||||
title: post.title,
|
||||
},
|
||||
items: items.map((url) => ({
|
||||
id: new URL(url).pathname.match(/\/(.*).\w+$/)?.[1],
|
||||
url,
|
||||
datetime: post.datetime,
|
||||
type: mime.getType(url) || 'image/jpeg',
|
||||
})),
|
||||
items,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user