Example #1
0
router.get('/:query', function(req, res, next){
  twitter.search({ q: req.params.query, count: 10, result_type: "popular" }, req.session.accessToken, req.session.accessTokenSecret, function(error, data, response) {
    if (error) {
      console.log(error);
    } else {
      res.json(data.statuses);
    }
  });
});
Example #2
0
router.get('/search', function (req, res) {

    return twitter.search(
        req.query,
        req.auth.accessToken,
        req.auth.accessTokenSecret,
        function(error, data, response) {
            if (error) return res.status(error.statusCode).send("Error api GET " + req.params.section + "/" + req.params.action + ":" + JSON.stringify(error));
            res.send(data);
        }
    );

});
Example #3
0
function getTweetsAndSend(followData, collection, client)
{
    if (!followData.query) {
        return;
    }

    try {
        twitter.search(
            {q: followData.query},
            config.twitterAccessToken,
            config.twitterAccessTokenSecret,
            function (error, tweets) {
                if (error) {
                    console.log(error);
                    return;
                }

                (function (tweets, followData, collection) {
                    for (var i in tweets.statuses) {
                        (function (tweet) {
                            var id = tweet.id;

                            collection.findOne({'jid': followData.jid, 'query': followData.query, 'sent_tweets': id}, function (err, item) {
                                if (err) {
                                    console.log(err);
                                    return;
                                }

                                if (item) {
                                    return;
                                }

                                var stanza = new xmpp.Element(
                                    'message',
                                    {to: followData.jid, type: 'chat'}
                                );
                                var text = entities.decode(tweet.text);
                                stanza.c('body').t([text, tweet.user.name + ' @' + tweet.user.screen_name].join('\n'));
                                client.send(stanza);

                                collection.update({'jid': followData.jid, 'query': followData.query}, {'$push': {'sent_tweets': id}}, function () {});
                            });
                        })(tweets.statuses[i]);
                    }
                })(tweets, followData, collection);
            }
        );
    } catch (e) {
        console.log(e);
    }
}
Example #4
0
app.post('/search', function(req, res) {
  tweets = [];
  var query = encode_URI(req.body.querystring);
  var geocode = "37.781157,-122.398720,4000mi";
  var count = 100;
  twitter.search({q:query, count: count, geocode:geocode}, accToken, accTokenSecret, function (error, data, response) {
    if (error) {
      console.log(JSON.stringify(error));
    }
    else { // success!!
      extract_data(data, query, res);
    }
  });
});
Example #5
0
    collection.findOne({'jid': jid, 'query': query}, function (err, item) {
        if (err) {
            console.log(err);
            return;
        }

        if (item) {
            stanza.c('body').t('Query "' + query + '" already added');
            client.send(stanza);
            return;
        }

        twitter.search(
            {q: query},
            config.twitterAccessToken,
            config.twitterAccessTokenSecret,
            function (error, data) {
                if (error) {
                    stanza.c('body').t('Error in query. Fix it and try to add again');
                    client.send(stanza);
                    return;
                }

                var oldItems = [];
                for (var i in data.statuses) {
                    oldItems.push(data.statuses[i].id);
                }

                var data = {
                    'jid': jid,
                    'query': query,
                    'added': new Date(),
                    'sent_tweets': oldItems
                };

                collection.insert(data, {w: 1}, function (err, db) {
                    if (err) {
                        console.log(err);
                        return;
                    }

                    stanza.c('body').t('Query added');
                    client.send(stanza);
                });
            }
        );
    });
Example #6
0
function tweetsForMeme(req, res) {
  var meme = "#" + req.body.meme
  twitter.search({q: meme, count: 100, result_type: 'mixed'}, token, secret, function(error, data, response) {
    if (error) {
      console.log("ERROR:", error, response)
      var json = JSON.stringify({ message: response })
      res.statusCode = 500
      res.setHeader('Content-Type', 'application/json')
      res.end(json)
    } else {
      var topTenTweets = _.sortBy(data.statuses, function(status) {
        return status.retweet_count
      }).reverse().slice(0, 10)
      var json = JSON.stringify(topTenTweets)
      res.setHeader('Content-Type', 'application/json')
      res.end(json)
    }
  })  
}
Example #7
0
 app.post('/api/twitter', function(req, res) {
   var query = req.body.search;    // Parse our req.body for query, this is the search term from the homepage
   if ( req.session.twitter ){                                   // Make sure a twitter session exists
     if ( req.session.twitter.accessToken !== undefined ){       // Checks if Twitter access token exists
       twitter.search( { q: '%23' + query,                       // Runs node-twitter-api's search method
                         count: 10,                              // Parameters: q = twitters query, '%23' is # in the url
                         result_type: 'recent' },                // Count is how many tweets you want. Result_type is recent tweets.
                         req.session.twitter.accessToken,        // Passes session accessToken
                         req.session.twitter.accessTokenSecret,  // Passes session accessTokenSecret
                         function(err, data, response){
         // console.log('data: ', data);
         res.json(data.statuses);                                // Sends data back to front-end
       });
     } else {
       res.send(404, undefined); // Sends back undefined if you're not authorized
     }
   } else {
     res.send(404, undefined);   // Sends back undefined if you don't have a twitter session
   }
 });
Example #8
0
function main() {
    var consumerKey = process.argv[2];
    var consumerSecret = process.argv[3];
    var accessToken = process.argv[4];
    var tokenSecret = process.argv[5];

    var twitter = new twitterAPI({
        consumerKey: consumerKey,
        consumerSecret: consumerSecret});

    var sinceId = fs.readFileSync("since_id.txt").toString();
    if (!sinceId) {
        sinceId = 0;
    }

    twitter.search(
        {"q": '"bots are"',
         "since_id": sinceId,
         "result_type": "recent",
         "count": 100},
        accessToken,
        tokenSecret,
        function(error, data, response) {
            if (error) {
                console.log("something went wrong: " + util.inspect(error));
            }
            var statuses = data['statuses'];
            var descriptions = [];
            for (var i = 0; i < statuses.length; i++) {
                var thisText = statuses[i]['text'];
                // find where in the string "bots are" occurs
                var prefixIndex = thisText.indexOf("bots are ");
                if (prefixIndex != -1) {
                    // extract the rest
                    var restOfStringStart = prefixIndex + 9;
                    var desc = thisText.substring(restOfStringStart);
                    // remove a period if present
                    if (desc.endsWith(".")) {
                        desc = desc.slice(0, -1);
                    }
                    // use only clean, short descriptions
                    if (isClean(desc)) {
                        descriptions.push(desc);
                    }
                }
            }

            if (descriptions.length > 0) {
                // randomize and get up to the first three
                shuffle(descriptions);
                var firstFew = descriptions.slice(0, 3);
                var tweetText = "we are " + firstFew.join(", ");

                // update status with tweet text
                twitter.statuses("update",
                    {"status": tweetText},
                    accessToken,
                    tokenSecret,
                    function(error, data, response) {
                        if (error) {
                            console.log("error: " + util.inspect(error));
                        }
                    }
                );
            }

            // if we found any new statuses, update our since_id.txt with
            // the latest status
            if (statuses.length > 0) {
                sinceId = statuses[0].id_str;
                fs.writeFileSync("since_id.txt", sinceId, "utf8");
            }

        }
    );

}