Hey guys, Welcome to the part 5 of twitter bot. As we have learned how to connect twitter API and python ide and also how to create some twitter bots in the previous articles. Now, let us create a follow back followers twitter bot. Sometimes it is very hard to follow each and every twitter follower back when the followers are hundreds or thousands and some times even in millions. That is where this twitter bot comes in handy. So, lets get into the coding part.
Code of Follow back followers twitter bot:
import tweepy auth=tweepy.OAuthHandler(consumer_key,consumer_secret)#enter your keys here auth.set_access_token(access_token,access_token_secret)#enter your keys here api=tweepy.API(auth)#to establish connection public_tweets=api.home_timeline()#assigned our twitter home timeline information to the variable public_tweets
#The above limit_handler function is used because when the followers are many, then continously getting back to twitter API to follow each person makes the twitter server to get hang. So, we use the function to set a time limit between each iteration, so that the program sleeps for some mentioned time. Here the function while going through every iteration, it checks whether we are getting an RateLimitError or not, if we do then it sleeps for 300ms and if not the next iteration goes on.
for follower in limit_handler(tweepy.Cursor(api.followers).items()): if follower.name=="xyz": #this is to follow certain follower, we can even mention multiple followers usernames. Here we considered xyz as an username for understanding purpose. follower.follow()
If we want to follow back every follower then, we can use:
for follower in limit_handler(tweepy.Cursor(api.followers).items()): follower.follow()
We will learn about auto like twitter bot in the next article.
Comments