Skip to content

Instantly share code, notes, and snippets.

@furukama
Created December 20, 2013 15:56
Show Gist options
  • Save furukama/8056754 to your computer and use it in GitHub Desktop.
Save furukama/8056754 to your computer and use it in GitHub Desktop.
Listening to the Twitter Streaming API and saving all tweets to CouchDB
import tweepy
import json
import hashlib
import re
import couchdb
from tweepy.utils import import_simplejson
#Define Database connection creds
server = "localhost:8091"
admin_username = "Administrator"
admin_password = ""
#Twitter auth stuff
#Get yours by registering an app at dev.twitter.com
consumer_key = ''
consumer_secret = ''
access_token_key = ''
access_token_secret = ''
#Define filter terms
filterTerms = ["holadiho"]
json = import_simplejson()
try:
couchclient = couchdb.Server()
except:
print "Cannot find CouchDB Server ... Exiting\n"
print "----_Stack Trace_-----\n"
raise
#Try to use the twitter bucket or else switch to use default bucket
try:
db = couchclient['mydatabase']
print "Using mydatabase bucket"
except:
db = couchclient['default']
print "Using default bucket"
#OAuth
auth1 = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth1.set_access_token(access_token_key, access_token_secret)
class StreamListener(tweepy.StreamListener):
json = import_simplejson()
def on_status(self, tweet):
print 'Ran on_status'
def on_error(self, status_code):
return False
def on_data(self, data):
if data[0].isdigit():
pass
else:
jdata = json.loads(data)
db.save(jdata)
l = StreamListener()
streamer = tweepy.Stream(auth=auth1, listener=l)
streamer.filter(track=filterTerms)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment