Skip to content

Instantly share code, notes, and snippets.

@aritraroy24
Created June 4, 2021 14:41
Show Gist options
  • Save aritraroy24/3208e068375765700c9be20659cd6b3d to your computer and use it in GitHub Desktop.
Save aritraroy24/3208e068375765700c9be20659cd6b3d to your computer and use it in GitHub Desktop.
following back who retweets
def follow_back(self, api):
'''
Following back to those users who are retweeting
'''
# getting the retweeters of latest tweet
latest_tweet_id = (api.user_timeline(count=1)[0]).id
retweets_list = api.retweets(latest_tweet_id)
user_name_list = [
retweet.user.id for retweet in retweets_list]
# getting the friendlist
friend_list = [friend for friend in api.friends(
user_id="CompChemNewsBot")]
friend_final_list = [friend.id for friend in friend_list]
# removing the already followed users
final_list = [
elem for elem in user_name_list if elem not in friend_final_list]
# in case no new user to follow
if final_list == []:
logger.info(
f"No new user found at: {str(datetime.datetime.now())}\n\t\t\t\t\t\t******\t\t\t\t\t\t")
# new user found and following the user
else:
for user in final_list:
try:
api.create_friendship(user_id=user)
logger.info(
f"{user} UserID followed back at : {str(datetime.datetime.now())}\n\t\t\t\t\t\t******\t\t\t\t\t\t")
except Exception as e:
logger.info(
f"Can't be followed back at : {str(datetime.datetime.now())} due to {e} error\n\t\t\t\t\t\t******\t\t\t\t\t\t")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment