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

fix twitter node to have 1 input :-(

and tidy up statuses
This commit is contained in:
Dave Conway-Jones 2016-09-27 21:21:28 +01:00
parent 2c49832bda
commit c04ca47804
3 changed files with 85 additions and 74 deletions

View File

@ -124,7 +124,8 @@
<li>all tweets by specific users</li> <li>all tweets by specific users</li>
<li>direct messages received by the authenticated user</li> <li>direct messages received by the authenticated user</li>
</ul></p> </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 term(s) via the <code>msg.payload</code>, leave the field blank.</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 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.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>Sets <code>msg.tweet</code> to the full tweet object as documented by <a href="https://dev.twitter.com/overview/api/tweets">Twitter</a>.
@ -145,8 +146,8 @@
name: {value:""}, name: {value:""},
topic: {value:"tweets"} topic: {value:"tweets"}
}, },
inputs:0, inputs: 1,
outputs:1, outputs: 1,
icon: "twitter.png", icon: "twitter.png",
label: function() { label: function() {
if (this.name) { if (this.name) {
@ -158,7 +159,10 @@
} else if (this.user == "user") { } else if (this.user == "user") {
return this.tags+" "+this._("twitter.label.tweetslabel"); return this.tags+" "+this._("twitter.label.tweetslabel");
} }
return this.tags; else if (this.user == "true") {
return this._("twitter.label.followers");
}
return "twitter";
}, },
labelStyle: function() { labelStyle: function() {
return this.name?"node_label_italic":""; return this.name?"node_label_italic":"";
@ -181,19 +185,18 @@
$("#node-input-tags-label").html(forlabel); $("#node-input-tags-label").html(forlabel);
$("#node-input-tags").attr("placeholder",forph); $("#node-input-tags").attr("placeholder",forph);
} }
}); });
$("#node-input-user").change(); $("#node-input-user").change();
}, }//,
oneditsave: function() { // oneditsave: function() {
if ($('#node-input-tags').val() === '' && $("#node-input-user option:selected").val() != 'dm' && $("#node-input-user option:selected").val() != 'user') { // if ($('#node-input-tags').val() === '' && $("#node-input-user option:selected").val() != 'dm' && $("#node-input-user option:selected").val() != 'user') {
this.inputs=1; // this.inputs=1;
} // }
else { // else {
//set back the default state of 0 inputs // //set back the default state of 0 inputs
this.inputs=0; // this.inputs=0;
} // }
} // }
}); });
</script> </script>

View File

@ -88,59 +88,64 @@ module.exports = function(RED) {
if (this.user === "user") { if (this.user === "user") {
node.poll_ids = []; node.poll_ids = [];
node.since_ids = {}; node.since_ids = {};
node.status({});
var users = node.tags.split(","); var users = node.tags.split(",");
//if (users == '') node.warn("User option selected but no users specified"); if (users === '') { node.warn(RED._("twitter.warn.nousers")); }
if (users.length === 0) { node.warn(RED._("twitter.warn.nousers")); } //if (users.length === 0) { node.warn(RED._("twitter.warn.nousers")); }
for (var i=0; i<users.length; i++) { else {
var user = users[i].replace(" ",""); for (var i=0; i<users.length; i++) {
twit.getUserTimeline({ var user = users[i].replace(" ","");
screen_name:user, twit.getUserTimeline({
trim_user:0, screen_name:user,
count:1 trim_user:0,
},(function() { count:1
var u = user+""; },(function() {
return function(err,cb) { var u = user+"";
if (err) { return function(err,cb) {
node.error(err); if (err) {
return; node.error(err);
} return;
if (cb[0]) { }
node.since_ids[u] = cb[0].id_str; if (cb[0]) {
} else { node.since_ids[u] = cb[0].id_str;
node.since_ids[u] = '0'; } else {
} node.since_ids[u] = '0';
node.poll_ids.push(setInterval(function() { }
twit.getUserTimeline({ node.poll_ids.push(setInterval(function() {
screen_name:u, twit.getUserTimeline({
trim_user:0, screen_name:u,
since_id:node.since_ids[u] trim_user:0,
},function(err,cb) { since_id:node.since_ids[u]
if (cb) { }, function(err,cb) {
for (var t=cb.length-1;t>=0;t-=1) { if (cb) {
var tweet = cb[t]; for (var t=cb.length-1; t>=0; t-=1) {
var where = tweet.user.location; var tweet = cb[t];
var la = tweet.lang || tweet.user.lang; var where = tweet.user.location;
var msg = { topic:node.topic+"/"+tweet.user.screen_name, payload:tweet.text, lang:la, tweet:tweet }; var la = tweet.lang || tweet.user.lang;
if (where) { var msg = { topic:node.topic+"/"+tweet.user.screen_name, payload:tweet.text, lang:la, tweet:tweet };
msg.location = {place:where}; if (where) {
addLocationToTweet(msg); msg.location = {place:where};
} addLocationToTweet(msg);
node.send(msg); }
if (t === 0) { node.send(msg);
node.since_ids[u] = tweet.id_str; if (t === 0) {
node.since_ids[u] = tweet.id_str;
}
} }
} }
} if (err) {
if (err) { node.error(err);
node.error(err); }
} });
}); },60000));
},60000)); }
} }()));
}())); }
} }
} else if (this.user === "dm") { }
else if (this.user === "dm") {
node.poll_ids = []; node.poll_ids = [];
node.status({});
twit.getDirectMessages({ twit.getDirectMessages({
screen_name:node.twitterConfig.screen_name, screen_name:node.twitterConfig.screen_name,
trim_user:0, trim_user:0,
@ -183,8 +188,8 @@ module.exports = function(RED) {
}); });
},120000)); },120000));
}); });
}
} else { else {
try { try {
var thing = 'statuses/filter'; var thing = 'statuses/filter';
if (this.user === "true") { thing = 'user'; } if (this.user === "true") { thing = 'user'; }
@ -218,11 +223,13 @@ module.exports = function(RED) {
} }
}); });
stream.on('limit', function(tweet) { stream.on('limit', function(tweet) {
node.status({fill:"grey", shape:"circle", text:"Rate limiting"});
node.warn(RED._("twitter.errors.ratelimit")); node.warn(RED._("twitter.errors.ratelimit"));
}); });
stream.on('error', function(tweet,rc) { stream.on('error', function(tweet,rc) {
if (rc == 420) { if (rc == 420) {
node.warn(RED._("twitter.errors.ratelimit")); node.status({fill:"grey", shape:"dot", text:"Rate limit hit"});
//node.warn(RED._("twitter.errors.ratelimit"));
} else { } else {
node.warn(RED._("twitter.errors.streamerror",{error:tweet.toString(),rc:rc})); node.warn(RED._("twitter.errors.streamerror",{error:tweet.toString(),rc:rc}));
} }
@ -241,13 +248,14 @@ 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:"dot", text:node.tags});
setupStream(); setupStream();
} }
node.on("input", function(msg) { node.on("input", function(msg) {
if (this.tags === '') { if (this.tags === '') {
if (this.stream) { this.stream.destroy(); } if (this.stream) { this.stream.destroy(); }
if (msg.payload !== "") { if ((typeof msg.payload === "string") && (msg.payload !== "")) {
st = { track: [msg.payload] }; st = { track:[msg.payload] };
setupStream(); setupStream();
node.status({fill:"green", shape:"dot", text:msg.payload}); node.status({fill:"green", shape:"dot", text:msg.payload});
} }
@ -257,7 +265,6 @@ module.exports = function(RED) {
} }
//We shouldn't get into this state, but just incase, check for it //We shouldn't get into this state, but just incase, check for it
else { else {
// node.warn("msg.payload passed in, but tag config is not blank, defaulting to tag config");
node.status({fill:"green", shape:"dot", text:node.tags}); node.status({fill:"green", shape:"dot", text:node.tags});
} }
}); });
@ -266,7 +273,8 @@ module.exports = function(RED) {
node.error(err); node.error(err);
} }
} }
} else { }
else {
this.error(RED._("twitter.errors.missingcredentials")); this.error(RED._("twitter.errors.missingcredentials"));
} }

View File

@ -6,6 +6,7 @@
"for": "for", "for": "for",
"user": "User", "user": "User",
"dmslabel": "DMs", "dmslabel": "DMs",
"followers": "user stream tweets",
"tweetslabel": "tweets", "tweetslabel": "tweets",
"clickhere": "Click here to authenticate with Twitter." "clickhere": "Click here to authenticate with Twitter."
}, },
@ -15,11 +16,11 @@
}, },
"search": { "search": {
"public": "all public tweets", "public": "all public tweets",
"follow": "the tweets of who you follow", "follow": "the tweets of user stream",
"user": "the tweets of specific users", "user": "the tweets of specific users",
"direct": "your direct messages" "direct": "your direct messages"
}, },
"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.", "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": { "status": {
"using-geo": "Using geo location: __location__", "using-geo": "Using geo location: __location__",
"tweeting": "tweeting", "tweeting": "tweeting",
@ -27,7 +28,7 @@
}, },
"warn": { "warn": {
"nousers":"User option selected but no users specified", "nousers":"User option selected but no users specified",
"waiting":"Waiting for search term" "waiting":"Waiting to search"
}, },
"errors": { "errors": {
"ratelimit":"tweet rate limit hit", "ratelimit":"tweet rate limit hit",
@ -38,7 +39,6 @@
"truncated":"truncated tweet greater than 140 characters", "truncated":"truncated tweet greater than 140 characters",
"sendfail":"send tweet failed: __error__", "sendfail":"send tweet failed: __error__",
"nopayload":"no payload to tweet", "nopayload":"no payload to tweet",
"oauthbroke":"something in twitter oauth broke.", "oauthbroke":"something in twitter oauth broke.",
"oautherror": "<html><head></head><body><p>Something went wrong with the authentication process. The following error was returned:</p><p><b>__statusCode__</b>: __errorData__</p><p>One known cause of this type of failure is if the clock is wrong on system running Node-RED</p></body></html>", "oautherror": "<html><head></head><body><p>Something went wrong with the authentication process. The following error was returned:</p><p><b>__statusCode__</b>: __errorData__</p><p>One known cause of this type of failure is if the clock is wrong on system running Node-RED</p></body></html>",
"authorized": "<html><head></head><body><p>Authorised - you can close this window and return to Node-RED</p></body></html>" "authorized": "<html><head></head><body><p>Authorised - you can close this window and return to Node-RED</p></body></html>"