Skip to content

Instantly share code, notes, and snippets.

@mknepprath
Created February 19, 2020 19:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mknepprath/e7cdafd078efffc5a27683a5017386e5 to your computer and use it in GitHub Desktop.
Save mknepprath/e7cdafd078efffc5a27683a5017386e5 to your computer and use it in GitHub Desktop.
The Delete Stale Likes script.
# In order to run this, you will need to get Twitter API access and set environment variables for the following:
# - TWITTER_CONSUMER_KEY
# - TWITTER_CONSUMER_SECRET
# - TWITTER_ACCESS_TOKEN
# - TWITTER_ACCESS_TOKEN_SECRET
#
# Then, update TWITTER_ID below with your Twitter ID. This is a string of numbers
# you can look up with http://gettwitterid.com/.
#
# You will also need to `pip install tweepy`.
#
# Finally, run `python unlike_bot.py`.
import os
import tweepy
TWITTER_ID = "replace with Twitter ID string" # http://gettwitterid.com/
class TwitterAPI:
"""
Class for accessing the Twitter API.
"""
def __init__(self):
consumer_key = os.environ.get('TWITTER_CONSUMER_KEY')
consumer_secret = os.environ.get('TWITTER_CONSUMER_SECRET')
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
access_token = os.environ.get('TWITTER_ACCESS_TOKEN')
access_token_secret = os.environ.get('TWITTER_ACCESS_TOKEN_SECRET')
auth.set_access_token(access_token, access_token_secret)
self.api = tweepy.API(auth, wait_on_rate_limit=True)
def unlike(self, id):
"""Unlike tweet"""
self.api.destroy_favorite(id=id)
if __name__ == "__main__":
twitter = TwitterAPI()
page = 1
while True:
print("Page " + str(page))
likes = twitter.api.favorites(id=TWITTER_ID, count=200, page=page)
print(str(len(likes)) + " likes found")
# THIS IS ACTUALLY SORTA BROKEN BC IT UNLIKES ALL BUT 10 ~PER PAGE~ 😔
# If there are more than 10 likes...
if len(likes) > 10:
# ...loop through the likes...
for i, like in enumerate(likes):
# ...and unlike any after the first (most recent) 10.
if i >= 10:
twitter.unlike(like.id)
print("Unliked " + str(like.id))
else:
# All done
break
page += 1 # next page
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment