ripunzel/methods/imgurAlbum.js

41 lines
1.4 KiB
JavaScript

'use strict';
const util = require('util');
const config = require('config');
const fetch = require('node-fetch');
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 => {
const extract = config.patterns.album.extractSingleItem && res.data.images.length === 1;
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
}))
};
}).catch(error => {
console.error(error);
});
};
module.exports = imgurAlbum;