1
0
mirror of https://github.com/node-red/node-red-nodes.git synced 2023-10-10 13:36:58 +02:00

better global handling of twitter rate limits

and make status messages i18n
This commit is contained in:
Dave Conway-Jones 2016-09-30 17:00:20 +01:00
parent 5d7d7e9d24
commit 8a03773567
2 changed files with 26 additions and 9 deletions

View File

@ -19,6 +19,7 @@ module.exports = function(RED) {
var Ntwitter = require('twitter-ng'); var Ntwitter = require('twitter-ng');
var OAuth= require('oauth').OAuth; var OAuth= require('oauth').OAuth;
var request = require('request'); var request = require('request');
var twitterRateTimeout;
function TwitterNode(n) { function TwitterNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
@ -197,9 +198,11 @@ module.exports = function(RED) {
var setupStream = function() { var setupStream = function() {
if (node.restart) { if (node.restart) {
node.status({fill:"green", shape:"dot", text:(tags||" ")});
twit.stream(thing, st, function(stream) { twit.stream(thing, st, function(stream) {
//console.log("ST",st); //console.log("ST",st);
node.stream = stream; node.stream = stream;
var retry = 15000; // 15 secs for general errors
stream.on('data', function(tweet) { stream.on('data', function(tweet) {
if (tweet.user !== undefined) { if (tweet.user !== undefined) {
var where = tweet.user.location; var where = tweet.user.location;
@ -214,24 +217,27 @@ module.exports = function(RED) {
} }
}); });
stream.on('limit', function(tweet) { stream.on('limit', function(tweet) {
node.status({fill:"grey", shape:"dot", text:"Rate limiting"}); node.status({fill:"grey", shape:"dot", text:RED._("twitter.errors.limitrate")});
//node.warn(RED._("twitter.errors.ratelimit")); //node.warn(RED._("twitter.errors.ratelimit"));
}); });
stream.on('error', function(tweet,rc) { stream.on('error', function(tweet,rc) {
var retry = 15000; // 15 secs for general errors
if (rc == 420) { if (rc == 420) {
node.status({fill:"red", shape:"ring", text:"Rate limit hit"}); node.status({fill:"red", shape:"ring", text:RED._("twitter.errors.ratelimit")});
retry = 60000; // 60 secs for rate limit retry = 60000; // 60 secs for rate limit
//node.warn(RED._("twitter.errors.ratelimit")); //node.warn(RED._("twitter.errors.ratelimit"));
} else { } else {
node.status({fill:"red", shape:"ring", text:"rc:"+rc});
node.warn(RED._("twitter.errors.streamerror",{error:tweet.toString(),rc:rc})); node.warn(RED._("twitter.errors.streamerror",{error:tweet.toString(),rc:rc}));
} }
twitterRateTimeout = Date.now() + retry;
if (node.restart) { if (node.restart) {
node.tout = setTimeout(function() { setupStream() },retry); node.tout = setTimeout(function() { setupStream() },retry);
} }
}); });
stream.on('destroy', function (response) { stream.on('destroy', function (response) {
twitterRateTimeout = Date.now() + retry;
if (node.restart) { if (node.restart) {
node.status({fill:"red", shape:"dot", text:" "});
node.warn(RED._("twitter.errors.unexpectedend")); node.warn(RED._("twitter.errors.unexpectedend"));
node.tout = setTimeout(function() { setupStream() },15000); node.tout = setTimeout(function() { setupStream() },15000);
} }
@ -262,7 +268,9 @@ module.exports = function(RED) {
if (this.user === "false") { if (this.user === "false") {
node.on("input", function(msg) { node.on("input", function(msg) {
if (this.tags === '') { if (this.tags === '') {
if (node.tout) { clearTimeout(node.tout); } if (node.tout) {
clearTimeout(node.tout);
}
if (this.stream) { if (this.stream) {
this.restart = false; this.restart = false;
node.stream.removeAllListeners(); node.stream.removeAllListeners();
@ -271,9 +279,17 @@ module.exports = function(RED) {
if ((typeof msg.payload === "string") && (msg.payload !== "")) { if ((typeof msg.payload === "string") && (msg.payload !== "")) {
st = { track:[msg.payload] }; st = { track:[msg.payload] };
tags = msg.payload; tags = msg.payload;
node.status({fill:"green", shape:"ring", text:tags});
this.restart = true; this.restart = true;
setupStream(); if ((twitterRateTimeout - Date.now()) > 0 ) {
node.status({fill:"red", shape:"ring", text:tags});
node.tout = setTimeout(function() {
setupStream();
}, twitterRateTimeout - Date.now() );
}
else {
setupStream();
}
} }
else { else {
node.status({fill:"yellow", shape:"ring", text:RED._("twitter.warn.waiting")}); node.status({fill:"yellow", shape:"ring", text:RED._("twitter.warn.waiting")});
@ -287,7 +303,7 @@ module.exports = function(RED) {
node.status({fill:"yellow", shape:"ring", text:RED._("twitter.warn.waiting")}); node.status({fill:"yellow", shape:"ring", text:RED._("twitter.warn.waiting")});
} }
else { else {
node.status({fill:"green", shape:"ring", text:(tags||" ")}); node.status({fill:"green", shape:"dot", text:(tags||" ")});
setupStream(); setupStream();
} }
} }

View File

@ -31,8 +31,9 @@
"waiting":"Waiting for search term" "waiting":"Waiting for search term"
}, },
"errors": { "errors": {
"ratelimit":"tweet rate limit hit", "ratelimit":"rate limit hit",
"streamerror":"stream errosr: __error__ (__rc__)", "limitrate":"limiting rate",
"streamerror":"stream error: __error__ (__rc__)",
"unexpectedend":"stream ended unexpectedly", "unexpectedend":"stream ended unexpectedly",
"invalidtag":"invalid tag property", "invalidtag":"invalid tag property",
"missingcredentials":"missing twitter credentials", "missingcredentials":"missing twitter credentials",