2024-09-11 03:16:56 +00:00
|
|
|
'use strict';
|
2024-09-11 03:16:53 +00:00
|
|
|
|
2024-09-11 03:16:57 +00:00
|
|
|
const config = require('config');
|
2024-09-11 03:16:53 +00:00
|
|
|
const fetch = require('node-fetch');
|
|
|
|
|
2024-09-11 03:16:58 +00:00
|
|
|
async function fetchPredata() {
|
|
|
|
const data = {
|
|
|
|
limit: 0,
|
|
|
|
remaining: 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
data.setRemaining = (remaining) => {
|
|
|
|
data.remaining = remaining;
|
|
|
|
};
|
|
|
|
|
|
|
|
const res = await fetch('https://api.imgur.com/3/credits', {
|
|
|
|
headers: {
|
|
|
|
Authorization: `Client-ID ${config.methods.imgur.clientId}`,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (res.ok) {
|
|
|
|
const body = await res.json();
|
|
|
|
|
|
|
|
if (body.success) {
|
|
|
|
data.limit = body.data.UserLimit;
|
|
|
|
data.remaining = body.data.UserRemaining;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2024-09-11 03:16:58 +00:00
|
|
|
async function imgurImageApi(host, post, { predata }) {
|
2024-09-11 03:16:58 +00:00
|
|
|
if (predata.remaining === 10) { // keep a buffer
|
|
|
|
throw new Error(`Reached Imgur API rate limit with source '${host.url}'`);
|
|
|
|
}
|
|
|
|
|
2024-09-11 03:16:57 +00:00
|
|
|
const res = await fetch(`https://api.imgur.com/3/image/${host.id}`, {
|
2024-09-11 03:16:57 +00:00
|
|
|
headers: {
|
|
|
|
Authorization: `Client-ID ${config.methods.imgur.clientId}`,
|
|
|
|
},
|
|
|
|
});
|
2024-09-11 03:16:57 +00:00
|
|
|
|
2024-09-11 03:16:58 +00:00
|
|
|
const rateRemaining = Number(res.headers.get('x-ratelimit-userremaining'));
|
2024-09-11 03:16:58 +00:00
|
|
|
|
2024-09-11 03:16:58 +00:00
|
|
|
if (rateRemaining) {
|
|
|
|
predata.setRemaining(rateRemaining);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!res.ok) {
|
2024-09-11 03:16:58 +00:00
|
|
|
throw new Error(`Imgur API returned HTTP ${res.status} for source '${host.url}'`);
|
|
|
|
}
|
|
|
|
|
2024-09-11 03:16:57 +00:00
|
|
|
const { data } = await res.json();
|
|
|
|
|
|
|
|
if (res.status !== 200) {
|
2024-09-11 03:16:58 +00:00
|
|
|
throw new Error(`Could not fetch info for imgur image '${host.id}': ${data.error}`);
|
2024-09-11 03:16:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
album: null,
|
|
|
|
items: [{
|
|
|
|
id: data.id,
|
|
|
|
url: data.animated ? data.mp4 : data.link,
|
|
|
|
title: data.title,
|
|
|
|
description: data.description,
|
|
|
|
type: data.animated ? 'video/mp4' : data.type,
|
|
|
|
datetime: new Date(data.datetime * 1000),
|
|
|
|
original: data,
|
|
|
|
}],
|
|
|
|
};
|
2024-09-11 03:16:57 +00:00
|
|
|
}
|
2024-09-11 03:16:56 +00:00
|
|
|
|
2024-09-11 03:16:58 +00:00
|
|
|
async function imgurImage(host, post, context) {
|
|
|
|
return imgurImageApi(host, post, context);
|
2024-09-11 03:16:56 +00:00
|
|
|
}
|
2024-09-11 03:16:53 +00:00
|
|
|
|
2024-09-11 03:16:58 +00:00
|
|
|
module.exports = {
|
|
|
|
fetchInfo: imgurImage,
|
|
|
|
fetchPredata,
|
|
|
|
};
|