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

allow twitter multipe sech terms... space for AND and , for OR. (not sure why I had knobbled it in first place...)

This commit is contained in:
Dave C-J 2013-11-15 08:59:06 +00:00
parent e700a11647
commit c17687e5db
2 changed files with 139 additions and 129 deletions

View File

@ -102,17 +102,17 @@
</script> </script>
<script type="text/x-red" data-template-name="twitter in"> <script type="text/x-red" data-template-name="twitter in">
<div class="form-row"> <div class="form-row">
<label for="node-input-twitter"><i class="icon-user"></i> Log in as</label> <label for="node-input-twitter"><i class="icon-user"></i> Log in as</label>
<input type="text" id="node-input-twitter"> <input type="text" id="node-input-twitter">
</div> </div>
<div class="form-row"> <div class="form-row">
<label for="node-input-user"><i class="icon-search"></i> Search</label> <label for="node-input-user"><i class="icon-search"></i> Search</label>
<select type="text" id="node-input-user" style="display: inline-block; vertical-align: middle; width:60%;"> <select type="text" id="node-input-user" style="display: inline-block; vertical-align: middle; width:60%;">
<option value="false">all public tweets</option> <option value="false">all public tweets</option>
<option value="true">the tweets of who you follow</option> <option value="true">the tweets of who you follow</option>
</select> </select>
</div> </div>
<div class="form-row"> <div class="form-row">
<label for="node-input-tags"><i class="icon-tags"></i> for</label> <label for="node-input-tags"><i class="icon-tags"></i> for</label>
<input type="text" id="node-input-tags" placeholder="comma-separated words, @ids, #tags"> <input type="text" id="node-input-tags" placeholder="comma-separated words, @ids, #tags">
@ -127,10 +127,11 @@
</script> </script>
<script type="text/x-red" data-help-name="twitter in"> <script type="text/x-red" data-help-name="twitter in">
<p>Twitter input node. Watches either the public or the user's stream for tweets containing the configured search term.</p> <p>Twitter input node. Watches either the public or the user's stream for tweets containing the configured search term.</p>
<p>Sets the <b>msg.topic</b> to <i>tweets/</i> and then appends the senders screen name.</p> <p>Use space for <i>and</i> and comma , for <i>or</i> when searching for multiple terms.</p>
<p>Sets <b>msg.location</b> to the tweeters location if known.</p> <p>Sets the <b>msg.topic</b> to <i>tweets/</i> and then appends the senders screen name.</p>
<p>Sets <b>msg.tweet</b> to the full tweet object as documented by <a href="https://dev.twitter.com/docs/platform-objects/tweets">Twitter</a>. <p>Sets <b>msg.location</b> to the tweeters location if known.</p>
<p>Sets <b>msg.tweet</b> to the full tweet object as documented by <a href="https://dev.twitter.com/docs/platform-objects/tweets">Twitter</a>.
</script> </script>
<script type="text/javascript"> <script type="text/javascript">
@ -157,7 +158,6 @@
</script> </script>
<script type="text/x-red" data-template-name="twitter out"> <script type="text/x-red" data-template-name="twitter out">
<div class="form-row"> <div class="form-row">
<label for="node-input-twitter"><i class="icon-user"></i> Twitter</label> <label for="node-input-twitter"><i class="icon-user"></i> Twitter</label>
@ -170,7 +170,7 @@
</script> </script>
<script type="text/x-red" data-help-name="twitter out"> <script type="text/x-red" data-help-name="twitter out">
<p>Twitter out node. Tweets the <b>msg.payload</b>.</p> <p>Twitter out node. Tweets the <b>msg.payload</b>.</p>
</script> </script>
<script type="text/javascript"> <script type="text/javascript">

View File

@ -28,7 +28,8 @@ function TwitterInNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.active = true; this.active = true;
this.user = n.user; this.user = n.user;
this.tags = n.tags.replace(/ /g,''); //this.tags = n.tags.replace(/ /g,'');
this.tags = n.tags;
this.twitter = n.twitter; this.twitter = n.twitter;
this.topic = n.topic||"tweets"; this.topic = n.topic||"tweets";
this.twitterConfig = RED.nodes.getNode(this.twitter); this.twitterConfig = RED.nodes.getNode(this.twitter);
@ -36,10 +37,10 @@ function TwitterInNode(n) {
if (credentials && credentials.screen_name == this.twitterConfig.screen_name) { if (credentials && credentials.screen_name == this.twitterConfig.screen_name) {
var twit = new ntwitter({ var twit = new ntwitter({
consumer_key: "OKjYEd1ef2bfFolV25G5nQ", consumer_key: "OKjYEd1ef2bfFolV25G5nQ",
consumer_secret: "meRsltCktVMUI8gmggpXett7WBLd1k0qidYazoML6g", consumer_secret: "meRsltCktVMUI8gmggpXett7WBLd1k0qidYazoML6g",
access_token_key: credentials.access_token, access_token_key: credentials.access_token,
access_token_secret: credentials.access_token_secret access_token_secret: credentials.access_token_secret
}); });
var node = this; var node = this;
@ -47,33 +48,48 @@ function TwitterInNode(n) {
try { try {
var thing = 'statuses/filter'; var thing = 'statuses/filter';
if (this.user == "true") { thing = 'user'; } if (this.user == "true") { thing = 'user'; }
var st = { track: [node.tags] };
var bits = node.tags.split(",");
if ((bits.length > 0) && (bits.length % 4 == 0)) {
if ((Number(bits[0]) < Number(bits[2])) && (Number(bits[1]) < Number(bits[3]))) {
st = { locations: node.tags };
}
else {
node.warn("twitter: possible bad geo area format. Should be lower-left lon,lat, upper-right lon,lat");
}
}
function setupStream() { function setupStream() {
if (node.active) { if (node.active) {
twit.stream(thing, { track: [node.tags] }, function(stream) { twit.stream(thing, st, function(stream) {
//twit.stream('user', { track: [node.tags] }, function(stream) { //console.log(st);
//twit.stream('site', { track: [node.tags] }, function(stream) { //twit.stream('user', { track: [node.tags] }, function(stream) {
//twit.stream('statuses/filter', { track: [node.tags] }, function(stream) { //twit.stream('site', { track: [node.tags] }, function(stream) {
node.stream = stream; //twit.stream('statuses/filter', { track: [node.tags] }, function(stream) {
stream.on('data', function(tweet) { node.stream = stream;
//console.log(tweet.user); stream.on('data', function(tweet) {
if (tweet.user !== undefined) { //console.log(tweet.user);
var where = tweet.user.location||""; if (tweet.user !== undefined) {
var la = tweet.lang || tweet.user.lang; var where = tweet.user.location||"";
//console.log(tweet.user.location,"=>",tweet.user.screen_name,"=>",pay); var la = tweet.lang || tweet.user.lang;
var msg = { topic:node.topic+"/"+tweet.user.screen_name, payload:tweet.text, location:where, lang:la, tweet:tweet }; //console.log(tweet.user.location,"=>",tweet.user.screen_name,"=>",pay);
node.send(msg); var msg = { topic:node.topic+"/"+tweet.user.screen_name, payload:tweet.text, location:where, lang:la, tweet:tweet };
} node.send(msg);
}); }
stream.on('error', function(tweet) { });
node.warn(tweet); stream.on('limit', function(tweet) {
setTimeout(setupStream,5000); node.log("tweet rate limit hit");
}); });
stream.on('destroy', function (response) { stream.on('error', function(tweet,rc) {
if (this.active) { node.warn(tweet);
node.warn("twitter ended unexpectedly"); setTimeout(setupStream,10000);
setTimeout(setupStream,5000); });
} stream.on('destroy', function (response) {
}); if (this.active) {
node.warn("twitter ended unexpectedly");
setTimeout(setupStream,10000);
}
});
}); });
} }
} }
@ -85,125 +101,119 @@ function TwitterInNode(n) {
} else { } else {
this.error("Invalid tag property"); this.error("Invalid tag property");
} }
} else { } else {
this.error("missing twitter credentials"); this.error("missing twitter credentials");
} }
}
this.on('close', function() {
if (this.stream) {
this.active = false;
this.stream.destroy();
}
});
}
RED.nodes.registerType("twitter in",TwitterInNode); RED.nodes.registerType("twitter in",TwitterInNode);
TwitterInNode.prototype.close = function() {
if (this.stream) {
this.active = false;
this.stream.destroy();
}
}
function TwitterOutNode(n) { function TwitterOutNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.topic = n.topic; this.topic = n.topic;
this.twitter = n.twitter; this.twitter = n.twitter;
this.twitterConfig = RED.nodes.getNode(this.twitter); this.twitterConfig = RED.nodes.getNode(this.twitter);
var credentials = RED.nodes.getCredentials(this.twitter); var credentials = RED.nodes.getCredentials(this.twitter);
var node = this; var node = this;
if (credentials && credentials.screen_name == this.twitterConfig.screen_name) { if (credentials && credentials.screen_name == this.twitterConfig.screen_name) {
var twit = new ntwitter({ var twit = new ntwitter({
consumer_key: "OKjYEd1ef2bfFolV25G5nQ", consumer_key: "OKjYEd1ef2bfFolV25G5nQ",
consumer_secret: "meRsltCktVMUI8gmggpXett7WBLd1k0qidYazoML6g", consumer_secret: "meRsltCktVMUI8gmggpXett7WBLd1k0qidYazoML6g",
access_token_key: credentials.access_token, access_token_key: credentials.access_token,
access_token_secret: credentials.access_token_secret access_token_secret: credentials.access_token_secret
}).verifyCredentials(function (err, data) { }).verifyCredentials(function (err, data) {
if (err) { if (err) {
node.error("Error verifying credentials: " + err); node.error("Error verifying credentials: " + err);
} else { } else {
node.on("input", function(msg) { node.on("input", function(msg) {
if (msg != null) { if (msg != null) {
if (msg.payload.length > 140) { if (msg.payload.length > 140) {
msg.payload = msg.payload.slice(0,139); msg.payload = msg.payload.slice(0,139);
node.warn("Tweet greater than 140 : truncated"); node.warn("Tweet greater than 140 : truncated");
} }
twit.updateStatus(msg.payload, function (err, data) { twit.updateStatus(msg.payload, function (err, data) {
if (err) node.error(err); if (err) node.error(err);
}); });
} }
}); });
} }
}); });
} }
} }
RED.nodes.registerType("twitter out",TwitterOutNode); RED.nodes.registerType("twitter out",TwitterOutNode);
var oa = new OAuth( var oa = new OAuth(
"https://api.twitter.com/oauth/request_token", "https://api.twitter.com/oauth/request_token",
"https://api.twitter.com/oauth/access_token", "https://api.twitter.com/oauth/access_token",
"OKjYEd1ef2bfFolV25G5nQ", "OKjYEd1ef2bfFolV25G5nQ",
"meRsltCktVMUI8gmggpXett7WBLd1k0qidYazoML6g", "meRsltCktVMUI8gmggpXett7WBLd1k0qidYazoML6g",
"1.0", "1.0",
null, null,
"HMAC-SHA1" "HMAC-SHA1"
); );
var credentials = {}; var credentials = {};
RED.app.get('/twitter/:id', function(req,res) { RED.app.get('/twitter/:id', function(req,res) {
var credentials = RED.nodes.getCredentials(req.params.id); var credentials = RED.nodes.getCredentials(req.params.id);
if (credentials) { if (credentials) {
res.send(JSON.stringify({sn:credentials.screen_name})); res.send(JSON.stringify({sn:credentials.screen_name}));
} else { } else {
res.send(JSON.stringify({})); res.send(JSON.stringify({}));
} }
}); });
RED.app.delete('/twitter/:id', function(req,res) { RED.app.delete('/twitter/:id', function(req,res) {
RED.nodes.deleteCredentials(req.params.id); RED.nodes.deleteCredentials(req.params.id);
res.send(200); res.send(200);
}); });
RED.app.get('/twitter/:id/auth', function(req, res){ RED.app.get('/twitter/:id/auth', function(req, res){
var credentials = {}; var credentials = {};
oa.getOAuthRequestToken({ oa.getOAuthRequestToken({
oauth_callback: req.query.callback oauth_callback: req.query.callback
},function(error, oauth_token, oauth_token_secret, results){ },function(error, oauth_token, oauth_token_secret, results){
if (error) { if (error) {
console.log(error); console.log(error);
res.send("yeah no. didn't work.") res.send("yeah no. didn't work.")
} }
else { else {
credentials.oauth_token = oauth_token; credentials.oauth_token = oauth_token;
credentials.oauth_token_secret = oauth_token_secret; credentials.oauth_token_secret = oauth_token_secret;
res.redirect('https://twitter.com/oauth/authorize?oauth_token='+oauth_token) res.redirect('https://twitter.com/oauth/authorize?oauth_token='+oauth_token)
RED.nodes.addCredentials(req.params.id,credentials); RED.nodes.addCredentials(req.params.id,credentials);
} }
}); });
}); });
RED.app.get('/twitter/:id/auth/callback', function(req, res, next){ RED.app.get('/twitter/:id/auth/callback', function(req, res, next){
var credentials = RED.nodes.getCredentials(req.params.id);
credentials.oauth_verifier = req.query.oauth_verifier;
var credentials = RED.nodes.getCredentials(req.params.id); oa.getOAuthAccessToken(
credentials.oauth_verifier = req.query.oauth_verifier; credentials.oauth_token,
credentials.token_secret,
oa.getOAuthAccessToken( credentials.oauth_verifier,
credentials.oauth_token, function(error, oauth_access_token, oauth_access_token_secret, results){
credentials.token_secret, if (error){
credentials.oauth_verifier, console.log(error);
function(error, oauth_access_token, oauth_access_token_secret, results){ res.send("yeah something broke.");
if (error){ } else {
console.log(error); credentials = {};
res.send("yeah something broke."); credentials.access_token = oauth_access_token;
} else { credentials.access_token_secret = oauth_access_token_secret;
credentials = {}; credentials.screen_name = "@"+results.screen_name;
credentials.access_token = oauth_access_token; RED.nodes.addCredentials(req.params.id,credentials);
credentials.access_token_secret = oauth_access_token_secret; res.send("<html><head></head><body>Authorised - you can close this window and return to Node-RED</body></html>");
credentials.screen_name = "@"+results.screen_name;
RED.nodes.addCredentials(req.params.id,credentials);
res.send("<html><head></head><body>Authorised - you can close this window and return to Node-RED</body></html>");
}
} }
); }
);
}); });