Skip to content

Instantly share code, notes, and snippets.

@marians
Created March 12, 2013 13:20
Show Gist options
  • Select an option

  • Save marians/5142825 to your computer and use it in GitHub Desktop.

Select an option

Save marians/5142825 to your computer and use it in GitHub Desktop.
Save tweets containing certain keywords from the twitter Straming API to MongoDB
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