From 04425993f20ae675b9e016f2d24c47acd202aaee Mon Sep 17 00:00:00 2001 From: Martin Donnelly Date: Mon, 18 Jun 2018 23:30:26 +0100 Subject: [PATCH] added bored --- gather.js | 78 +++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 64 insertions(+), 14 deletions(-) diff --git a/gather.js b/gather.js index b261152..686b22e 100644 --- a/gather.js +++ b/gather.js @@ -42,6 +42,31 @@ const rss_braider = RssBraider.createClient(braider_options); // Override logging level (debug, info, warn, err, off) //rss_braider.logger.level('off'); +function gatherV2(feedName, fileName, mode) { + const modeSuffix = {'rss': '.xml', 'json': '.json'}; + return new Promise((resolve, reject) => { + logger.info(`Gathering ${feedName}...`); + rss_braider.processFeed(feedName, mode, function (err, data) { + if (err) { + logger.error(err); + return reject(err); + } + + fs.writeFile(`${__dirname}/dist/${fileName}${modeSuffix[mode]}`, + data, + function (err) { + if (err) { + logger.error(err); + return reject(err); + } + + return resolve(`${feedName} saved`); + }); + }); + }); +} + + async function gather(feedName, fileName) { logger.info(`Gathering ${feedName}...`); rss_braider.processFeed(feedName, 'json', function (err, data) { @@ -86,34 +111,59 @@ async function gather(feedName, fileName) { } async function gatherXML(feedName, fileName) { - rss_braider.processFeed(feedName, 'rss', function(err, data) { + rss_braider.processFeed(feedName, 'rss', function (err, data) { if (err) { return console.log(err); } - console.log('Saving', __dirname + "/dist/" + fileName + ".xml"); - fs.writeFile(__dirname + "/dist/" + fileName + ".xml", data, function(err) { + console.log('Saving', __dirname + '/dist/' + fileName + '.xml'); + fs.writeFile(__dirname + '/dist/' + fileName + '.xml', data, function (err) { if (err) { return console.log(err); } - console.log("The file was saved!"); + console.log('The file was saved!'); }); }); } async function main() { - await gatherXML('jobsSpecial','jobs-special').then((d) => { - logger.info('### jobsSpecial done.') + await gatherV2('jobsSpecial', 'jobs-special', 'rss').then((d) => { + logger.info(d); + }).catch((e) => { + logger.error(e); }); - await gather('news', 'news'); - await gather('lifestyle', 'lifestyle'); - - await gather('paleo', 'paleo'); - await gather('tech', 'tech'); - await gather('fit', 'fit'); - await gather('bored', 'bored'); - // await gatherXML('jobsLocal','jobs-local'); + await gatherV2('news', 'news', 'json').then((d) => { + logger.info(d); + }).catch((e) => { + logger.error(e); + }); + await gatherV2('lifestyle', 'lifestyle', 'json').then((d) => { + logger.info(d); + }).catch((e) => { + logger.error(e); + }); + await gatherV2('paleo', 'paleo', 'json').then((d) => { + logger.info(d); + }).catch((e) => { + logger.error(e); + }); + await gatherV2('tech', 'tech', 'json').then((d) => { + logger.info(d); + }).catch((e) => { + logger.error(e); + }); + await gatherV2('fit', 'fit', 'json').then((d) => { + logger.info(d); + }).catch((e) => { + logger.error(e); + }); + await gatherV2('bored', 'bored', 'json').then((d) => { + logger.info(d); + }).catch((e) => { + logger.error(e); + }); + // await gatherXML('jobsLocal','jobs-local'); }