64 lines
1.9 KiB
JavaScript
64 lines
1.9 KiB
JavaScript
'use strict';
|
|
|
|
const config = require('config');
|
|
const fetch = require('node-fetch');
|
|
|
|
const logger = require('../logger')(__filename);
|
|
const { fetchPredata } = require('./imgurImage');
|
|
|
|
async function imgurAlbumApi(host, post, { predata }) {
|
|
if (predata.remaining === 10) { // keep a buffer
|
|
throw new Error(`Reached Imgur API rate limit with source '${host.url}'`);
|
|
}
|
|
|
|
const res = await fetch(`https://api.imgur.com/3/album/${host.id}`, {
|
|
headers: {
|
|
Authorization: `Client-ID ${config.methods.imgur.clientId}`,
|
|
},
|
|
});
|
|
|
|
const rateRemaining = Number(res.headers.get('x-ratelimit-userremaining'));
|
|
|
|
if (rateRemaining) {
|
|
predata.setRemaining(rateRemaining);
|
|
}
|
|
|
|
const { data } = await res.json();
|
|
|
|
if (res.status !== 200) {
|
|
throw new Error(`Could not fetch info for imgur album '${host.id}': ${res.data.error}`);
|
|
}
|
|
|
|
const extract = config.library.extractSingleAlbumItem && data.images.length === 1;
|
|
|
|
if (extract) {
|
|
logger.verbose(`Extracting single item from album '${data.link}' (${post ? post.url : 'no post'})`);
|
|
}
|
|
|
|
return {
|
|
album: extract ? null : {
|
|
id: data.id,
|
|
url: data.link,
|
|
title: data.title,
|
|
description: data.description,
|
|
datetime: new Date(data.datetime * 1000),
|
|
original: data,
|
|
},
|
|
items: data.images.map((item) => ({
|
|
extracted: extract,
|
|
id: item.id,
|
|
url: item.animated ? item.mp4 : item.link,
|
|
title: item.title || data.title || null,
|
|
description: item.description || data.description || null,
|
|
type: item.animated ? 'video/mp4' : item.type,
|
|
datetime: item.datetime * 1000,
|
|
original: item,
|
|
})),
|
|
};
|
|
}
|
|
|
|
module.exports = {
|
|
fetchInfo: imgurAlbumApi,
|
|
fetchPredata,
|
|
};
|