Created
March 12, 2013 13:20
-
-
Save marians/5142825 to your computer and use it in GitHub Desktop.
Save tweets containing certain keywords from the twitter Straming API to MongoDB
This file contains hidden or 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 tweetstream | |
| from pymongo import MongoClient | |
| # look for these words: | |
| WORDS = ['word1', 'word2'] | |
| TWITTER_USER = "" | |
| TWITTER_PASS = "" | |
| MONGO_DB = 'tweetstream' | |
| MONGO_COLLECTION = 'tweets' | |
| if __name__ == '__main__': | |
| connection = MongoClient() | |
| db = connection[MONGO_DB] | |
| collection = db[MONGO_COLLECTION] | |
| collection.ensure_index('id', unique=True) | |
| try: | |
| with tweetstream.FilterStream(TWITTER_USER, TWITTER_PASS, track=WORDS) as stream: | |
| for tweet in stream: | |
| print "From: %s (%d)\n%s" % ( | |
| tweet["user"]["screen_name"], stream.count, tweet['text']) | |
| collection.update({'id': tweet['id']}, tweet, upsert=True) | |
| except tweetstream.ConnectionError, e: | |
| print "Disconnected from twitter. Reason:", e.reason |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment