Monday, September 21, 2009

Script to unblock people on Twitter – Ruby

I created this script because I couldn’t really find anything out there for it. Both the Twitter support page and all the Twitter APIs out there had the ability to unblock people, but only if you knew who you wanted to unblock. Recently I tried the Twitter Karma service that could Mass unfollow / block people (hence my last couple scripts). I clicked the wrong button one time and it blocked a whole bunch of people. But say your not a klutz like me, maybe you just forgot who you’ve blocked over time.

This script will dump the list of people you block and unblock them all. Now you could expand this to get the names of each individual that you block but that’s an API call for each. Let me know if there is a better way, right now, the only way to figure out who was unblocked is through the 302 response that is generated with each request that sends you to the users page that you unblocked. (Push this script through a proxy to see it.)

 

#!/usr/bin/env ruby

require 'net/http'

require 'rexml/document'
include REXML

use_proxy = false
proxy_srvr = "127.0.0.1"
proxy_port = "8080"
proxy_user = ""
proxy_pass = ""

twitter_user = "joeuser"
twitter_pass = "password1"

header = {
    'User-Agent' => "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3 (.NET CLR 3.5.30729)",
    'X-Requested-With' => "XMLHttpRequest",
    'Cookie' => "__utma="
}

data = "authenticity_token=&twttr=true"

doc = "temp"

if use_proxy == true
    Net::HTTP::Proxy(proxy_srvr, proxy_port, proxy_user, proxy_pass).start('twitter.com') {|http|
        req = Net::HTTP::Get.new('/blocks/blocking/ids.xml')
        req.basic_auth twitter_user, twitter_pass
        response = http.request(req)
        doc = Document.new response.body
    }
else
        Net::HTTP.start('twitter.com') {|http|
            req = Net::HTTP::Get.new('/blocks/blocking/ids.xml')
            req.basic_auth twitter_user, twitter_pass
            response = http.request(req)
            doc = Document.new response.body
    }
end

blocks = doc.elements.each('//id') { |f|
    if use_proxy == true
            Net::HTTP::Proxy(proxy_srvr, proxy_port, proxy_user, proxy_pass).start('twitter.com') {|http|
            req2 = '/blocks/destroy/' + f.text
            response2 = http.post(req2, data, header)
            puts response2.code
            }
    else
            Net::HTTP.start('twitter.com') {|http|            
            req2 = '/blocks/destroy/' + f.text
            response2 = http.post(req2, data, header)
            puts response2.code
            }
    end

    puts "Unblocking: " + f.text
}

6 comments:

BC said...

Alright, this is great and all, but how would a layperson use this?

Rob Fuller (mubix) said...

To even run the script, you need Ruby and Net::HTTP. After you have that you need your twitter username and password, cookie and auth token.

You have your user/pass. The cookie and auth token can be easily gotten out of any POST that you make on the twitter web site. So fire up Wireshark or your favorite proxy and capture a request. Copy and paste the cookie string and the auth token into the areas allotted in the in the script, save, and run. It was written in Linux so I recommend running it under Linux.

M.I.S.S. Kari Baby said...
This comment has been removed by the author.
M.I.S.S. Kari Baby said...

I'm really confused even by your reply to BC. BTW, I use a Mac. Can I still use your script? (I commented again so that I could get an email when you respond)

Rob Fuller (mubix) said...

Ya, I'm pretty sure ruby runs just fine on a Mac. I don't own a Mac, and it's been a decade since I've sat behind one for any length of time so I'm not sure the specifics, and I'm sorry, but at this time I can't really think of a good way to explain how to capture the auth token and the cookie. Sorry. I have pinged a couple friends to see if they can help explain and I'll post an update to the new home of this blog over at http://www.room362.com/mubixlinks

M.I.S.S. Kari Baby said...

ok thanks! I'll continue to check there for updates