Skip to content

Instantly share code, notes, and snippets.

@duarteocarmo
Last active April 18, 2020 11:01
Show Gist options
  • Save duarteocarmo/fe3084b45ed1ef19e7aac5e96e4d3018 to your computer and use it in GitHub Desktop.
Save duarteocarmo/fe3084b45ed1ef19e7aac5e96e4d3018 to your computer and use it in GitHub Desktop.
Unfollow twitter accounts that have not tweeted for X days.
import tweepy
import datetime
NO_ACTIVITY_FOR = 30
UNFOLLOW_COUNTER = 0
PROCESSED_USERS = 0
# get these at: https://developer.twitter.com/en/apps
access_token = ""
access_token_secret = ""
consumer_key = ""
consumer_secret = ""
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
my_id = api.me().id
today = datetime.datetime.now()
print(f"Your twitter name: {api.me().name}")
def days_since_last_tweet(id_):
try:
tweet = api.user_timeline(id_, count=1)[0]
last_tweet_datetime = tweet.created_at
user = api.get_user(id_).name
days_since_last_tweet = today - last_tweet_datetime
days_since_last_tweet = days_since_last_tweet.days
# print(f"User: {user}")
# print(f"Tweet: {tweet.text[0:100]}")
# print(f"Tweet datetime: {last_tweet_datetime}")
# print(f"Days passed: {days_since_last_tweet}")
return days_since_last_tweet
except Exception as e:
print(str(e))
return 0
def unfollow_user(id_):
user = api.get_user(id_).name
print(f"Unfollowing: {user}..")
api.destroy_friendship(id_)
print("Done.")
for id_ in api.friends_ids(my_id):
PROCESSED_USERS += 1
if days_since_last_tweet(id_) > NO_ACTIVITY_FOR:
unfollow_user(id_)
UNFOLLOW_COUNTER += 1
print(f"Unfollowed users: {UNFOLLOW_COUNTER}")
print(f"Processed users: {PROCESSED_USERS}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment