Add events to Twitter node

to close #126
Thanks @ukmoose
(sorry had to do it manually)
This commit is contained in:
Dave Conway-Jones 2016-11-12 11:43:25 +00:00
parent a9d669411c
commit 1bfda975ef
5 changed files with 78 additions and 5 deletions

View File

@ -90,6 +90,7 @@
<option value="true" data-i18n="twitter.search.follow"></option>
<option value="user" data-i18n="twitter.search.user"></option>
<option value="dm" data-i18n="twitter.search.direct"></option>
<option value="event" data-i18n="twitter.search.events"></option>
</select>
</div>
<div class="form-row" id="node-input-tags-row">
@ -109,12 +110,17 @@
<li>all the tweets from accounts that the authenticated user follows</li>
<li>all tweets by specified users</li>
<li>direct messages received by the authenticated user</li>
<li>twitter events for the authenticated user</li>
</ul></p>
<p>Use space for <i>and</i> and comma , for <i>or</i> when searching for multiple terms.
If you want to pass in the search term(s) via the <code>msg.payload</code>, leave the <b>for</b> field blank.</p>
<p>Sets the <code>msg.topic</code> to <i>tweets/</i> and then appends the senders screen name.</p>
<p>Sets <code>msg.location</code> to the tweeters location if known.</p>
<p>Sets <code>msg.tweet</code> to the full tweet object as documented by <a href="https://dev.twitter.com/overview/api/tweets">Twitter</a>.
<p>When returning events it sets the <code>msg.payload</code> to the twitter event, a full list is documented by
<a href="https://dev.twitter.com/streaming/overview/messages-types#Events_event" target="_new">Twitter</a>.</p>
<p>Sets <code>msg.tweet</code> to the full tweet object as documented by <a href="https://dev.twitter.com/overview/api/tweets" target="_new">Twitter</a>.
<p><b>Note</b>: This node is not connected to the FireHose, so will not return 100% of all tweets to a busy @id or #hashtag.</p>
<p><b>Note:</b> when set to follow specific users, or your direct messages, the node is subject to
the rate limiting of the Twitter API. If you deploy the flows multiple times within a 15 minute window, you may
exceed the limit and will see errors from the node. These errors will clear automatically when the current 15
@ -143,6 +149,9 @@
var uname = RED.nodes.node(this.twitter);
if (this.user == "dm") {
return (uname?uname.label()+" ":"")+this._("twitter.label.dmslabel");
} else if (this.user == "event") {
var user = RED.nodes.node(this.twitter);
return (user?user.label()+" ":"")+this._("twitter.label.eventslabel");
} else if (this.user == "user") {
return this.tags+" "+this._("twitter.label.tweetslabel");
}
@ -165,7 +174,7 @@
$("#node-input-tags-row").show();
$("#node-input-tags-label").html(userlabel);
$("#node-input-tags").attr("placeholder",userph);
} else if ((type == "dm")||(type == "true")) {
} else if ((type == "dm")||(type == "true")||(type == "event")) {
$("#node-input-tags-row").hide();
} else {
$("#node-input-tags-row").show();

View File

@ -175,6 +175,61 @@ module.exports = function(RED) {
},120000));
});
}
else if (this.user === "event") {
try {
var thingu = 'user';
var setupEvStream = function() {
if (node.active) {
twit.stream(thingu, st, function(stream) {
node.status({fill:"green", shape:"dot", text:" "});
node.stream = stream;
stream.on('data', function(tweet) {
if (tweet.event !== undefined) {
var where = tweet.source.location;
var la = tweet.source.lang;
var msg = { topic:node.topic+"/"+tweet.source.screen_name, payload:tweet.event, lang:la, tweet:tweet };
if (where) {
msg.location = {place:where};
addLocationToTweet(msg);
}
node.send(msg);
}
});
stream.on('limit', function(tweet) {
node.status({fill:"grey", shape:"dot", text:" "});
node.tout2 = setTimeout(function() { node.status({fill:"green", shape:"dot", text:" "}); },10000);
});
stream.on('error', function(tweet,rc) {
//console.log("ERRO",rc,tweet);
if (rc == 420) {
node.status({fill:"red", shape:"ring", text:RED._("twitter.errors.ratelimit")});
} else {
node.status({fill:"red", shape:"ring", text:" "});
node.warn(RED._("twitter.errors.streamerror",{error:tweet.toString(),rc:rc}));
}
twitterRateTimeout = Date.now() + retry;
if (node.restart) {
node.tout = setTimeout(function() { setupEvStream() },retry);
}
});
stream.on('destroy', function (response) {
//console.log("DEST",response)
twitterRateTimeout = Date.now() + 15000;
if (node.restart) {
node.status({fill:"red", shape:"dot", text:" "});
node.warn(RED._("twitter.errors.unexpectedend"));
node.tout = setTimeout(function() { setupEvStream() },15000);
}
});
});
}
}
setupEvStream();
}
catch (err) {
node.error(err);
}
}
else {
try {
var thing = 'statuses/filter';

View File

@ -33,9 +33,16 @@ Sets the `msg.topic` to *tweets/* and then appends the senders screen name.
Sets `msg.location` to the tweeters location if known.
When returning events it sets the `msg.payload` to the twitter event, a full list is documented by
<a href="https://dev.twitter.com/streaming/overview/messages-types#Events_event" target="_new">Twitter</a>.
Sets `msg.tweet` to the full tweet object as documented by <a href="https://dev.twitter.com/overview/api/tweets" target="_new">Twitter</a>.
**Note**: This node is not connected to the FireHose, so will not return 100% of all tweets to a busy @id or #hashtag.
Sets `msg.tweet` to the full tweet object as documented by <a href="https://dev.twitter.com/overview/api/tweets">Twitter</a>.
**Note:** when set to a specific user's tweets, or your direct messages, the node is subject to
**Note**: when set to a specific user's tweets, or your direct messages, the node is subject to
Twitter's API rate limiting. If you deploy the flows multiple times within a 15 minute window, you may
exceed the limit and will see errors from the node. These errors will clear when the current 15 minute window
passes.

View File

@ -8,6 +8,7 @@
"dmslabel": "DMs",
"followers": "followed by",
"tweetslabel": "tweets",
"eventslabel": "events",
"clickhere": "Click here to authenticate with Twitter."
},
"placeholder": {
@ -18,7 +19,8 @@
"public": "all public tweets",
"follow": "all tweets from people I follow",
"user": "the tweets of specific users",
"direct": "your direct messages"
"direct": "your direct messages",
"events": "your twitter events"
},
"tip": "Tip: Use commas without spaces between multiple search terms. Comma = OR, Space = AND.<br/>The Twitter API WILL NOT deliver 100% of all tweets.<br/>Tweets of who you follow will include their retweets and favourites.<br/><br/>Leave <b>for</b> blank to set using msg.payload.",
"status": {

View File

@ -1,6 +1,6 @@
{
"name" : "node-red-node-twitter",
"version" : "0.1.8",
"version" : "0.1.9",
"description" : "A Node-RED node to talk to Twitter",
"dependencies" : {
"twitter-ng": "0.6.2",