Last active
April 18, 2020 11:01
-
-
Save duarteocarmo/fe3084b45ed1ef19e7aac5e96e4d3018 to your computer and use it in GitHub Desktop.
Unfollow twitter accounts that have not tweeted for X days.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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