Last active
August 1, 2019 08:44
-
-
Save oliviaguest/bfb0133e6f6660c0e7eb4817b5dd749e to your computer and use it in GitHub Desktop.
Remove (and backup) non-mutuals on Twitter.
This file contains hidden or 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
#!/usr/bin/env python3 | |
import csv | |
import time | |
import tweepy | |
import datetime | |
# Read http://docs.tweepy.org/en/v3.5.0/auth_tutorial.html to fill in the two lines below. | |
auth = tweepy.OAuthHandler('XXX', 'YYY') | |
auth.set_access_token('123', '456') | |
api = tweepy.API(auth, wait_on_rate_limit=True) | |
# Create this directory to save the people that will be removed, in case you need them back. | |
backup_directory = './backup/' | |
# Who you follow = followed. | |
followed = set(api.friends_ids()) | |
# Who follows you = followers. | |
followers = set(api.followers_ids()) | |
# Give the backups a unique name. | |
timestamp = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d-%H-%M-%S') | |
# Save both your followers and who you follow. | |
with open(backup_directory + 'followed_' + timestamp + '.csv', 'w') as myfile: | |
wr = csv.writer(myfile) | |
wr.writerow(followed) | |
with open(backup_directory + 'followers_' + timestamp + '.csv', 'w') as myfile: | |
wr = csv.writer(myfile) | |
wr.writerow(followers) | |
# Now find who your mutuals are (people you follow and who follow you). | |
mutuals = followers.intersection(followed) | |
# Who is left are the non-mutuals. | |
non_mutuals = followed - mutuals | |
# So get rid of them! | |
for non_mutual in non_mutuals: | |
api.destroy_friendship(non_mutual) | |
print(api.get_user(non_mutual).name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment