Twitter PowerShell Script
- Posted: Feb 23, 2009 at 7:01 AM
- 479 Views
Want to get super-geeky with the tracking of your Twitter followers? You can now, thanks to Mike Ormond’s Twitter PowerShell script for checking the status of your Twitter followers. His script produces two lists – those that follow you which you aren’t following back and those you follow who don’t follow you back.
There’s also an example at the end of the script (commented out) of how to iterate over the $notfriended list to add the people who are following you but you’re not yet following.
When it runs it populates two arrays $notfriended and $notfollowed (just type the names at the prompt to list their contents).
Using it is easy – just change the username and password and then paste it into PowerShell. (To get to PowerShell, just type powershell into the Start Menu search box in Vista, 7, or Windows Server). Update: You actually have to install PowerShell in Vista - sorry, tried this on Windows 7 only! Here's a link: http://support.microsoft.com/kb/928439
If you have a lot of followers, you might encounter the Twitter API’s rate limit.
Here’s the script:
1: ############################################################# 2: ## Add or Remove a friend with the given ID 3: #############################################################4: Function AddOrRemove-TwitterFriend ( [string] $ID,
5: [string] $Command,
6: [string] $Username,
7: [string] $Password)
8: {9: [System.Net.ServicePointManager]::Expect100Continue = $false
10: 11: if ($ID -ne $null) {
12: $request =13: [System.Net.WebRequest]::Create("http://twitter.com/friendships/$Command/$ID.xml")
14: $request.Credentials = new-object System.Net.NetworkCredential($Username, $Password)
15: $request.Method = "POST"
16: $request.ContentType = "application/x-www-form-urlencoded"
17: $formdata = [System.Text.Encoding]::UTF8.GetBytes( 'follow=true' )
18: $requestStream = $request.GetRequestStream() 19: $requestStream.Write($formdata, 0, $formdata.Length) 20: $requestStream.Close() 21: $response = $request.GetResponse() 22: 23: $reader = new-object System.IO.StreamReader($response.GetResponseStream())
24: $result = $reader.ReadToEnd() 25: $reader.Close()26: return $result
27: } 28: } 29: 30: #############################################################31: ## Get the current rate limit status for account or IP address
32: #############################################################33: Function Get-TwitterLimitStatus ( [string] $Username, [string] $Password )
34: { 35: if ($WebClient -eq $null) {
36: $Global:WebClient=new-object System.Net.WebClient
37: }38: if ($Username) {
39: $WebClient.Credentials = 40: (New-Object System.Net.NetworkCredential -argumentList $Username, $Password) 41: } 42: 43: $URL = "http://twitter.com/account/rate_limit_status.xml"
44: return $WebClient.DownloadString($URL)
45: } 46: 47: ############################################################# 48: ## Get all the friends / followers of the authenticated user or the specific ID 49: #############################################################50: Function Get-TwitterFriendOrFollower ( [string] $Username,
51: [string] $Password,
52: [string] $Command,
53: [string] $ID )
54: { 55: if ($WebClient -eq $null) {
56: $Global:WebClient=new-object System.Net.WebClient
57: }58: if ($Username) {
59: $WebClient.Credentials = 60: (New-Object System.Net.NetworkCredential -argumentList $Username, $Password) 61: }62: if ($ID) {
63: $URL="http://twitter.com/statuses/$Command/$ID.xml?page="
64: }65: else {
66: $URL="http://twitter.com/statuses/$Command.xml?page="
67: } 68: $page = 1 69: $friends = @()70: do {
71: $rawResponse = $WebClient.DownloadString($url+$Page) 72: $response = (([xml]($rawResponse)).users.user)73: if ($response -ne $null) {
74: foreach ($u in $response) {
75: $friends += $u["screen_name"].psbase.InnerText
76: } 77: } 78: $page ++79: } while ($response.count -gt 0)
80: return $friends
81: } 82: 83: ############################################################# 84: # Add your username and password here 85: #############################################################86: $Username = "MyUsername"
87: $Password = "MyPassword"
88: 89: [System.Net.ServicePointManager]::Expect100Continue = $false
90: 91: ## Show the current rate limiet status for this account / IP
92: Get-TwitterLimitStatus $Username $Password 93: 94: ## Get a complete list of friends (ie people you follow)95: $friends = Get-TwitterFriendOrFollower $Username $Password "friends"
96: 97: ## Get a complete list of followers (people who follow you)98: $followers = Get-TwitterFriendOrFollower $Username $Password "followers"
99: 100: ## Generate a list of people that follow you but you don't follow them
101: $notfriended = @() 102: foreach ($item in $followers) { if (!($friends -contains $item)) { $notfriended += $item } } 103: 104: ## Generate a list of people you follow but who don't follow you 105: $notfollowed = @()106: foreach ($item in $friends) { if (!($followers -contains $item)) { $notfollowed += $item } }
107: 108: ## This will add a follow for all the people in your notfriended list
109: ##foreach ($item in $notfriended) { AddOrRemove-TwitterFriend $item "create" $Username $Password }
Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation,
please create a new thread in our Forums,
or
Contact Us and let us know.
Follow the Discussion
Oops, something didn't work.
What does this mean?
Following an item on Channel 9 allows you to watch for new content and comments that you are interested in. You need to be signed in to Channel 9 to use this feature.What does this mean?
Following an item on Channel 9 allows you to watch for new content and comments that you are interested in and view them all on your notifications page.sign up for email notifications?