Created
May 23, 2014 05:22
-
-
Save szolotykh/058c2e424f48f45a9fd4 to your computer and use it in GitHub Desktop.
Tweets streaming
This file contains 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 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