ripunzel/fetchContent.js

48 lines
1.6 KiB
JavaScript

'use strict';
const fs = require('fs-extra');
const path = require('path');
const fetch = require('node-fetch');
function saveToDisk(buffer, item, index, filename, post, user) {
const stream = fs.createWriteStream(filename);
return new Promise((resolve, reject) => {
buffer.body.pipe(stream).on('error', error => {
reject(error);
}).on('finish', () => {
console.log(`Fetched and saved ${filename}`);
resolve(filename);
});
});
};
function fetchItem(item, index, post, user) {
return fetch(item.url).then(res => {
return res.ok ? res : Promise.reject(`Failed to fetch ${item.url}`);
});
};
module.exports = function(posts, user) {
return Promise.all(posts.map(post => {
const filepath = post.content.album ? `output/${user}/${post.content.album.datetime} - ${post.content.album.id} - ${post.title}/` : `output/${user}/`;
return Promise.resolve().then(() => {
if(post.content.album) {
return fs.ensureDir(filepath);
};
}).then(() => {
return Promise.all(post.content.items.map((item, index) => {
return fetchItem(item, index, post, user).then(buffer => Object.assign(item, {buffer}));
}));
}).then(items => {
return Promise.all(items.map((item, index) => {
const filename = post.content.album ? `${filepath}${index + 1} - ${item.id}.jpg` : `${filepath}${item.datetime} - ${item.id}.jpg`;
return saveToDisk(item.buffer, item, index, filename, post, user)
}));
});
}));
};