Geeks With Blogs

News

qrcode

Lance Robinson

Create Your Badge

Lance Robinson is a software engineer in Durham, Chapel Hill, Raleigh, and surrounding areas. More about Lance.

 Subscribe


Lance's TextBox » About Me » My Resume »Twitter

Anybody who uses Twitter has heard of auto-follow services for Twitter.

Below is a PowerShell script I used to auto UNfollow.  The script goes through the people you're following and eliminates "idle" or "spam" friends.  An idle friend is determined by a low friend count or low status count on an account that has been in existance for at least a couple months (configurable).  A spam friend is determined by a high friend count but a low follower count (by default, the script uses a 15-1 ratio as the max).

I would appreciate any good tweaks or improvements you might like to share!  In particular I wasn't sure how to deal with Twitter's rate limiting (150 get requests per hour).  For now I just catch the exception when it occurs and sleep for an hour before resuming.

 
 
 
 
 
#this script goes through the people you're following and eliminates "idle" or "spam" friends.
#an idle friend is determined by low friends or statuses on an account that has been in existance for a while
#a spam friend is determined by high friends but low followers

param([int] $minfriends = 20,
        [int] $minstatuses = 20,
        [int] $minaccountage = 60, #days
        [int] $maxratio = 15,
        [switch] $silent = $false #whether or not to prompt before unfollowing
        )

function unfollow($userid, $userinfo) {
    do {
        Write-Host "Unfollowing User $($userinfo.user.screen_name): $($userinfo.user.friends_count) friends/$($userinfo.user.followers_count) followers/$($userinfo.user.statuses_count) statuses [y/n/l]: " -noNewLine -foregroundColor Yellow
        if ($silent -eq $false) {
            $cmd = Read-Host
        }
        if ($silent -eq $true -or $cmd -eq "y") {
            $wc = new-object net.webclient
            $wc.Credentials = $global:twittercred
            $unfollo = $wc.UploadString("http://www.twitter.com/friendships/destroy/$($userid).xml", "")                
            $unfollow.user.following            
        }
        if ($cmd -eq "l") {
            Start-Process "http://www.twitter.com/$($userinfo.user.screen_name)"
        }
    } while ($silent -eq $false -and $cmd -eq "l")
}

#enter your Twitter credentials for retrieving your friends list
$global:twittercred = Get-Credential

#retrieve the full friends list
$wc = new-object net.webclient
$wc.Credentials = $global:twittercred
$response = [xml]($wc.DownloadString("http://www.twitter.com/friends/ids.xml"))

#now loop through each friend
$response.ids.id | % {
    try
    {
        #get detailed info about this user
        $showuser = [xml]($wc.DownloadString("http://www.twitter.com/users/show.xml?id=$($_)"))
    } catch {
        Write-Host "Twitter rate limit reached (150 gets/hour).  Pausing for one hour..." -foregroundColor Red -noNewLine
        Start-Sleep -Seconds 3600
    }
    #determine if they are worthy of following:
     $datecreated = [DateTime]::ParseExact($showuser.user.created_at, "ddd MMM dd H:mm:ss +0000 yyyy", [System.Globalization.CultureInfo]::InvariantCulture)
     $accountage = ([DateTime]::Now).Subtract($datecreated).Days    
    $statusescount = [int]$showuser.user.statuses_count
    $friendscount = [int]$showuser.user.friends_count
    $followerscount = [int]$showuser.user.followers_count
    if (($accountage -ge $minaccountage) -and ($friendscount -lt $minfriends -or $statusescount -lt $minstatuses)) {
        #unfollow idle friends:
        unfollow $_ $showuser
    }
    elseif (($accountage -ge $minaccountage -and $followerscount -eq 0) -or ($followerscount -gt 0 -and $friendscount / $followerscount -gt $maxratio)) {
        #unfollow spam friends:
        unfollow $_ $showuser
    }    
    else {
        Write-Host "Good friend $($showuser.user.screen_name): $($friendscount)/$($friendscount)/$($statusescount): "
    }
}
 
 
 
Technorati Tags: ,
Posted on Tuesday, January 12, 2010 1:06 PM | Back to top


Comments on this post: PowerShell: Twitter Auto UN-follow

# re: PowerShell: Twitter Auto UN-follow
Requesting Gravatar...
Love your layout! The embedded ISE is awesome. How are you doing it?
Left by Wes Stahler on Jan 14, 2010 9:13 AM

# re: PowerShell: Twitter Auto UN-follow
Requesting Gravatar...
Its css driven, the ISE that you see is actually a screenshot cut into 8 pieces (top left, top middle repeated, top right, body left, body right, bottom left, bottom middle repeated, bottom right). The code itself is marked up with surrounding div tags that mark the css classes to use for each (top left, top middle, etc). The css class definitions specify background images for their respective locations.
Left by Lance Robinson on Jan 14, 2010 9:33 AM

# re: PowerShell: Twitter Auto UN-follow
Requesting Gravatar...
Cool script -- Even recognizing what you were doing, I love the cute CSS trick.
Left by iHunger on Jan 14, 2010 9:52 AM

# re: PowerShell: Twitter Auto UN-follow
Requesting Gravatar...
Thanks Jim - that toolbar at the bottom of PowerWF is awesome!
Left by Lance Robinson on Jan 14, 2010 9:59 AM

Your comment:
 (will show your gravatar)


Copyright © Lance Robinson | Powered by: GeeksWithBlogs.net | Join free