-
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 #81 from ngVenezuela/develop
Se actualiza rama master desde develop
- Loading branch information
Showing
9 changed files
with
173 additions
and
3 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 |
---|---|---|
|
@@ -42,3 +42,9 @@ config.js | |
|
||
# Webstorm config files | ||
.idea | ||
|
||
# JShint config file | ||
.jshintrc | ||
|
||
# Vscode config files | ||
.vscode |
This file was deleted.
Oops, something went wrong.
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,2 @@ | ||
# File that save the last tracked tweet id | ||
/last-tweetId.json |
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
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,114 @@ | ||
const fs = require('fs'); | ||
const events = require('events'); | ||
const Twitter = require('twitter'); | ||
|
||
const eventEmitter = new events.EventEmitter(); | ||
const twitterFeed = require('./../../config/config').twitterFeed; | ||
|
||
const twitterTokens = twitterFeed.auth; | ||
const twitterAccount = twitterFeed.twitterAccount; | ||
const twitterClient = new Twitter(twitterTokens); | ||
|
||
|
||
/** | ||
* Get the last tracked tweet id registered int the file: last-tweetId.json. | ||
* | ||
* Note: If there is no file then create it and return false | ||
* | ||
* @return {number} Last Tracked Tweet Id | ||
* OR {boolean} false - If there is no last-tweetId.json file | ||
*/ | ||
function getLastTrackedTweetId() { | ||
try { | ||
// Read the last-tweetId.json to get the last tracked tweet id | ||
const data = fs.readFileSync('./config/last-tweetId.json', 'utf8'); | ||
const parsed = JSON.parse(data); | ||
|
||
return parsed.lastTweetId; | ||
} catch (err) { | ||
/** | ||
* If the file doesn't exist, create the file and return false because there is no last | ||
* tweet tracked | ||
*/ | ||
if (err.code === 'ENOENT') { | ||
fs.writeFileSync('./config/last-tweetId.json', ''); | ||
return false; | ||
} | ||
throw err; | ||
} | ||
} | ||
|
||
/** | ||
* Save the last tweet id sent by ngVenezuela in the last-tweetId.json file | ||
* | ||
* @param {number} lastTweetId Last tweet id sent by ngVenezuela | ||
*/ | ||
function updateLastTrackedTweetId(lastTweetId) { | ||
fs.writeFileSync('./config/last-tweetId.json', `{"lastTweetId":${lastTweetId}}`); | ||
} | ||
|
||
/** | ||
* Callback function when gettting latest tweets since the last tracked tweet registered | ||
* | ||
* @param {error} error | ||
* @param {array} tweets Array of tweets object | ||
* @param {response} response HTTP response | ||
*/ | ||
function getLatestTweets(error, tweets) { | ||
// If there is no tweets then do nothing | ||
if (tweets.length === 0) { | ||
return; | ||
} | ||
|
||
/** | ||
* If the last tweet id is the same that the last tracked tweet id, do nothing | ||
* | ||
* Note: The Twitter API has a strange behavior, when you use | ||
* the parameter since_id equal to the most | ||
* recent tweet, it brings you the same tweet... | ||
* But, when you use since_id equal to an older tweet, it doesn't include the since_id tweet | ||
*/ | ||
if (getLastTrackedTweetId() === tweets[0].id) { | ||
return; | ||
} | ||
|
||
// Update the last tracked tweet id with the last tweet gotten | ||
updateLastTrackedTweetId(tweets[0].id); | ||
tweets.map(tweet => eventEmitter.emit('newTweet', tweet)); | ||
} | ||
|
||
/** | ||
* Check if there are new tweets from ngVenezuela account since the last tracked tweet. | ||
* | ||
* If there is no last tracked tweet registered, save the last tweet id sent by ngVenezuela | ||
* in the last-tweetId.json file | ||
*/ | ||
function checkNewTweets() { | ||
// Get the last tracked tweet id | ||
const lastTrackedTweetId = getLastTrackedTweetId(); | ||
|
||
if (lastTrackedTweetId) { | ||
twitterClient.get( | ||
'statuses/user_timeline', | ||
{ screen_name: twitterAccount, since_id: lastTrackedTweetId }, | ||
getLatestTweets | ||
); | ||
} else { | ||
// No last tracked tweet registered | ||
// Get latest tweets since the last tracked tweet registered | ||
twitterClient.get( | ||
'statuses/user_timeline', | ||
{ screen_name: twitterAccount, count: 1 }, | ||
(error, tweets) => { | ||
const lastNgTweetId = tweets[0].id; | ||
updateLastTrackedTweetId(lastNgTweetId); | ||
} | ||
); | ||
} | ||
} | ||
|
||
if (twitterFeed) { | ||
setInterval(checkNewTweets, 60 * 1000); // 60 seconds | ||
} | ||
|
||
module.exports = eventEmitter; |
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,27 @@ | ||
const config = require('./../../config/config'); | ||
const sendMessage = require('./../utils/send-message'); | ||
const newTweetMessage = require('./../../config/messages').newTweet; | ||
const hashtagMessage = require('./../../config/config').twitterFeed.hashtagMessage; | ||
|
||
/** | ||
* Construct and send the message to notify a new tweet in the group | ||
* | ||
* @param {Object} bot Telegram bot | ||
* @param {Object} tweet New tweet | ||
*/ | ||
function sendNewTweet(bot, tweet) { | ||
const tweetUrl = `https://twitter.com/${tweet.user.screen_name}/status/${tweet.id_str}`; | ||
|
||
sendMessage( | ||
bot, | ||
config.groupId, | ||
newTweetMessage | ||
.replace('#{tweetText}', tweet.text) | ||
.replace('#{tweetUrl}', tweetUrl) | ||
.replace('#{hashtagMessage}', hashtagMessage) | ||
); | ||
} | ||
|
||
module.exports = { | ||
sendNewTweet | ||
}; |