Created
March 14, 2017 18:56
-
-
Save inventionsbyhamid/5bd0b1d9156b867fad46e59536938b4f to your computer and use it in GitHub Desktop.
Retweet all tweets with a hashtag from Twitter Stream
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
# 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