From fab6d0aa1cf058a2159b61d5d84dbd50806d6ba5 Mon Sep 17 00:00:00 2001 From: DebaucheryLibrarian Date: Wed, 11 Sep 2024 05:16:57 +0200 Subject: [PATCH] Reintroduced imgur API as a fallback method. --- config/default.js | 5 +++++ src/methods/imgurAlbum.js | 47 ++++++++++++++++++++++++++++++++++++++- src/methods/imgurImage.js | 36 +++++++++++++++++++++++++++--- 3 files changed, 84 insertions(+), 4 deletions(-) diff --git a/config/default.js b/config/default.js index 589b052..32fd4f1 100644 --- a/config/default.js +++ b/config/default.js @@ -67,4 +67,9 @@ module.exports = { scope: 'history identity mysubreddits read subscribe', }, }, + methods: { + imgur: { + clientId: null, + }, + }, }; diff --git a/src/methods/imgurAlbum.js b/src/methods/imgurAlbum.js index bd432d0..87e56c4 100644 --- a/src/methods/imgurAlbum.js +++ b/src/methods/imgurAlbum.js @@ -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]; diff --git a/src/methods/imgurImage.js b/src/methods/imgurImage.js index b839064..06c18cb 100644 --- a/src/methods/imgurImage.js +++ b/src/methods/imgurImage.js @@ -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];