2013-09-05 16:02:48 +02:00
|
|
|
/**
|
|
|
|
* Copyright 2013 IBM Corp.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
|
|
|
|
2013-11-14 16:44:54 +01:00
|
|
|
var RED = require(process.env.NODE_RED_HOME+"/red/red");
|
2013-09-05 16:02:48 +02:00
|
|
|
var ntwitter = require('ntwitter');
|
|
|
|
var OAuth= require('oauth').OAuth;
|
|
|
|
|
|
|
|
function TwitterNode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.screen_name = n.screen_name;
|
|
|
|
}
|
|
|
|
RED.nodes.registerType("twitter-credentials",TwitterNode);
|
|
|
|
|
|
|
|
function TwitterInNode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.active = true;
|
2013-10-03 00:37:26 +02:00
|
|
|
this.user = n.user;
|
2013-11-15 09:59:06 +01:00
|
|
|
//this.tags = n.tags.replace(/ /g,'');
|
|
|
|
this.tags = n.tags;
|
2013-09-05 16:02:48 +02:00
|
|
|
this.twitter = n.twitter;
|
2013-10-09 21:18:09 +02:00
|
|
|
this.topic = n.topic||"tweets";
|
2013-09-05 16:02:48 +02:00
|
|
|
this.twitterConfig = RED.nodes.getNode(this.twitter);
|
|
|
|
var credentials = RED.nodes.getCredentials(this.twitter);
|
|
|
|
|
|
|
|
if (credentials && credentials.screen_name == this.twitterConfig.screen_name) {
|
|
|
|
var twit = new ntwitter({
|
2013-11-15 09:59:06 +01:00
|
|
|
consumer_key: "OKjYEd1ef2bfFolV25G5nQ",
|
|
|
|
consumer_secret: "meRsltCktVMUI8gmggpXett7WBLd1k0qidYazoML6g",
|
|
|
|
access_token_key: credentials.access_token,
|
|
|
|
access_token_secret: credentials.access_token_secret
|
2013-09-05 16:02:48 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
var node = this;
|
2013-11-23 00:07:08 +01:00
|
|
|
if (this.user === "user") {
|
|
|
|
node.poll_ids = [];
|
|
|
|
node.since_ids = {};
|
|
|
|
var users = node.tags.split(",");
|
|
|
|
for (var i=0;i<users.length;i++) {
|
|
|
|
var user = users[i].replace(" ","");
|
|
|
|
twit.getUserTimeline({
|
|
|
|
screen_name:user,
|
|
|
|
trim_user:0,
|
|
|
|
count:1
|
|
|
|
},function() {
|
|
|
|
var u = user+"";
|
|
|
|
return function(err,cb) {
|
2013-11-23 22:48:17 +01:00
|
|
|
if (err) {
|
|
|
|
node.error(err);
|
|
|
|
return;
|
|
|
|
}
|
2013-11-23 00:07:08 +01:00
|
|
|
if (cb[0]) {
|
|
|
|
node.since_ids[u] = cb[0].id_str;
|
|
|
|
} else {
|
|
|
|
node.since_ids[u] = '0';
|
|
|
|
}
|
|
|
|
node.poll_ids.push(setInterval(function() {
|
|
|
|
twit.getUserTimeline({
|
|
|
|
screen_name:u,
|
|
|
|
trim_user:0,
|
|
|
|
since_id:node.since_ids[u]
|
|
|
|
},function(err,cb) {
|
|
|
|
if (cb) {
|
|
|
|
for (var t=cb.length-1;t>=0;t-=1) {
|
|
|
|
var tweet = cb[t];
|
|
|
|
var where = tweet.user.location||"";
|
|
|
|
var la = tweet.lang || tweet.user.lang;
|
|
|
|
//console.log(tweet.user.location,"=>",tweet.user.screen_name,"=>",pay);
|
|
|
|
var msg = { topic:node.topic+"/"+tweet.user.screen_name, payload:tweet.text, location:where, lang:la, tweet:tweet };
|
|
|
|
node.send(msg);
|
|
|
|
if (t == 0) {
|
|
|
|
node.since_ids[u] = tweet.id_str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (err) {
|
|
|
|
node.error(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},60000));
|
|
|
|
}
|
|
|
|
}());
|
|
|
|
}
|
2013-11-23 22:48:17 +01:00
|
|
|
} else if (this.user === "dm") {
|
|
|
|
node.poll_ids = [];
|
|
|
|
twit.getDirectMessages({
|
|
|
|
screen_name:node.twitterConfig.screen_name,
|
|
|
|
trim_user:0,
|
|
|
|
count:1
|
|
|
|
},function(err,cb) {
|
|
|
|
if (err) {
|
|
|
|
node.error(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (cb[0]) {
|
|
|
|
node.since_id = cb[0].id_str;
|
|
|
|
} else {
|
|
|
|
node.since_id = '0';
|
|
|
|
}
|
|
|
|
node.poll_ids.push(setInterval(function() {
|
|
|
|
twit.getDirectMessages({
|
|
|
|
screen_name:node.twitterConfig.screen_name,
|
|
|
|
trim_user:0,
|
|
|
|
since_id:node.since_id
|
|
|
|
},function(err,cb) {
|
|
|
|
if (cb) {
|
|
|
|
for (var t=cb.length-1;t>=0;t-=1) {
|
|
|
|
var tweet = cb[t];
|
|
|
|
var where = tweet.user.location||"";
|
|
|
|
var la = tweet.lang || tweet.user.lang;
|
|
|
|
//console.log(tweet.user.location,"=>",tweet.user.screen_name,"=>",pay);
|
|
|
|
var msg = { topic:node.topic+"/"+tweet.user.screen_name, payload:tweet.text, location:where, lang:la, tweet:tweet };
|
|
|
|
node.send(msg);
|
|
|
|
if (t == 0) {
|
|
|
|
node.since_id = tweet.id_str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (err) {
|
|
|
|
node.error(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},65000));
|
|
|
|
});
|
|
|
|
|
2013-11-23 00:07:08 +01:00
|
|
|
} else if (this.tags !== "") {
|
2013-09-05 16:02:48 +02:00
|
|
|
try {
|
|
|
|
var thing = 'statuses/filter';
|
2013-11-23 00:07:08 +01:00
|
|
|
if (this.user === "true") { thing = 'user'; }
|
2013-11-15 09:59:06 +01:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-05 16:02:48 +02:00
|
|
|
function setupStream() {
|
|
|
|
if (node.active) {
|
2013-11-15 09:59:06 +01:00
|
|
|
twit.stream(thing, st, function(stream) {
|
|
|
|
//console.log(st);
|
|
|
|
//twit.stream('user', { track: [node.tags] }, function(stream) {
|
|
|
|
//twit.stream('site', { track: [node.tags] }, function(stream) {
|
|
|
|
//twit.stream('statuses/filter', { track: [node.tags] }, function(stream) {
|
|
|
|
node.stream = stream;
|
|
|
|
stream.on('data', function(tweet) {
|
|
|
|
//console.log(tweet.user);
|
|
|
|
if (tweet.user !== undefined) {
|
|
|
|
var where = tweet.user.location||"";
|
|
|
|
var la = tweet.lang || tweet.user.lang;
|
|
|
|
//console.log(tweet.user.location,"=>",tweet.user.screen_name,"=>",pay);
|
|
|
|
var msg = { topic:node.topic+"/"+tweet.user.screen_name, payload:tweet.text, location:where, lang:la, tweet:tweet };
|
|
|
|
node.send(msg);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
stream.on('limit', function(tweet) {
|
|
|
|
node.log("tweet rate limit hit");
|
|
|
|
});
|
|
|
|
stream.on('error', function(tweet,rc) {
|
|
|
|
node.warn(tweet);
|
|
|
|
setTimeout(setupStream,10000);
|
|
|
|
});
|
|
|
|
stream.on('destroy', function (response) {
|
|
|
|
if (this.active) {
|
|
|
|
node.warn("twitter ended unexpectedly");
|
|
|
|
setTimeout(setupStream,10000);
|
|
|
|
}
|
|
|
|
});
|
2013-09-05 16:02:48 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setupStream();
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
node.error(err);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.error("Invalid tag property");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.error("missing twitter credentials");
|
|
|
|
}
|
|
|
|
|
2013-11-15 09:59:06 +01:00
|
|
|
this.on('close', function() {
|
|
|
|
if (this.stream) {
|
|
|
|
this.active = false;
|
|
|
|
this.stream.destroy();
|
|
|
|
}
|
2013-11-23 00:07:08 +01:00
|
|
|
if (this.poll_ids) {
|
|
|
|
for (var i=0;i<this.poll_ids.length;i++) {
|
|
|
|
clearInterval(this.poll_ids[i]);
|
|
|
|
}
|
|
|
|
}
|
2013-11-15 09:59:06 +01:00
|
|
|
});
|
|
|
|
}
|
2013-09-05 16:02:48 +02:00
|
|
|
RED.nodes.registerType("twitter in",TwitterInNode);
|
|
|
|
|
|
|
|
|
|
|
|
function TwitterOutNode(n) {
|
2013-11-15 09:59:06 +01:00
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.topic = n.topic;
|
|
|
|
this.twitter = n.twitter;
|
|
|
|
this.twitterConfig = RED.nodes.getNode(this.twitter);
|
2013-09-05 16:02:48 +02:00
|
|
|
var credentials = RED.nodes.getCredentials(this.twitter);
|
|
|
|
var node = this;
|
|
|
|
|
|
|
|
if (credentials && credentials.screen_name == this.twitterConfig.screen_name) {
|
|
|
|
var twit = new ntwitter({
|
2013-11-15 09:59:06 +01:00
|
|
|
consumer_key: "OKjYEd1ef2bfFolV25G5nQ",
|
|
|
|
consumer_secret: "meRsltCktVMUI8gmggpXett7WBLd1k0qidYazoML6g",
|
|
|
|
access_token_key: credentials.access_token,
|
|
|
|
access_token_secret: credentials.access_token_secret
|
2013-09-05 16:02:48 +02:00
|
|
|
}).verifyCredentials(function (err, data) {
|
2013-11-15 09:59:06 +01:00
|
|
|
if (err) {
|
|
|
|
node.error("Error verifying credentials: " + err);
|
|
|
|
} else {
|
|
|
|
node.on("input", function(msg) {
|
|
|
|
if (msg != null) {
|
|
|
|
if (msg.payload.length > 140) {
|
|
|
|
msg.payload = msg.payload.slice(0,139);
|
|
|
|
node.warn("Tweet greater than 140 : truncated");
|
|
|
|
}
|
|
|
|
twit.updateStatus(msg.payload, function (err, data) {
|
|
|
|
if (err) node.error(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2013-09-05 16:02:48 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RED.nodes.registerType("twitter out",TwitterOutNode);
|
|
|
|
|
|
|
|
var oa = new OAuth(
|
2013-11-15 09:59:06 +01:00
|
|
|
"https://api.twitter.com/oauth/request_token",
|
|
|
|
"https://api.twitter.com/oauth/access_token",
|
|
|
|
"OKjYEd1ef2bfFolV25G5nQ",
|
|
|
|
"meRsltCktVMUI8gmggpXett7WBLd1k0qidYazoML6g",
|
|
|
|
"1.0",
|
|
|
|
null,
|
|
|
|
"HMAC-SHA1"
|
2013-09-05 16:02:48 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
var credentials = {};
|
|
|
|
|
|
|
|
RED.app.get('/twitter/:id', function(req,res) {
|
2013-11-15 09:59:06 +01:00
|
|
|
var credentials = RED.nodes.getCredentials(req.params.id);
|
|
|
|
if (credentials) {
|
|
|
|
res.send(JSON.stringify({sn:credentials.screen_name}));
|
|
|
|
} else {
|
|
|
|
res.send(JSON.stringify({}));
|
|
|
|
}
|
2013-09-05 16:02:48 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
RED.app.delete('/twitter/:id', function(req,res) {
|
2013-11-15 09:59:06 +01:00
|
|
|
RED.nodes.deleteCredentials(req.params.id);
|
|
|
|
res.send(200);
|
2013-09-05 16:02:48 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
RED.app.get('/twitter/:id/auth', function(req, res){
|
2013-11-15 09:59:06 +01:00
|
|
|
var credentials = {};
|
|
|
|
oa.getOAuthRequestToken({
|
|
|
|
oauth_callback: req.query.callback
|
|
|
|
},function(error, oauth_token, oauth_token_secret, results){
|
|
|
|
if (error) {
|
|
|
|
console.log(error);
|
|
|
|
res.send("yeah no. didn't work.")
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
credentials.oauth_token = oauth_token;
|
|
|
|
credentials.oauth_token_secret = oauth_token_secret;
|
|
|
|
res.redirect('https://twitter.com/oauth/authorize?oauth_token='+oauth_token)
|
|
|
|
RED.nodes.addCredentials(req.params.id,credentials);
|
|
|
|
}
|
|
|
|
});
|
2013-09-05 16:02:48 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
RED.app.get('/twitter/:id/auth/callback', function(req, res, next){
|
2013-11-15 09:59:06 +01:00
|
|
|
var credentials = RED.nodes.getCredentials(req.params.id);
|
|
|
|
credentials.oauth_verifier = req.query.oauth_verifier;
|
|
|
|
|
|
|
|
oa.getOAuthAccessToken(
|
|
|
|
credentials.oauth_token,
|
|
|
|
credentials.token_secret,
|
|
|
|
credentials.oauth_verifier,
|
|
|
|
function(error, oauth_access_token, oauth_access_token_secret, results){
|
|
|
|
if (error){
|
|
|
|
console.log(error);
|
|
|
|
res.send("yeah something broke.");
|
|
|
|
} else {
|
|
|
|
credentials = {};
|
|
|
|
credentials.access_token = oauth_access_token;
|
|
|
|
credentials.access_token_secret = oauth_access_token_secret;
|
|
|
|
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>");
|
2013-09-05 16:02:48 +02:00
|
|
|
}
|
2013-11-15 09:59:06 +01:00
|
|
|
}
|
|
|
|
);
|
2013-09-05 16:02:48 +02:00
|
|
|
});
|