Last active
December 26, 2015 18:09
-
-
Save j127/7192295 to your computer and use it in GitHub Desktop.
Expanded version of this: https://gist.github.com/j127/7191906 -- just run npm install mongoose in the terminal to install the database stuff.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Import the twit module that was downloaded with npm install twit. | |
// The module is loaded in node_modules and Node.js knows where to find it. | |
var Twit = require('twit'); | |
// Import the MongoDB connection stuff | |
var mongoose = require('mongoose'); | |
// Connect to the database - this also creates the database if it doesn't exist | |
mongoose.connect('mongodb://localhost/twittersearch'); | |
// Define the DB schema | |
var Tweet = mongoose.model('Tweet', { tweettext: String }); | |
// Settings -- put your keys here: | |
var T = new Twit({ | |
consumer_key: '...', | |
consumer_secret: '...', | |
access_token: '...', | |
access_token_secret: '...' | |
}); | |
// A method to search tweets. This will search for five JavaScript tweets from 2013. | |
T.get('search/tweets', { q: 'javascript since:2013-01-01', count: 5 }, function(err, reply) { | |
var tweet, tweets, currentTweet, i, len; | |
// Uncomment the next line to view the JSON that is returned | |
// console.log(reply); | |
// Extract the statuses | |
tweets = reply.statuses; | |
// Loop over the statuses | |
for (i = 0, len = tweets.length; i < len; i++) { | |
// Get the text | |
currentTweet = tweets[i].text; | |
// Prepare tweet for the database | |
tweet = new Tweet( { tweettext: currentTweet } ); | |
tweet.save(function (err) { | |
if (err) { | |
console.log(err); | |
} | |
// Success: | |
console.log('Tweet saved: ' + currentTweet); | |
}); | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment