Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@inventionsbyhamid
Created March 14, 2017 18:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inventionsbyhamid/5bd0b1d9156b867fad46e59536938b4f to your computer and use it in GitHub Desktop.
Save inventionsbyhamid/5bd0b1d9156b867fad46e59536938b4f to your computer and use it in GitHub Desktop.
Retweet all tweets with a hashtag from Twitter Stream
# Retweet all tweets with a hashtag from Twitter Stream
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler, API
from tweepy import Stream
import json
import warnings
warnings.filterwarnings("ignore")
# Create a Twitter app from https://apps.twitter.com/
# Generate an access token with read/write permission
# Copy the 4 key values inside quotes
access_token = ""
access_token_secret = ""
consumer_key = ""
consumer_secret = ""
auth_handler = OAuthHandler(consumer_key, consumer_secret)
auth_handler.set_access_token(access_token, access_token_secret)
twitter_client = API(auth_handler)
class PyStreamListener(StreamListener):
def on_data(self, data):
tweet = json.loads(data)
#print json.dumps(tweet, indent=2)
print "Processing"
print tweet["text"].encode('utf-8')
try:
if "retweeted_status" not in tweet:
twitter_client.retweet(tweet['id'])
print "Retweeted\n"
else:
print "Already Retweeted this.\n"
except Exception as ex:
print ex
return True
def on_error(self, status):
print status
if __name__ == '__main__':
listener = PyStreamListener()
stream = Stream(auth_handler, listener)
# List of hashtags to track
stream.filter(track=['campusforgbpec'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment