# ============================================================================================== # # Microsoft PowerShell Source File -- Created with SAPIEN Technologies PrimalScript 2007 # # NAME: Get-Tweet.ps1 # VERSION: 0.6 # # AUTHOR: Jeffery Hicks , SAPIEN Technologies # DATE : 6/25/2008 # # COMMENT: This function will return all Twitter tweets from your friends # timeline. At first run it will get all tweets from the past six hours. # Everytime you run Get-Tweet a timestamp is written to another global # variable. The next time you run the function, only tweets since the last # tweet check will be displayed # The function will write an object to the pipeline with the tweet time stamp, # the friend's name and the tweet. I like to use the function like this: # # get-tweet | format-table -autosize -wrap # # which presents it in a nicely formatted fashion. Perhaps you'd like to # filter out your own tweets: # # get-tweet | where {$_.twitter -notmatch "JeffHicks"} | format-table -auto -wrap # # Or maybe you only want to see tweets from a certain person: # # get-tweet | where {$_.twitter match "halr9000"} | format-table Twitted,Tweet -autosize -wrap # # ============================================================================================== Function Get-Tweet { #check for global Twitter credential if (!$global:Twitter_Credential) { $global:Twitter_Credential=Get-Credential } #check for last tweet check if ($global:Tweet_Check) { [datetime]$since=$global:Tweet_Check } else { #if not found then calculate the date 6 hours ago [datetime]$since=(Get-Date).AddHours(-6) } #convert to GMT and a string [string]$sinceGMT=$since.ToUniversalTime().ToString() #replace AM or PM with GMT if ($sinceGMT.EndsWith("AM")) { $sinceGMT=$sinceGMT.Replace("AM","GMT") } else { $sinceGMT=$sinceGMT.Replace("PM","GMT") } #construct URL [string]$urlbase="http://twitter.com/statuses/friends_timeline.rss?since={0}" [string]$url=$urlbase -f $sinceGMT #this line is optional, but informational Write-Host "Downloading $url [$since]" -foregroundcolor CYAN #create web client $webclient=New-Object "System.Net.WebClient" #create network credential $username=$global:Twitter_Credential.GetNetworkCredential().Username $password=$global:Twitter_Credential.GetNetworkCredential().Password $webclient.credentials = New-Object System.Net.NetworkCredential($username,$password) #retrieve RSS file and store as an XML object [xml]$tweets=$webclient.DownloadString($url) #parse RSS/XML and write to the pipeline #only keep tweets that are newer than the last Tweet Check $tweets.rss.channel.item | where {($_.pubDate -as [datetime]) -gt $global:Tweet_Check} | select ` @{name="Twitted";Expression={ #convert to a datetime object $_.pubDate -as [datetime]}},` @{name="Twitter";Expression={ #parse out the friend name $t=$_.title $t.substring(0,$t.indexof(":"))}}, ` @{name="Tweet";Expression={ #parse out the message $t=$_.title $t.substring($t.indexof(":")+1).Trim()}} #store datetime for last tweet check $global:Tweet_Check = Get-Date }