Rinser/gather.js

150 lines
3.6 KiB
JavaScript
Raw Normal View History

2018-06-14 08:07:51 +00:00
const RssBraider = require('rss-braider');
const fs = require('fs');
2018-06-20 10:22:12 +00:00
const util = require('util');
const fsWriteFile = util.promisify(fs.writeFile);
2018-06-14 08:07:51 +00:00
const feeds = {};
const log4js = require('log4js');
const logger = log4js.getLogger();
logger.level = 'trace';
2018-06-20 10:22:12 +00:00
let timerID = 0;
2018-06-14 08:07:51 +00:00
// Pull feeds from config files: ,
// feeds.simple_test_feed = require("./config/feed").feed;
// Or define in-line
feeds.news = require('./feeds/news.json');
feeds.jobsSpecial = require('./feeds/jobs-special.json');
feeds.jobsLocal = require('./feeds/jobs-local.json');
feeds.lifestyle = require('./feeds/lifestyle.json');
feeds.paleo = require('./feeds/paleo.json');
feeds.tech = require('./feeds/tech.json');
feeds.fit = require('./feeds/fit.json');
2018-06-18 22:01:32 +00:00
feeds.bored = require('./feeds/bored.json');
2018-06-14 08:07:51 +00:00
2018-06-20 10:22:12 +00:00
const braiderOptions = {
feeds,
'indent': ' ',
'date_sort_order': 'desc', // Newest first
'log_level': 'warn',
'dedupe_fields': ['link', 'guid'],
'plugins_directories': [`${__dirname }/plugins/`]
2018-06-14 08:07:51 +00:00
};
2018-06-20 10:22:12 +00:00
const rssBraider = RssBraider.createClient(braiderOptions);
2018-06-14 08:07:51 +00:00
// Override logging level (debug, info, warn, err, off)
2018-06-20 10:22:12 +00:00
// rssBraider.logger.level('off');
2018-06-14 08:07:51 +00:00
2018-06-18 22:30:26 +00:00
function gatherV2(feedName, fileName, mode) {
return new Promise((resolve, reject) => {
2018-06-20 10:22:12 +00:00
const modeSuffix = { 'rss': '.xml', 'json': '.json' };
logger.info(`Gathering ${feedName} as ${modeSuffix[mode]}...`);
rssBraider.processFeed(feedName, mode, (err, data) => {
if (err) {
logger.error(err);
2018-06-18 23:10:50 +00:00
2018-06-20 10:22:12 +00:00
return reject(err);
}
2018-06-18 22:30:26 +00:00
2018-06-20 10:22:12 +00:00
fsWriteFile(`${__dirname}/dist/${fileName}${modeSuffix[mode]}`, data).then(function () {
return resolve(`${feedName} saved`);
});
});
2018-06-18 22:30:26 +00:00
});
}
2018-06-20 10:22:12 +00:00
function toHour(extra = 0) {
const now = new Date();
2018-06-18 22:30:26 +00:00
2018-06-20 10:22:12 +00:00
return (3600000 - (now.getTime() % 3600000)) + extra;
2018-06-14 08:07:51 +00:00
}
async function gatherXML(feedName, fileName) {
2018-06-20 10:22:12 +00:00
rssBraider.processFeed(feedName, 'rss', function (err, data) {
if (err)
2018-06-14 08:07:51 +00:00
return console.log(err);
2018-06-20 10:22:12 +00:00
console.log('Saving', `${__dirname }/dist/${ fileName }.xml`);
fs.writeFile(`${__dirname }/dist/${ fileName }.xml`, data, function (err) {
if (err)
2018-06-14 08:07:51 +00:00
return console.log(err);
2018-06-18 22:30:26 +00:00
console.log('The file was saved!');
2018-06-14 08:07:51 +00:00
});
});
}
async function main() {
2018-06-18 22:30:26 +00:00
await gatherV2('jobsSpecial', 'jobs-special', 'rss').then((d) => {
2018-06-18 23:10:50 +00:00
logger.debug(d);
2018-06-18 22:30:26 +00:00
}).catch((e) => {
logger.error(e);
});
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);
2018-06-18 22:01:32 +00:00
});
2018-06-29 07:30:17 +00:00
await gatherV2('jobsSpecial', 'jobs-special', 'json').then((d) => {
logger.debug(d);
}).catch((e) => {
logger.error(e);
});
2018-06-18 22:30:26 +00:00
// await gatherXML('jobsLocal','jobs-local');
2018-06-14 08:07:51 +00:00
}
2018-06-29 07:51:37 +00:00
function test() {
gatherV2('jobsSpecial', 'jobs-special', 'rss').then((d) => {
logger.debug(d);
}).catch((e) => {
logger.error(e);
});
}
2018-06-20 10:22:12 +00:00
function tick() {
const extra = Math.floor((Math.random() * 60) + 1) * 1000;
2018-06-14 08:07:51 +00:00
main();
2018-06-18 22:13:29 +00:00
2018-06-20 10:22:12 +00:00
timerID = setTimeout(
() => tick(),
toHour(extra)
);
}
(() => {
logger.info('Gather started...');
timerID = setTimeout(
() => tick(),
toHour()
);
2018-06-29 07:51:37 +00:00
// test();
2018-06-20 10:22:12 +00:00
})();