41 lines
1.4 KiB
JavaScript
41 lines
1.4 KiB
JavaScript
'use strict';
|
|
|
|
const config = require('config');
|
|
const fetch = require('node-fetch');
|
|
const mime = require('mime-types');
|
|
|
|
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}'`);
|
|
}
|
|
|
|
const dataString = html.replace(/\s+/g, ' ').match(/}}, item:(.*)}; var PREBID_TIMEOUT/)[1];
|
|
const data = JSON.parse(dataString);
|
|
|
|
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;
|