2018-04-15 02:51:02 +00:00
|
|
|
'use strict';
|
|
|
|
|
2018-04-17 22:18:04 +00:00
|
|
|
const Readable = require('stream').Readable;
|
2018-04-15 02:51:02 +00:00
|
|
|
const fs = require('fs-extra');
|
|
|
|
const path = require('path');
|
2018-04-16 21:46:39 +00:00
|
|
|
const config = require('config');
|
2018-04-15 02:51:02 +00:00
|
|
|
const fetch = require('node-fetch');
|
2018-04-17 22:18:04 +00:00
|
|
|
const interpolate = require('./interpolate.js');
|
2018-04-15 02:51:02 +00:00
|
|
|
|
2018-04-17 22:18:04 +00:00
|
|
|
function saveItemToDisk(stream, filepath) {
|
|
|
|
const file = fs.createWriteStream(filepath);
|
2018-04-16 21:46:39 +00:00
|
|
|
|
2018-04-17 22:18:04 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
stream.pipe(file).on('error', error => {
|
|
|
|
reject(error);
|
|
|
|
}).on('finish', () => {
|
|
|
|
console.log(`Saved '${filepath}'`);
|
2018-04-16 21:46:39 +00:00
|
|
|
|
2018-04-17 22:18:04 +00:00
|
|
|
resolve(filepath);
|
2018-04-16 21:46:39 +00:00
|
|
|
});
|
2018-04-17 22:18:04 +00:00
|
|
|
});
|
2018-04-16 21:46:39 +00:00
|
|
|
};
|
|
|
|
|
2018-04-17 22:18:04 +00:00
|
|
|
function textPostToStream(item) {
|
|
|
|
const stream = new Readable();
|
2018-04-15 02:51:02 +00:00
|
|
|
|
2018-04-17 22:18:04 +00:00
|
|
|
stream.push(item.text);
|
|
|
|
stream.push(null);
|
2018-04-15 02:51:02 +00:00
|
|
|
|
2018-04-17 22:18:04 +00:00
|
|
|
return Object.assign(item, {
|
|
|
|
stream: stream
|
2018-04-15 02:51:02 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-04-17 23:55:20 +00:00
|
|
|
function fetchItem(item, post, attempt) {
|
2018-04-16 21:46:39 +00:00
|
|
|
function retry(error) {
|
|
|
|
console.log(error);
|
|
|
|
|
|
|
|
if(attempt < 3) {
|
|
|
|
console.log('Retrying...');
|
|
|
|
|
2018-04-17 23:55:20 +00:00
|
|
|
return fetchItem(item, post, ++attempt);
|
2018-04-16 21:46:39 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-04-15 02:51:02 +00:00
|
|
|
return fetch(item.url).then(res => {
|
|
|
|
return res.ok ? res : Promise.reject(`Failed to fetch ${item.url}`);
|
2018-04-16 21:46:39 +00:00
|
|
|
}).then(res => {
|
|
|
|
console.log(`Fetched '${item.url}'`);
|
|
|
|
|
2018-04-17 22:18:04 +00:00
|
|
|
return Object.assign({}, item, {
|
|
|
|
stream: res.body
|
|
|
|
});
|
2018-04-16 21:46:39 +00:00
|
|
|
}).catch(retry);
|
2018-04-15 02:51:02 +00:00
|
|
|
};
|
|
|
|
|
2018-04-16 21:46:39 +00:00
|
|
|
module.exports = function(posts) {
|
2018-04-15 02:51:02 +00:00
|
|
|
return Promise.all(posts.map(post => {
|
|
|
|
return Promise.resolve().then(() => {
|
|
|
|
return Promise.all(post.content.items.map((item, index) => {
|
2018-04-17 23:55:20 +00:00
|
|
|
item.index = index;
|
|
|
|
|
2018-04-17 22:18:04 +00:00
|
|
|
if(item.self) {
|
|
|
|
return textPostToStream(item);
|
|
|
|
}
|
|
|
|
|
2018-04-17 23:55:20 +00:00
|
|
|
return fetchItem(item, post, 0);
|
2018-04-15 02:51:02 +00:00
|
|
|
}));
|
|
|
|
}).then(items => {
|
2018-04-17 23:55:20 +00:00
|
|
|
return Promise.all(items.map(item => {
|
2018-04-16 21:46:39 +00:00
|
|
|
const type = item.type.split('/')[0];
|
2018-04-17 23:55:20 +00:00
|
|
|
const filepath = post.content.album ? interpolate(config.patterns.album[type], post, item) : interpolate(config.patterns[type], post, item);
|
2018-04-15 02:51:02 +00:00
|
|
|
|
2018-04-16 21:46:39 +00:00
|
|
|
return Promise.resolve().then(() => {
|
|
|
|
return fs.ensureDir(path.dirname(filepath));
|
|
|
|
}).then(() => {
|
2018-04-17 22:18:04 +00:00
|
|
|
return saveItemToDisk(item.stream, filepath)
|
2018-04-16 21:46:39 +00:00
|
|
|
});
|
2018-04-15 02:51:02 +00:00
|
|
|
}));
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
};
|