Skip to content

Commit

Permalink
Fixed main cycle and job scheduling, bare notification
Browse files Browse the repository at this point in the history
  • Loading branch information
avivace committed May 20, 2017
1 parent 14786d9 commit 929d85a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 21 deletions.
2 changes: 1 addition & 1 deletion bot.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
start: function(db, bot) {
start: function(db, bot, config) {
var HashMap = require('hashmap');

// Strings
Expand Down
49 changes: 30 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,22 @@ var request = require('request'),
zlib = require('zlib'),
schedule = require('node-schedule'),
sqlite3 = require('sqlite3').verbose(),
bot = require('./bot.js')
botUI = require('./bot.js'),
config = require('./config.js');

const TelegramBot = require('node-telegram-bot-api');
var db = new sqlite3.Database('db.sqlite');

// Telegram bot. Polling to fetch new messages
const bot = new TelegramBot(config.token, {
polling: true
});

function getFeeds() {
// Get all unique active feeds to retrieve
gf_Query = 'SELECT DISTINCT FeedURL from QUERIES where Active =\'1\'';
db.all(gf_Query, function(error, rows) {
if (rows.length == 0) console.log("No feeds")

// For each unique feed retrieved...
rows.forEach(function(row) {
Expand All @@ -25,24 +32,28 @@ function getFeeds() {
});
}

function match(title, queryKeywords, chatId) {
function match(post, queryKeywords, chatId) {

// Parse current query element into an Object
var queryKeywords = JSON.parse(queryKeywords);

// Declare the match valid in advance
var matchStillValid = true;

var title = post["rss:title"]["#"];

// For each keyword of current query test the match with Feed Title
for (var keyword in queryKeywords) {

// Build a new regular expression with current keyword to pass it "i" flag for case-insensitive check
// Build a new regular expression with current keyword to pass it
// "i" flag for case-insensitive check
var re = new RegExp(queryKeywords[keyword], 'i');

// Test the match for current keyword
var match = title.match(re) ? true : false;

// If even a single keyword does not pass the test make the entire query useless
// If even a single keyword does not pass the test make the entire
// query useless
if (match == false) {
matchStillValid = false;
}
Expand All @@ -51,9 +62,13 @@ function match(title, queryKeywords, chatId) {

// If every keyword in current query passed the test we have our match!
// Time to start with notifications!
if (matchStillValid)
{
//do stuff...
if (matchStillValid) {
// TODO: prevalid Feed-relative hardcoded values and avoid composing
// the notification message with invalid ones
console.log("matched")
var description = post["rss:description"]["#"];
var link = post["rss:link"]["#"]
bot.sendMessage(chatId, "<b>New match!</b> \n"+ title + "\n" + description + "\n" + link, { parse_mode: "HTML" })
}


Expand All @@ -67,6 +82,7 @@ function fetch(url) {
timeout: 10000,
pool: false
});
console.log("Fetching "+url)
req.setMaxListeners(50); // Some feeds do not respond without user-agent and accept headers.
req.setHeader('user-agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36');
req.setHeader('accept', 'text/html,application/xhtml+xml');
Expand All @@ -90,19 +106,16 @@ function fetch(url) {
// Select every active query (and its owner) for current feed...
var rq_Query = 'SELECT Owner, Keywords AS keywordGroup FROM QUERIES where FeedURL = \'' + url + '\' AND Active = \'1\'';
db.all(rq_Query, function(error, rows) {

feedparser.on('readable', function() {

var post;
while (post = this.read()) {

var title = post["rss:title"]["#"];

// ...and for every results...
rows.forEach(function(row) {

// ...look for match
match(title, row.keywordGroup, row.Owner);
match(post, row.keywordGroup, row.Owner);

});

Expand Down Expand Up @@ -164,12 +177,10 @@ function done(err) {
//process.exit();
}

/*
var job = schedule.scheduleJob('0 * * * * *', function(){
readSettings();
fetch(FeedURL);
});*/

// Do things
bot.start(db)
getFeeds();
// Bot up and running
botUI.start(db, bot, config)
// Run the entire thing every 5 seconds
var job = schedule.scheduleJob('*/5 * * * * *', function(){
getFeeds();
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rssnotifier",
"version": "0.0.1",
"version": "0.1",
"description": "A resident RSS scanner looking for matching entries",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 929d85a

Please sign in to comment.