-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from ngVenezuela/develop
Merge a master para #39
- Loading branch information
Showing
10 changed files
with
287 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,8 +13,6 @@ | |
} | ||
}], | ||
"max-len": ["error", 100], | ||
"error": { | ||
"properties": "never" | ||
} | ||
"error": 0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"eslint.enable": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
const config = { | ||
telegramToken: 'MY_SUPER_SECRET_TOKEN' | ||
telegramToken: 'MY_SUPER_SECRET_TOKEN', | ||
morningHour: 7, | ||
groupId: -1001031605415, | ||
// groupId: -165387746 //test group, | ||
goodMorningRegExp: new RegExp('buen(os)*\\sd[ií]+as', 'iu'), | ||
blogFeedUrl: 'http://ngvenezuela.org.ve/blog/feeds/all.atom.xml', | ||
}; | ||
|
||
module.exports = config; | ||
module.exports = config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"lastPubDate":"2016-09-26T21:00:00.000Z"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
'use strict'; | ||
|
||
const FeedParser = require('feedparser'); | ||
const request = require('request'); | ||
const fs = require('fs'); | ||
const config = require('../config/config'); | ||
const events = require('events'); | ||
|
||
let feedparser = new FeedParser(); | ||
let eventEmitter = new events.EventEmitter(); | ||
let articles = []; | ||
let lastPubDate; | ||
let req; | ||
|
||
setInterval(readLastPubDate, 60000 * 60 * 24); // check daily | ||
|
||
module.exports = eventEmitter; | ||
|
||
/** | ||
* Check the blog feed for entries | ||
*/ | ||
function makeRequest() { | ||
req = request(config.blogFeedUrl); | ||
|
||
/** | ||
* We listen to the error event, | ||
* in case there is an error with | ||
* the request. | ||
*/ | ||
req.on('error', (err) => { | ||
feedparser.emit('error', new Error(err)); | ||
}); | ||
|
||
/** | ||
* Listening to the response event, | ||
* if it's an error, we emit an error, otherwise | ||
* we pipe the stream to feedparser object. | ||
*/ | ||
req.on('response', (res) => { | ||
if (res.statusCode !== 200) { | ||
feedparser.emit('error', new Error('Bad status code')); | ||
} else { | ||
req.pipe(feedparser); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Read the last article publication date | ||
* from the config/last-blog-pubDate.json file. | ||
*/ | ||
function readLastPubDate() { | ||
try { | ||
let data = fs.readFileSync('./config/last-blog-pubDate.json', 'utf8'); | ||
let parsed = JSON.parse(data); | ||
lastPubDate = new Date(parsed.lastPubDate); | ||
makeRequest(); | ||
} catch (err) { | ||
feedparser.emit('error', new Error(err)); | ||
} | ||
} | ||
|
||
/** | ||
* It updates the config/last-blog-pubDate.json | ||
* file with the last article publication date | ||
*/ | ||
function lookupFinished() { | ||
if (articles.length > 0) { // assuming that first item is latest | ||
try { | ||
fs.writeFileSync( | ||
'./config/last-blog-pubDate.json', | ||
JSON.stringify({'lastPubDate': articles[0].pubDate}), | ||
'utf8' | ||
); | ||
lastPubDate = undefined; | ||
eventEmitter.emit('newArticles', articles); | ||
articles = []; | ||
} catch (err) { | ||
feedparser.emit('error', new Error('Could not save file')); | ||
} | ||
} | ||
}; | ||
|
||
/** | ||
* we print errors in console. | ||
* we capture 'lookupFinished' 'error' and | ||
* execute corresponding function. | ||
*/ | ||
feedparser.on('error', (error) => { | ||
if (error === 'lookupFinished') { | ||
lookupFinished(); | ||
} else { | ||
console.log('error emitted: ', error); | ||
} | ||
}); | ||
|
||
/** | ||
* If we get here, it means all of the | ||
* blog entries are not yet posted in | ||
* the telegram group | ||
*/ | ||
feedparser.on('end', () => { | ||
if (lastPubDate !== undefined) { | ||
feedparser.emit('error', 'lookupFinished'); | ||
} | ||
}); | ||
|
||
/** | ||
* This function is executed, every time that an | ||
* entry is found in the xml. | ||
* It is also assumed that articles are fetched | ||
* from latest to oldest. | ||
*/ | ||
feedparser.on('readable', () => { | ||
let item; | ||
|
||
if (lastPubDate !== undefined) { | ||
while (item = feedparser.read()) { | ||
let itemPubDate = new Date(item.pubDate); | ||
if (itemPubDate <= lastPubDate) { | ||
/** | ||
* This is the only way we can get | ||
* out of the loop, is not an actual error. | ||
*/ | ||
feedparser.emit('error', 'lookupFinished'); | ||
} else { | ||
articles.push({ | ||
title: item.title, | ||
author: item.author, | ||
link: item.link, | ||
pubDate: item.pubDate, | ||
}); | ||
} | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,25 @@ | ||
'use strict'; | ||
|
||
const events = require ('events'); | ||
const events = require('events'); | ||
const timeUtility = require('./time-utility.js'); | ||
let eventEmitter = new events.EventEmitter(); | ||
|
||
setInterval(emitMinuteMark, 60000); | ||
setInterval(emitMinuteMark, 60000); // 60 seconds | ||
|
||
module.exports = eventEmitter; | ||
|
||
/** | ||
* this function emits an event | ||
* with the current hour/minute & | ||
* it also emits an event when it's | ||
* a new day | ||
*/ | ||
function emitMinuteMark() { | ||
let vzlanHour = getVzlanHour(); | ||
let vzlanMinute = getVzlanMinute(); | ||
let vzlanHour = timeUtility.vzlanHour(); | ||
let vzlanMinute = timeUtility.vzlanMinute(); | ||
|
||
eventEmitter.emit('minuteMark', vzlanHour, vzlanMinute, getVzlanWeekday()); | ||
eventEmitter.emit('minuteMark', vzlanHour, vzlanMinute, timeUtility.vzlanWeekday()); | ||
if (vzlanHour === 0 && vzlanMinute == 0) { | ||
eventEmitter.emit('newDay'); | ||
} | ||
} | ||
|
||
function getVzlanHour(date = new Date()) { | ||
let vzlanTime = getVzlanTime(date); | ||
let vzlanHour = vzlanTime.getUTCHours(); // 24 hours format (0-23) | ||
|
||
return vzlanHour; | ||
} | ||
|
||
function getVzlanMinute(date = new Date()) { | ||
let vzlanTime = getVzlanTime(date); | ||
let vzlanMinute = vzlanTime.getUTCMinutes(); // 0-59 | ||
|
||
return vzlanMinute; | ||
} | ||
|
||
function getVzlanWeekday(date = new Date()) { | ||
let vzlanTime = getVzlanTime(date); | ||
let vzlanWeekDay = vzlanTime.getUTCDay(); // sunday = 0 | ||
|
||
return vzlanWeekDay; | ||
} | ||
|
||
function getVzlanTime(date) { | ||
const vzlanOffset = 4; | ||
let actualTime = new Date( | ||
Date.UTC( | ||
date.getFullYear(), | ||
date.getMonth(), | ||
date.getDate(), | ||
date.getHours(), | ||
date.getMinutes(), | ||
0,0 | ||
) | ||
); | ||
let vzlanTimeInMs = actualTime.setUTCHours(date.getUTCHours() - vzlanOffset); | ||
let vzlanTime = new Date(vzlanTimeInMs); | ||
|
||
return vzlanTime; | ||
} | ||
|
||
module.exports = { | ||
event: eventEmitter, | ||
getVzlanHour: getVzlanHour | ||
}; |
Oops, something went wrong.