Everything and Nothing

Tweepy Cleanup

Cropped shot of a young man downloading in mobile store popular applications and multimedia programs. Man’s hands using device at co-working office. Blurred background and infographics icons effect

Every so often, I like to go back at clean up my old tweets, favorites, etc. It’s kind of like email… holding on to old content is probably a bad idea. For whatever reason, there really isn’t a good way to do this from the Twitter UI. Not unique to Twitter, but since I don’t really play on FaceBook or the other social nets, I don’t worry so much about them.

I gave my class last semester an assignment to write a bot using Tweepy which is a really useful Python library that makes manipulating the Twitter API very simple, with some limitations.

Combine that with being really lazy, not wanting to pay for something I can do myself, and not giving my account credentials for sketchy tweet clean-up apps, and now I’m posting a code example for cleaning up old tweets.

In order to run this you need to install Python 3.x, Tweepy (via PiP), and have a Twitter developer account with credentials to access the API (consumer key and secret and access token and secret).

I’ll leave all that as an exercise to anyone interested. Once you have that, you can write a script like what I have below to remove favorites. Once you understand the script – not hard modifying it to filter what you want to delete or to modify it to do other things.

To explain the code, once you authenticate to the Twitter API via Tweepy, you can use the Tweepy functions to manipulate your timeline as well and interact with others.

The Tweepy Cursor is key to manipulating Twitter data. In the code example below – likes (or in Twitter parlance – favorites) are returned as paginated data. We use the cursor to put this into an iterable object. This can be used with lots of Twitter data, statuses, retweets, followers, etc.

In this super simple example, we use the Cursor object to iterate over lists of likes/favorites and delete them using tweepy.API.destroy_favorite() function.

We slapped all this together into a quickie script that can be run every so often to clean up old likes/favorites… this is better than manually clicking through… and if you are managing your social media preference this is the type of things not built into program.

Questions/Comments? Drop a note..

import tweepy
import time

#authentication function
def get_auth_api(p1,p2,p3,p4):
    auth = tweepy.OAuthHandler(p1, p2)
    auth.set_access_token(p3, p4)
    return tweepy.API(auth)

# returns iterator containing tweet objects
def get_favorites(api):
    return tweepy.Cursor(api.favorites).items()

# remove tweets from favorites list
def delete_favorites(p1, p2, p3, p4):
    delete_count = 0
    api = get_auth_api(p1, p2, p3, p4)
    favorites = get_favorites(api)

    for tweet in favorites:
        print("removing favorite %d: [%s] %s" % (tweet.id, tweet.created_at, tweet.text))
        api.destroy_favorite(tweet.id)
        delete_count += 1
        time.sleep(3)

    print("removed %d favorite tweets" % (delete_count))

#call function
delete_favorites('CONSUMER_KEY', 'CONSUMER_SECRET',
                 'ACCESS_TOKEN', 'ACCESS_TOKEN_SECRET')

or share this content