General Geekery

Musings of an incurable technophile

Deleting Twitter Favorites

People use the favorites feature in Twitter differently. The way I use it, is to mark tweets that I want to look up later. I usually access Twitter on the phone, and many times I find interesting tweets that link to websites or articles. I mark these tweets as favorites, and later look them up in a desktop twitter client.

Over time this has left me with a huge number of tweets marked as favorites, and I was looking for an easy way to remove the favorite status from them. The prospect of going through them one by one was not a tempting option, as we are talking about hundreds and thousands of messages. I love to code in Ruby, and John Nunemaker has coded a twitter gem that wraps the Twitter API. From there it was but a short step to write a code snippet that did what I wanted:

unfavorite.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
require "twitter"
require "yaml"
class Unfavorite
def configure
twitter_credentials = YAML.load_file("twitter_credentials.yml")["twitter"]
Twitter.configure do |config|
config.consumer_key = twitter_credentials["consumer_key"]
config.consumer_secret = twitter_credentials["consumer_secret"]
config.oauth_token = twitter_credentials["oauth_token"]
config.oauth_token_secret = twitter_credentials["oauth_token_secret"]
end
end
def get_favorites
favs = []
page_num = 1
begin
page = Twitter.favorites(:page => page_num)
favs += page
page_num += 1
end while page.size > 0
favs
end
def unfavorite(favorites)
favorites.each do |tweet|
Twitter.favorite_destroy tweet.id
end
end
end
uf = Unfavorite.new
uf.configure
favs = uf.get_favorites
uf.unfavorite favs

The code above is mostly self explanatory. To use it, you need to register as a developer on the Twitter website to get the required keys and token used for authorization. I am storing the twitter authorization information in a YAML file separate from the code, but for a snippet like this, the keys and tokens could easily be hardcoded directly in the configure method.

The structure of the YAML file is as follows:

twitter_credentials.yml
1
2
3
4
5
6
twitter:
consumer_key: [replace with key assigned from dev.twitter.com]
consumer_secret: [replace with secret assigned from dev.twitter.com]
oauth_token: [replace with token assigned from dev.twitter.com]
oauth_token_secret: [replace with secret assigned from dev.twitter.com]

I have found this code to be a very convenient way to clean up my Twitter favorites, and hopefully someone else can also find it of use.

Comments