Twitter Script – Who’s following you (or not following you)?
Find out who your friends really are with this handy PowerShell script! Essentially it will go through your friends and followers and produce two “difference” lists; people who follow you that you’re not following and people you follow but don’t follow you back.
[Update 20/2/09: Made a minor change to the sript to avoid the server returning 417 Expectation Failed]
Originally I created this as a little console app as a safety net to catch those people who’d followed me but I hadn’t pushed the button to follow them. I thought it might be easier to share it as a PowerShell script. I used a couple of James’s functions but as I didn’t understand some of his bits I had to re-write them my own way (which almost certainly isn’t as good a way given James knows a lot more about PowerShell than I do but at least I now think I know what’s going on).
All you have to do is copy and paste the below script at a PowerShell prompt – change the Username and Password first! When it runs it populates two arrays $notfriended and $notfollowed (just type the names at the prompt to list their contents).
There’s an example at the end (commented out) of how to iterate over the $notfriended list to add the people who are following you but you’re not yet following.
There is a rate limit on the Twitter API. If you have *a lot* of friends / followers, you may hit it. The call to Get-TwitterLimitStatus gives you a short summary of your current limit status.
Usual rules apply, no warranties, limited testing, limited error handling, use at your own risk, yadda, yadda, yadda…
#############################################################
## Add or Remove a friend with the given ID
#############################################################
Function AddOrRemove-TwitterFriend ( [string] $ID,
[string] $Command,
[string] $Username,
[string] $Password)
{
[System.Net.ServicePointManager]::Expect100Continue = $false
if ($ID -ne $null) {
$request =
[System.Net.WebRequest]::Create("https://twitter.com/friendships/$Command/$ID.xml")
$request.Credentials = new-object System.Net.NetworkCredential($Username, $Password)
$request.Method = "POST"
$request.ContentType = "application/x-www-form-urlencoded"
$formdata = [System.Text.Encoding]::UTF8.GetBytes( 'follow=true' )
$requestStream = $request.GetRequestStream()
$requestStream.Write($formdata, 0, $formdata.Length)
$requestStream.Close()
$response = $request.GetResponse()
$reader = new-object System.IO.StreamReader($response.GetResponseStream())
$result = $reader.ReadToEnd()
$reader.Close()
return $result
}
}
#############################################################
## Get the current rate limit status for account or IP address
#############################################################
Function Get-TwitterLimitStatus ( [string] $Username, [string] $Password )
{
if ($WebClient -eq $null) {
$Global:WebClient=new-object System.Net.WebClient
}
if ($Username) {
$WebClient.Credentials =
(New-Object System.Net.NetworkCredential -argumentList $Username, $Password)
}
$URL = "https://twitter.com/account/rate_limit_status.xml"
return $WebClient.DownloadString($URL)
}
#############################################################
## Get all the friends / followers of the authenticated user or the specific ID
#############################################################
Function Get-TwitterFriendOrFollower ( [string] $Username,
[string] $Password,
[string] $Command,
[string] $ID )
{
if ($WebClient -eq $null) {
$Global:WebClient=new-object System.Net.WebClient
}
if ($Username) {
$WebClient.Credentials =
(New-Object System.Net.NetworkCredential -argumentList $Username, $Password)
}
if ($ID) {
$URL="https://twitter.com/statuses/$Command/$ID.xml?page="
}
else {
$URL="https://twitter.com/statuses/$Command.xml?page="
}
$page = 1
$friends = @()
do {
$rawResponse = $WebClient.DownloadString($url+$Page)
$response = (([xml]($rawResponse)).users.user)
if ($response -ne $null) {
foreach ($u in $response) {
$friends += $u["screen_name"].psbase.InnerText
}
}
$page ++
} while ($response.count -gt 0)
return $friends
}
#############################################################
# Add your username and password here
#############################################################
$Username = "MyUsername"
$Password = "MyPassword"
[System.Net.ServicePointManager]::Expect100Continue = $false
## Show the current rate limiet status for this account / IP
Get-TwitterLimitStatus $Username $Password
## Get a complete list of friends (ie people you follow)
$friends = Get-TwitterFriendOrFollower $Username $Password "friends"
## Get a complete list of followers (people who follow you)
$followers = Get-TwitterFriendOrFollower $Username $Password "followers"
## Generate a list of people that follow you but you don't follow them
$notfriended = @()
foreach ($item in $followers) { if (!($friends -contains $item)) { $notfriended += $item } }
## Generate a list of people you follow but who don't follow you
$notfollowed = @()
foreach ($item in $friends) { if (!($followers -contains $item)) { $notfollowed += $item } }
## This will add a follow for all the people in your notfriended list
##foreach ($item in $notfriended) { AddOrRemove-TwitterFriend $item "create" $Username $Password }
Technorati Tags: powershell,twitter,followers,windows
Comments
Anonymous
February 17, 2009
PingBack from http://www.anith.com/?p=10904Anonymous
February 28, 2009
Для любителей твиттера и PowerShell Майк Ормонд (Mike Ormond) написал специальный скрипт на PowerShell,Anonymous
March 02, 2009
veee cool but you get MrTweet to do this for you http://mrtweet.net/Anonymous
March 03, 2009
Mike - sure, there are various other services that can do this for you. I like the script because I'm 100% in control and I can configure it / modify it to do exactly what I want. I also don't have to share my credentials with anyone. MikeAnonymous
March 08, 2009
Для любителей твиттера и PowerShell Майк Ормонд (Mike Ormond) написал специальный скрипт на PowerShell,