Monthly Archives: August 2010

Use Powershell To Follow Your Followers On Twitter

Update: Twitter “statuses/followers” API documentation had a small note at the bottom that says that it returns only 100 followers if no paging is used, I have updated the script accordingly. Thanks to @RamyMahrous for notifying me on this in his comment below. <End of Update>

For a long time I thought that @DotNetArabi shouldn’t follow it’s own followers due to various reasons I had, but lately I discovered that I was wrong. So I have decided to follow them back no matter how many they are, but that would be a tedious thing to do manually. Here comes Powershell to the rescue.

I wrote the following Powershell script in Powershell ISE (the Integrated Scripting Environment) which is already shipped with Win7, utilizing Twitter’s API’s and it did the trick:

———————–
 $wc = new-object System.Net.Webclient
 $wc.Credentials = new-object System.Net.NetworkCredential "dotnetarabi","UseYourOwnPWbuddy!"
 $cursorCount = "-1"
 
 do { 
 
 $rest = $wc.DownloadString("http://api.twitter.com/statuses/followers.xml?cursor=" + $cursorCount)
 $xml = $rest
 foreach( $user in $xml.Users_list.users.user ) 
 { 
    $wc.UploadString("http://api.twitter.com/1/friendships/create/" + $user.screen_name + ".xml", "")
 } 

 $cursorCount = $xml.users_list.next_cursor
}
while($xml.users_list.next_cursor -ne 0)

——————-

It’s simple and straight forward; I used “UploadString” method of the Webclient class because it produces a “POST” HTTP request required by Twitter API rather than “GET”.
Note that the response you get from the “Create” API will be either an XML representation of the followed-user entity, or a server error “Forbidden” for many of reasons Twitter may have. This is how you will know if each “follow” procedure succeeded or not

Thanks to Helge Klein I could highlight my Powershell script by his script here