Reading a tweeters timeline using PowerShell-Take2


There was a to do list on “Reading a tweeters timeline using PowerShell“, and now it is no more. What I learned while doing this exercise is scoping of variables is important and formatting is a very important part of the user experience (yes, even on a console).

Here is what the list on part 1 contained:

  • I would’ve liked to throw in some more formatting but, since this was a proof of concept script I did not get it in. I would like to get back to this sometime in the near future get the output a little more readable. Done: formatting in place. Each tweet, is divided into 3 lines. Each line being 47 characters wide. Tweets alternate in color.
  • I would like to process this for a list of users. Done: Accepts a comma separated string of users whose tweets you want to read.

 

Function Get-TwitterStream{
##
#.SYNOPSIS
# Get the Twitter time line of a user or a set of users.
#.DESCRIPTION
# Get the Twitter time line of a user or a set of users using the REST API.
# 
# The api allows us to retrieve the timelines of users when queried:
# http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=UserName
# 
# You can retrieve multiple pages but, for this script we only retrieve top
# twenty tweets in a single page.
# 
# Since, the function expects a string argument when passing multiple parameters
# you need to pass them as: "handle#1, handle#2, ...handle#n"
# 
#.PARAMETER TwitterHandle
# A user's "Twitter handle" is the username they have selected.
# 
#.EXAMPLE
# Get the Twitter timeline of a single user:
# Get-TwitterStream -TwitterHandle SqlChow
# 
#.EXAMPLE
# When getting the Twitter timeline of multiple users 
# Get-TwitterStream -TwitterHandle "SqlChow, BrentO, PaulRandal"
# 
#.NOTES
# ###########################################################################
# #
# # NAME: Get-TwitterStream
# #
# # AUTHOR: @SqlChow
# #
# # COMMENT: Get-TwitterStream -TwitterHandle "SqlChow, BrentO,realpreityzinta"
# #
# # COPYRIGHT: © 2011 SqlChow. Code provided as is. User discretion advised.
# #
# # VERSION HISTORY:
# # 1.0 12/25/2011 - Initial release
# # 1.1 12/27/2011 - Completed todo list
# ########################################################################### 

param(
[parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[String]
$TwitterHandle
) 

Begin
{
$script:flip = $true
$origColor = $Host.ui.rawui.ForegroundColor 

Function Color-Tweet ($text){
if($flip){$Host.UI.RawUI.ForegroundColor="cyan"; "{0,4}" -f "$text `n";$script:flip=$false}
else{$Host.ui.rawui.foregroundcolor = "yellow";"$text `n";$script:flip=$true}
} 

$colorTweet = @{
label = "Tweets:" ;
Expression = { Color-Tweet $_.text };
Width = 47; Alignment="Left"
} 

} 

Process
{
try
{
$objWebClient = New-Object System.Net.WebClient 

$arryOfHandles = $TwitterHandle.Split(",")
foreach($handle in $arryOfHandles)
{
Write-Host "`@"$handle.Trim()" tweets:" -Fore Green -Back Black
$strTwitURL = "http://api.twitter.com/1/statuses/user_timeline.xml?screen_name="+ $handle.Trim() + "&count=20&page=1"
Write-Debug $strTwitUrl
$strContentString = $objWebClient.DownloadString($strTwitURL)
$xmlContents = [xml]$strContentString 

#$xmlContents.statuses.ChildNodes|ForEach-Object{ if ($flip) { $flip=$false;$Host.ui.rawui.ForegroundColor = "cyan"; "{0,4}" -f $_.text;"`n";} else { $flip=$true;$Host.ui.rawui.foregroundcolor = "yellow";"{0,4}" -f $_.text;"`n";} }
#$xmlContents.statuses.ChildNodes|Add-Member NoteProperty Tweeter $handle -PassThru|Format-Table $colorTweet, Tweeter -Wrap
$xmlContents.statuses.ChildNodes| Format-Table $colorTweet -Wrap -HideTableHeaders
$Host.ui.rawui.ForegroundColor = $origColor
}
}
catch
{
Write-Error "Unable to get stream data"
}
finally
{
$objWebClient.Dispose()
$Host.ui.rawui.ForegroundColor = $origColor
}
}
}
 
Nicely formatted

Much better to look at

Advertisements
About

By profession, I’m a SQL Server Database Administrator. I love to poke my nose into different corners and see how stuff looks in there. I keep looking for new things to do as my mind refuses to settle on one topic.

Tagged with: , , , , ,
Posted in PowerShell
2 comments on “Reading a tweeters timeline using PowerShell-Take2
  1. Doug Finke says:

    Nice work!

    I like your stream approach, I need to re-think how I could re-use it.
    Here a couple PowerShell Twitter posts I did. Wondering if you had any feedback.

    PowerShell, ShowUI and the Twitter API
    http://www.dougfinke.com/blog/index.php/2011/06/26/powershell-showui-and-the-twitter-api/

    Use PowerShell V3 to Find Out About Your Twitter Followers
    http://www.dougfinke.com/blog/index.php/2011/11/23/use-powershell-v3-to-find-out-about-your-twitter-followers/

    Thanks
    Doug

    • Hemanth says:

      It is an honor to have an MVP from the community, take the time to read the blog. Thanks Doug, really glad that you liked the post.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: