Reintroduced imgur API as a fallback method.

This commit is contained in:
DebaucheryLibrarian 2024-09-11 05:16:57 +02:00
parent 0377053d5a
commit fab6d0aa1c
3 changed files with 84 additions and 4 deletions

View File

@ -67,4 +67,9 @@ module.exports = {
scope: 'history identity mysubreddits read subscribe',
},
},
methods: {
imgur: {
clientId: null,
},
},
};

View File

@ -4,12 +4,57 @@ const config = require('config');
const fetch = require('node-fetch');
const mime = require('mime-types');
async function imgurAlbumApi(post) {
return fetch(`https://api.imgur.com/3/album/${post.host.id}`, {
headers: {
Authorization: `Client-ID ${config.methods.imgur.clientId}`,
},
}).then(res => res.json()).then((res) => {
if (res.status !== 200) {
throw new Error(`Could not fetch info for imgur album '${post.host.id}': ${res.data.error} (${post.permalink})`);
}
const extract = config.library.album.extractSingleItem && res.data.images.length === 1;
if (extract) {
console.log('\x1b[36m%s\x1b[0m', `Extracting single item from album '${post.title}' - ${res.data.link}`);
}
return {
album: extract ? null : {
id: res.data.id,
url: res.data.link,
title: res.data.title,
description: res.data.description,
datetime: new Date(res.data.datetime * 1000),
original: res.data,
},
items: res.data.images.map(item => ({
extracted: extract,
id: item.id,
url: item.animated ? item.mp4 : item.link,
title: item.title || (extract ? res.data.title : null),
description: item.description || (extract ? res.data.description : null),
type: item.animated ? 'video/mp4' : item.type,
datetime: item.datetime * 1000,
original: item,
})),
};
});
}
async function imgurAlbum(post) {
const res = await fetch(`https://imgur.com/a/${post.host.id}`);
const html = await res.text();
if (res.status !== 200) {
throw new Error(`Could not fetch info for imgur image '${post.host.id}': '${res.statusText}'`);
if (config.methods.imgur.clientId) {
console.log('\x1b[31m%s\x1b[0m', `Could not fetch info for direct imgur album '${post.host.id}' (${res.statusText}), trying API fallback (${post.permalink})`);
return imgurAlbumApi(post);
}
throw new Error(`Could not fetch info for imgur album '${post.host.id}' (${res.statusText}) no API fallback configured (${post.permalink})`);
}
const dataString = html.replace(/\s+/g, ' ').match(/}}, item:(.*)}; var PREBID_TIMEOUT/)[1];

View File

@ -1,15 +1,45 @@
'use strict';
const config = require('config');
const fetch = require('node-fetch');
async function imgurImage(post) {
console.log(post.host);
async function imgurImageApi(post) {
return fetch(`https://api.imgur.com/3/image/${post.host.id}`, {
headers: {
Authorization: `Client-ID ${config.methods.imgur.clientId}`,
},
}).then(res => res.json()).then((res) => {
if (res.status !== 200) {
throw new Error(`Could not fetch info for imgur image '${post.host.id}': ${res.data.error} (${post.permalink})`);
}
return {
album: null,
items: [{
id: res.data.id,
url: res.data.animated ? res.data.mp4 : res.data.link,
title: res.data.title,
description: res.data.description,
type: res.data.animated ? 'video/mp4' : res.data.type,
datetime: new Date(res.data.datetime * 1000),
original: res.data,
}],
};
});
}
async function imgurImage(post) {
const res = await fetch(`https://imgur.com/${post.host.id}`);
const html = await res.text();
if (res.status !== 200) {
throw new Error(`Could not fetch info for imgur image '${post.host.id}': '${res.statusText}'`);
if (config.methods.imgur.clientId) {
console.log('\x1b[31m%s\x1b[0m', `Could not scrape info for imgur image '${post.host.id}' (${res.statusText}), trying API fallback (${post.permalink})`);
return imgurImageApi(post);
}
throw new Error(`Could not scrape info for imgur image '${post.host.id}' (${res.statusText}), no API fallback configured (${post.permalink})`);
}
const dataString = html.replace(/\s+/g, ' ').match(/}}, item:(.*)}; var PREBID_TIMEOUT/)[1];