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:
ThePendulum 2018-07-05 02:54:29 +02:00
parent f5382ddc37
commit 2cc15eecd0
4 changed files with 33 additions and 46 deletions

View File

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

View File

@ -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',

View File

@ -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;

View File

@ -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;