Created
March 2, 2020 20:39
-
-
Save nwithan8/d0ff1ef35c66db7472b61d8246375b02 to your computer and use it in GitHub Desktop.
Tweepy (Twitter + Python) boilerplate
This file contains hidden or 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 | |
| from tweepy.streaming import StreamListener | |
| from tweepy import Stream | |
| # Twitter API Credentials | |
| consumer_key = "xxxx" | |
| consumer_secret = "xxxx" | |
| access_token = "xxxx" | |
| access_secret = "xxxx" | |
| auth = tweepy.OAuthHandler(consumer_key, consumer_secret) | |
| auth.set_access_token(access_token, access_secret) | |
| twitter = tweepy.API(auth) | |
| def reply_to_status(status, response, image=None): | |
| if image: | |
| twitter.update_with_media(image, '@{} {}'.format(str(status.user.screen_name), response), | |
| in_reply_to_status_id=status.id) | |
| else: | |
| twitter.update_status('@{} {}'.format(str(status.user.screen_name), response), in_reply_to_status_id=status.id) | |
| class StdOutListener(StreamListener): | |
| def on_status(self, status): | |
| # do something with a new status | |
| def on_error(self, status_code): | |
| print("Error, code {}".format(status_code)) | |
| listener = StdOutListener() | |
| stream = Stream(auth, listener) | |
| # Tweepy Documentation: http://docs.tweepy.org/en/latest/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment