From e950a9ac5451a224c8e1134ad695348a04c8ef4a Mon Sep 17 00:00:00 2001 From: DebaucheryLibrarian Date: Wed, 11 Sep 2024 05:16:56 +0200 Subject: [PATCH] No longer relying on imgur API for albums. Removed config for imgur API. Fixed ?#0 URLs not being detected for imgur. --- config/default.js | 5 --- src/dissectLink.js | 2 +- src/methods/imgurAlbum.js | 68 ++++++++++++++++++--------------------- src/methods/imgurImage.js | 4 +-- 4 files changed, 33 insertions(+), 46 deletions(-) diff --git a/config/default.js b/config/default.js index 37355f5..589b052 100644 --- a/config/default.js +++ b/config/default.js @@ -67,9 +67,4 @@ module.exports = { scope: 'history identity mysubreddits read subscribe', }, }, - methods: { - imgur: { - clientId: '1234567abcdefgh', - }, - }, }; diff --git a/src/dissectLink.js b/src/dissectLink.js index f755b20..cb12f58 100644 --- a/src/dissectLink.js +++ b/src/dissectLink.js @@ -25,7 +25,7 @@ const hosts = [{ }, { method: 'imgurAlbum', label: 'imgur', - pattern: new urlPattern('http(s)\\://(:subdomain.)imgur.com/:type/:id') + pattern: new urlPattern('http(s)\\://(:subdomain.)imgur.com/:type/:id(#:focus)') }, { method: 'vidbleImage', label: 'vidble', diff --git a/src/methods/imgurAlbum.js b/src/methods/imgurAlbum.js index fbcccec..d0877b5 100644 --- a/src/methods/imgurAlbum.js +++ b/src/methods/imgurAlbum.js @@ -1,46 +1,40 @@ - 'use strict'; +'use strict'; -const util = require('util'); const config = require('config'); const fetch = require('node-fetch'); +const mime = require('mime-types'); -function imgurAlbum(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}'`); - } +async function imgurAlbum(post) { + const res = await fetch(`https://imgur.com/a/${post.host.id}`); + const html = await res.text(); - const extract = config.library.album.extractSingleItem && res.data.images.length === 1; + if (res.status !== 200) { + throw new Error(`Could not fetch info for imgur image '${post.host.id}': '${res.data.error}'`); + } - if(extract) { - console.log('\x1b[36m%s\x1b[0m', `Extracting single item from album '${post.title}' - ${res.data.link}`); - } + const dataString = html.replace(/\s+/g, ' ').match(/}}, item:(.*)}; var PREBID_TIMEOUT/)[1]; + const data = JSON.parse(dataString); - 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: new Date(item.datetime * 1000), - original: item - })) - }; - }); -}; + const extract = config.library.album.extractSingleItem && data.album_images.images.length === 1; + + return { + album: extract ? null : { + id: data.id, + url: `https://imgur.com/a/${post.host.id}`, + title: data.title, + description: data.description, + datetime: new Date(data.datetime), + }, + items: data.album_images.images.map(item => ({ + extracted: extract, + id: item.hash, + url: data.animated ? `https://i.imgur.com/${item.hash}.mp4` : `https://i.imgur.com/${item.hash}${item.ext}`, + title: item.title || (extract ? data.title : null), + description: item.description || (extract ? data.description : null), + type: item.animated ? 'video/mp4' : mime.lookup(item.ext.split('?')[0]), + datetime: new Date(item.datetime), + })), + }; +} module.exports = imgurAlbum; diff --git a/src/methods/imgurImage.js b/src/methods/imgurImage.js index 10f87cb..1442ccd 100644 --- a/src/methods/imgurImage.js +++ b/src/methods/imgurImage.js @@ -13,7 +13,7 @@ async function imgurImage(post) { const dataString = html.replace(/\s+/g, ' ').match(/}}, item:(.*)}; var PREBID_TIMEOUT/)[1]; const data = JSON.parse(dataString); - const item = { + return { album: null, items: [{ id: data.hash, @@ -24,8 +24,6 @@ async function imgurImage(post) { datetime: new Date(data.timestamp || data.datetime), }], }; - - return item; } module.exports = imgurImage;