Skip to content

Instantly share code, notes, and snippets.

@agustincl
Created March 31, 2017 11:58
Show Gist options
  • Save agustincl/c3c7c109dee2c7ff9c9b57ca0ca7d147 to your computer and use it in GitHub Desktop.
Save agustincl/c3c7c109dee2c7ff9c9b57ca0ca7d147 to your computer and use it in GitHub Desktop.
Insert tweets into couchbase
from __future__ import absolute_import, print_function
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
from couchbase.bucket import Bucket
from couchbase.n1ql import N1QLQuery
import json
# Go to http://apps.twitter.com and create an app.
# The consumer key and secret will be generated for you after
consumer_key="__get_the_key__"
consumer_secret="__get_the_secrect__"
# After the step above, you will be redirected to your app's page.
# Create an access token under the the "Your access token" section
access_token="__get_the_access_token__"
access_token_secret="__get_the_token_secret__"
class StdOutListener(StreamListener):
""" A listener handles tweets that are received from the stream.
This is a basic listener that just prints received tweets to stdout.
"""
def on_data(self, data):
cb = Bucket('couchbase://10.0.10.160/default')
json_load = json.loads(data)
# print json_load
# texts = json_load['text']
idtexts = json_load['id']
# coded = texts.encode('utf-8')
idd = str(idtexts)
# cb.insert(idd, json_load)
cb.upsert(idd, json_load)
print(idd)
# print(data)
return True
def on_error(self, status):
print(status)
# print("Rate limited, sleeping for 3000")
# time.sleep(3000)
# self.disconnect()
if __name__ == '__main__':
l = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
stream = Stream(auth, l)
while True:
try:
stream.filter(track=['trump'])
except Exception:
print ("Oops! That was no valid number. Try again...")
pass
continue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment