No longer relying on imgur API for albums. Removed config for imgur API. Fixed ?#0 URLs not being detected for imgur.

This commit is contained in:
DebaucheryLibrarian 2024-09-11 05:16:56 +02:00
parent dbc4c45601
commit e950a9ac54
4 changed files with 33 additions and 46 deletions

View File

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

View File

@ -25,7 +25,7 @@ const hosts = [{
}, { }, {
method: 'imgurAlbum', method: 'imgurAlbum',
label: 'imgur', 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', method: 'vidbleImage',
label: 'vidble', label: 'vidble',

View File

@ -1,46 +1,40 @@
'use strict'; 'use strict';
const util = require('util');
const config = require('config'); const config = require('config');
const fetch = require('node-fetch'); const fetch = require('node-fetch');
const mime = require('mime-types');
function imgurAlbum(post) { async function imgurAlbum(post) {
return fetch(`https://api.imgur.com/3/album/${post.host.id}`, { const res = await fetch(`https://imgur.com/a/${post.host.id}`);
headers: { const html = await res.text();
'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}'`);
}
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) { const dataString = html.replace(/\s+/g, ' ').match(/}}, item:(.*)}; var PREBID_TIMEOUT/)[1];
console.log('\x1b[36m%s\x1b[0m', `Extracting single item from album '${post.title}' - ${res.data.link}`); const data = JSON.parse(dataString);
}
return { const extract = config.library.album.extractSingleItem && data.album_images.images.length === 1;
album: extract ? null : {
id: res.data.id, return {
url: res.data.link, album: extract ? null : {
title: res.data.title, id: data.id,
description: res.data.description, url: `https://imgur.com/a/${post.host.id}`,
datetime: new Date(res.data.datetime * 1000), title: data.title,
original: res.data description: data.description,
}, datetime: new Date(data.datetime),
items: res.data.images.map(item => ({ },
extracted: extract, items: data.album_images.images.map(item => ({
id: item.id, extracted: extract,
url: item.animated ? item.mp4 : item.link, id: item.hash,
title: item.title || (extract ? res.data.title : null), url: data.animated ? `https://i.imgur.com/${item.hash}.mp4` : `https://i.imgur.com/${item.hash}${item.ext}`,
description: item.description || (extract ? res.data.description : null), title: item.title || (extract ? data.title : null),
type: item.animated ? 'video/mp4' : item.type, description: item.description || (extract ? data.description : null),
datetime: new Date(item.datetime * 1000), type: item.animated ? 'video/mp4' : mime.lookup(item.ext.split('?')[0]),
original: item datetime: new Date(item.datetime),
})) })),
}; };
}); }
};
module.exports = imgurAlbum; module.exports = imgurAlbum;

View File

@ -13,7 +13,7 @@ async function imgurImage(post) {
const dataString = html.replace(/\s+/g, ' ').match(/}}, item:(.*)}; var PREBID_TIMEOUT/)[1]; const dataString = html.replace(/\s+/g, ' ').match(/}}, item:(.*)}; var PREBID_TIMEOUT/)[1];
const data = JSON.parse(dataString); const data = JSON.parse(dataString);
const item = { return {
album: null, album: null,
items: [{ items: [{
id: data.hash, id: data.hash,
@ -24,8 +24,6 @@ async function imgurImage(post) {
datetime: new Date(data.timestamp || data.datetime), datetime: new Date(data.timestamp || data.datetime),
}], }],
}; };
return item;
} }
module.exports = imgurImage; module.exports = imgurImage;