Monday, September 21, 2009

Script to unfollow people on twitter - Python

This is exactly like the last script with a few minor changes. 1st, the last script only has the ability to force people to unfollow you if you aren’t following them. 2nd, the api call and the request URL are different. GetFollowers instead of GetFriends, and friendships/remove instead of friendships/destroy. Don’t forget to fill in the same 4 fields that were missing/wrong in the last one.

 

#!/usr/bin/python

import twitter
import urllib2

headers = {
'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)",
'Cookie'  : "__utma=",
}

data = "authenticity_token=&twttr=true"

api = twitter.Api(username='joeuser', password='password1')
for b in range(1,100):
    users = api.GetFollowers(page=b)
    for i in users:
        request = http://twitter.com/friendships/remove/ + str(i.id)
        req = urllib2.Request(request,data,headers)
        post = urllib2.urlopen(req)
        print post

4 comments:

Tofi said...

Hmm, I wrote a script on python-twitter on my blog here and I have used python-twitter before. I think it'd be easier to remove friendships with someone by using the destroyFriendship() method which will destroy it rather than using urllib. Just my two cents. (:

mubix said...

You're right, but it would cost you another API call to do so. I was working on the premice that I wanted to unfollow everyone as quick as I could and using the least amount of API calls was my way of doing it. You way would have definitely be a lot less hassle on the side of the person using the script definitely.

Tofi said...

Yeah, I see. (: Interesting.

Tom said...

i would usggest to find a workaround not using the API, as the API is closly tracked and limited :)