Skip to content

Instantly share code, notes, and snippets.

@szolotykh
Created May 23, 2014 05:22
Show Gist options
  • Save szolotykh/058c2e424f48f45a9fd4 to your computer and use it in GitHub Desktop.
Save szolotykh/058c2e424f48f45a9fd4 to your computer and use it in GitHub Desktop.
Tweets streaming
import tweepy
import os
import json
# Consumer keys and access tokens
consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''
class CustomStreamListener(tweepy.StreamListener):
def __init__(self, api):
self.api = api
def on_data(self, tweet):
jTweet = json.loads(tweet)
text = jTweet['text'].encode('ascii', 'ignore')
print text
print "--------------------------------------------"
return True
def on_error(self, status_code):
return True
def on_timeout(self):
return True
if __name__ == '__main__':
# OAuth process
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
# Creation of the actual interface
api = tweepy.API(auth)
# Creates the user object
user = api.me()
print('Name: ' + user.name)
print('Location: ' + user.location)
print('Friends: ' + str(user.friends_count))
sapi = tweepy.streaming.Stream(auth, CustomStreamListener(api))
try:
sapi.filter(track=['Russia'])
except:
print "error!"
sapi.disconnect()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment