Concurrent-Jobs (#7)

Fredy 2.0.0 introduces the concept of search jobs. You can now configure multiple of these jobs running in the same instance of Fredy. Also a new API has been created 🎉
This commit is contained in:
Christian Kellner
2020-02-26 09:05:20 +01:00
committed by GitHub
parent 52a32f7453
commit 770d30af95
50 changed files with 3297 additions and 1564 deletions

View File

@@ -1,17 +1,15 @@
const config = require('../../../conf/config.json');
/**
* simply prints out the found data to the console
* @param serviceName e.g immoscout
* @param newListings an array with newly found listings
* @param notificationConfig config of this notification adapter
*/
exports.send = (serviceName, newListings) => {
return [Promise.resolve(console.info(`Found entry from service ${serviceName}:`, newListings))];
};
/**
* each integration needs to implement this method
*/
exports.enabled = () => {
return config.notification.console.enabled;
exports.send = (serviceName, newListings, notificationConfig) => {
const { enabled } = notificationConfig.console;
if (!enabled) {
return [Promise.resolve()];
}
/* eslint-disable no-console */
return [Promise.resolve(console.info(`Found entry from service ${serviceName}:`, newListings))];
/* eslint-enable no-console */
};

View File

@@ -1,54 +1,50 @@
const Slack = require('slack');
const config = require('../../../conf/config.json');
const msg = Slack.chat.postMessage;
const {token, channel} = config.notification.slack;
/**
* sends a new listing to slack
* @param serviceName e.g immoscout
* @param newListings an array with newly found listings
* @param notificationConfig config of this notification adapter
* @returns {Promise<Chat.PostMessage.Response> | void}
*/
exports.send = (serviceName, newListings) => {
return newListings.map(payload => msg({
token,
channel,
text: `*(${serviceName})* - ${payload.title}`,
"attachments": [
{
"fallback": payload.title,
"color": "#36a64f",
"title": "Link to Exposé",
"title_link": payload.link,
"fields": [
{
"title": "Price",
"value": payload.price,
"short": false
},
{
"title": "Size",
"value": payload.size,
"short": false
},
{
"title": "Address",
"value": payload.address,
"short": false
}
],
"footer": "Powered by Fredy",
ts: new Date().getTime() / 1000
}
]
})
);
};
/**
* each integration needs to implement this method
*/
exports.enabled = () => {
return config.notification.slack.enabled;
exports.send = (serviceName, newListings, notificationConfig) => {
const { token, channel, enabled } = notificationConfig.slack;
if (!enabled) {
return [Promise.resolve()];
}
return newListings.map(payload =>
msg({
token,
channel,
text: `*(${serviceName})* - ${payload.title}`,
attachments: [
{
fallback: payload.title,
color: '#36a64f',
title: 'Link to Exposé',
title_link: payload.link,
fields: [
{
title: 'Price',
value: payload.price,
short: false
},
{
title: 'Size',
value: payload.size,
short: false
},
{
title: 'Address',
value: payload.address,
short: false
}
],
footer: 'Powered by Fredy',
ts: new Date().getTime() / 1000
}
]
})
);
};

View File

@@ -1,35 +1,33 @@
const config = require('../../../conf/config.json');
const TelegramBot = require('tg-yarl');
const opts = { parse_mode: 'Markdown' };
const bot = new TelegramBot(config.notification.telegram.token);
const opts = {parse_mode: 'Markdown'};
/**
* sends new listings to telegram
* @param serviceName e.g immoscout
* @param newListings an array with newly found listings
* @param notificationConfig config of this notification adapter
* @returns {Promise<Void> | void}
*/
exports.send = (serviceName, newListings) => {
exports.send = (serviceName, newListings, notificationConfig) => {
const {enabled, token, chatId} = notificationConfig.telegram;
if (!enabled) {
return [Promise.resolve()];
}
const bot = new TelegramBot(token);
let message = `Service _${serviceName}_ found _${newListings.length}_ new listings:\n\n`;
message += newListings.map(o =>
`*${shorten(o.title.replace(/\*/g, ''), 45)}*\n` +
[o.address, o.price, o.size].join(' | ') + '\n' +
`[LINK](${o.link})\n\n`);
`*${shorten(o.title.replace(/\*/g, ''), 45)}*\n` +
[o.address, o.price, o.size].join(' | ') + '\n' +
`[LINK](${o.link})\n\n`);
return bot.sendMessage(config.notification.telegram.chatId, message, opts);
return bot.sendMessage(chatId, message, opts);
};
/**
* each integration needs to implement this method
*/
exports.enabled = () => {
return config.notification.telegram.enabled;
};
function shorten (str, len = 30) {
function shorten(str, len = 30) {
return str.length > len ? str.substring(0, len) + '...' : str;
}

View File

@@ -4,14 +4,13 @@ const path = './adapter';
/** Read every integration existing in ./adapter **/
const adapter = fs
.readdirSync('./lib/notification/adapter')
.map(integPath => require(`${path}/${integPath}`))
.filter(integration => integration.enabled());
.map(integPath => require(`${path}/${integPath}`));
if (adapter.length === 0) {
throw new Error('Please specify at least one notification provider');
}
exports.send = (serviceName, payload) => {
exports.send = (serviceName, payload, notificationConfig) => {
//this is not being used in tests, therefor adapter are always set
return adapter.map(a => a.send(serviceName, payload));
return adapter.map(a => a.send(serviceName, payload, notificationConfig));
};