Skip to content

Instantly share code, notes, and snippets.

@szolotykh
Created January 29, 2015 16:55
Show Gist options
  • Save szolotykh/5e98f3c1e0e5c8ef58d3 to your computer and use it in GitHub Desktop.
Save szolotykh/5e98f3c1e0e5c8ef58d3 to your computer and use it in GitHub Desktop.
Script streams posts from twitter with tweepy
import tweepy
import os
import json
# Consumer keys and access tokens, used for OAuth
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)
#contributors
#truncated
#text
#in_reply_to_status_id
#id
#favorite_count
#source
#retweeted
#coordinates
#entities
#in_reply_to_screen_name
#id_str
#retweet_count
#in_reply_to_user_id
#favorited
#user
#geo
#in_reply_to_user_id_str
#possibly_sensitive
#lang
#created_at
#filter_level
#in_reply_to_status_id_str
#place
#print jTweet['user']['name'].encode('ascii', 'ignore')
text = jTweet['text'].encode('ascii', 'ignore')
if not "//t.co" in text:
print text
print "--------------------------------------------"
return True
def on_error(self, status_code):
return True # Don't kill the stream
def on_timeout(self):
return True # Don't kill the stream
if __name__ == '__main__':
# OAuth process, using the keys and tokens
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
# Creation of the actual interface, using authentication
api = tweepy.API(auth)
# Creates the user object. The me() method returns the user whose authentication keys were used.
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','Ukraine'])
except:
print "error!"
sapi.disconnect()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment