Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ $ pip install tweepy
```
consumer_key = ''
consumer_secret = ''
bearer_token = ''
access_token = ''
access_token_secret = ''
```
Expand Down
1 change: 1 addition & 0 deletions credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

consumer_key = ''
consumer_secret = ''
bearer_token= ''
access_token = ''
access_token_secret = ''
42 changes: 21 additions & 21 deletions twitterbot_retweet.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,38 @@
from credentials import *
from config import QUERY, FOLLOW, LIKE, SLEEP_TIME

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
client = tweepy.Client(bearer_token, consumer_key, consumer_secret, access_token, access_token_secret)
auth = tweepy.OAuth1UserHandler(consumer_key, consumer_secret, access_token, access_token_secret)
api = tweepy.API(auth)

print("Twitter bot which retweets,like tweets and follow users")
print("Bot Settings")
print("Like Tweets :", LIKE)
print("Follow users :", FOLLOW)

for tweet in tweepy.Cursor(api.search, q=QUERY).items():
try:
print('\nTweet by: @' + tweet.user.screen_name)
class MyStream(tweepy.StreamingClient):
def on_tweet(self, tweet):
print(tweet.text)

tweet.retweet()
print('Retweeted the tweet')
try:
client.retweet(tweet.id)
print("Retweeted the tweet")

# Favorite the tweet
if LIKE:
tweet.favorite()
print('Favorited the tweet')
if LIKE:
client.like(tweet.id)
print("Liked the tweet")

# Follow the user who tweeted
#check that bot is not already following the user
if FOLLOW:
if not tweet.user.following:
tweet.user.follow()
print('Followed the user')
if FOLLOW and not tweet.user.following:
client.follow(tweet.user.id)
print("Followed the user")

except Exception as error:
print(error)

sleep(SLEEP_TIME)

except tweepy.TweepError as e:
print(e.reason)
stream = MyStream(bearer_token = bearer_token)

except StopIteration:
break
rule = tweepy.StreamRule(f"({QUERY})(-is:retweet -is:reply)" )
stream.add_rules(rule)
stream.filter()