15. Twitter Bot¶
- Twitter: https://twitter.com/
- Developer Twitter: https://developer.twitter.com/
15.1. List Tweets From an Account¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | import tweepy # The keys for the app consumer_key = "secret_data_goes_here" consumer_secret = "secret_data_goes_here" # User's account key/secret key = "secret_data_goes_here" secret = "secret_data_goes_here" def main(): # Authenticate to Twitter auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(key, secret) # Create API object api = tweepy.API(auth) # Make sure we are logged in # Not really needed try: api.verify_credentials() print("Authentication OK") except: print("Error ") return # Get tweets from timeline timeline = api.user_timeline("@professorcraven", count=10) # Loop through each tweet for tweet in timeline: # Print. You can also save in a file. # Can't pull more that 3,000 unless this is your own account. print(f"{tweet.user.name} said:\n{tweet.text}") main() |
15.2. Get Keys To Log In As User¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | import tweepy # The keys for the app consumer_key = "secret_data_goes_here" consumer_secret = "secret_data_goes_here" # Authenticate to Twitter auth = tweepy.OAuthHandler(consumer_key, consumer_secret) try: redirect_url = auth.get_authorization_url() print(redirect_url) except tweepy.TweepError: print('Error! Failed to get request token.') # ask user to verify the PIN generated in broswer verifier = input('PIN: ').strip() auth.get_access_token(verifier) print(f'key = "{auth.access_token}"') print(f'secret = "{auth.access_token_secret}"') # authenticate and retrieve user name auth.set_access_token(auth.access_token, auth.access_token_secret) api = tweepy.API(auth) username = api.me().name print('Ready to post to ' + username) |
15.3. Like Tweets¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | import tweepy # The keys for the app consumer_key = "secret_data_goes_here" consumer_secret = "secret_data_goes_here" # User's account key/secret key = "secret_data_goes_here" secret = "secret_data_goes_here" def main(): # Authenticate to Twitter auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(key, secret) # Create API object api = tweepy.API(auth) # Get tweets from timeline timeline = api.user_timeline("@professorcraven", count=10) # Loop through each tweet for tweet in timeline: # Go ahead and favorite the tweet if not tweet.favorited: tweet.favorite() print("Tweet favorited") main() |
15.4. Make a Tweet¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import tweepy # The keys for the app consumer_key = "secret_data_goes_here" consumer_secret = "secret_data_goes_here" # User's account key/secret key = "secret_data_goes_here" secret = "secret_data_goes_here" def main(): # Authenticate to Twitter auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(key, secret) # Create API object api = tweepy.API(auth) api.update_status("WooHoo! Iowa is the best.") print("Done tweeting.") main() |
15.5. Search on Term¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | import tweepy # The keys for the app consumer_key = "secret_data_goes_here" consumer_secret = "secret_data_goes_here" # User's account key/secret key = "secret_data_goes_here" secret = "secret_data_goes_here" def main(): # Authenticate to Twitter auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(key, secret) # Create API object api = tweepy.API(auth) # Get tweets from timeline timeline = api.search("\"Simpson College\"") # Loop through each tweet for tweet in timeline: # Print. You can also save in a file. # Can't pull more that 3,000 unless this is your own account. print(f"{tweet.user.name} said:\n{tweet.text}") print() main() |
15.6. Search on Location¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | import tweepy # The keys for the app consumer_key = "secret_data_goes_here" consumer_secret = "secret_data_goes_here" # User's account key/secret key = "secret_data_goes_here" secret = "secret_data_goes_here" def main(): # Authenticate to Twitter auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(key, secret) # Create API object api = tweepy.API(auth) # Get tweets from timeline timeline = api.search(geocode="41.354,-93.575,5mi") # Loop through each tweet for tweet in timeline: # Print. You can also save in a file. # Can't pull more that 3,000 unless this is your own account. print(f"@{tweet.user.screen_name} - {tweet.user.name} said:\n{tweet.text}") print() main() |