#A little script to use twitter.com services.
#03-JUN-2010
[System.Net.ServicePointManager]::Expect100Continue = $false;
function Execute-HTTPGetCommand()
{
param([string] $url = $null);
if ( $url )
{
[System.Net.WebClient]$webClient = New-Object System.Net.WebClient
$webClient.Credentials = Get-TwitterCredentials
[System.IO.Stream]$stream = $webClient.OpenRead($url);
[System.IO.StreamReader]$sr = New-Object System.IO.StreamReader -argumentList $stream;
[string]$results = $sr.ReadToEnd();
$results;
}
}
function Execute-HTTPPostCommand()
{
param([string] $url = $null, [string] $data = $null);
if ( $url -and $data )
{
[System.Net.WebRequest]$webRequest = [System.Net.WebRequest]::Create($url);
$webRequest.Credentials = Get-TwitterCredentials
$webRequest.PreAuthenticate = $true;
$webRequest.ContentType = "application/x-www-form-urlencoded";
$webRequest.Method = "POST";
$webRequest.Headers.Add("X-Twitter-Client", "Twittie");
$webRequest.Headers.Add("X-Twitter-Version", "0.1");
$webRequest.Headers.Add("X-Twitter-URL", "http://ring0a.blogspot.com");
[byte[]]$bytes = [System.Text.Encoding]::UTF8.GetBytes($data);
$webRequest.ContentLength = $bytes.Length;
[System.IO.Stream]$reqStream = $webRequest.GetRequestStream();
$reqStream.Write($bytes, 0, $bytes.Length);
$reqStream.Flush();
[System.Net.WebResponse]$resp = $webRequest.GetResponse();
$rs = $resp.GetResponseStream();
[System.IO.StreamReader]$sr = New-Object System.IO.StreamReader -argumentList $rs;
[string]$results = $sr.ReadToEnd();
$results;
}
}
$script:g_creds = $null;
function Set-TwitterCredentials()
{
param([string]$user = $null, [string]$pass = $null);
if ( $user -and $pass )
{
$script:g_creds = New-Object System.Net.NetworkCredential -argumentList ($user, $pass);
}
else
{
$creds = Get-TwitterCredentials;
}
}
function Get-TwitterCredentials()
{
if ( $null -eq $g_creds )
{
trap { Write-Error "ERROR: You must enter your Twitter credentials for PoshTweet to work!"; continue; }
$c = Get-Credential
if ( $c )
{
$user = $c.GetNetworkCredential().Username;
$pass = $c.GetNetworkCredential().Password;
$script:g_creds = New-Object System.Net.NetworkCredential -argumentList ($user, $pass);
}
}
$script:g_creds;
}
#----------------------------------------------------------------------------
# public_timeline
#----------------------------------------------------------------------------
function Get-TwitterPublicTimeline()
{
param([bool]$raw = $false);
$results = Execute-HTTPGetCommand "http://twitter.com/statuses/public_timeline.xml";
Process-TwitterStatus $results $raw;
}
#----------------------------------------------------------------------------
# friends_timeline
#----------------------------------------------------------------------------
function Get-TwitterFriendsTimeline()
{
param([bool]$raw = $false);
$results = Execute-HTTPGetCommand "http://twitter.com/statuses/friends_timeline.xml";
Process-TwitterStatus $results $raw
}
#----------------------------------------------------------------------------
#user_timeline
#----------------------------------------------------------------------------
function Get-TwitterUserTimeline()
{
param([string]$username = $null, [bool]$raw = $false);
if ( $username )
{
$username = "/$username";
}
$results = Execute-HTTPGetCommand "http://twitter.com/statuses/user_timeline$username.xml";
Process-TwitterStatus $results $raw
}
#----------------------------------------------------------------------------
# show
#----------------------------------------------------------------------------
function Get-TwitterStatus()
{
param([string]$id, [bool]$raw = $false);
if ( $id )
{
$results = Execute-HTTPGetCommand "http://twitter.com/statuses/show/" + $id + ".xml";
Process-TwitterStatus $results $raw;
}
}
#----------------------------------------------------------------------------
# update
#----------------------------------------------------------------------------
function Set-TwitterStatus()
{
param([string]$status);
#$encstatus = [System.Web.HttpUtility]::UrlEncode("$status");
[System.Reflection.Assembly]::LoadWithPartialName("System.web")
$encstatus = [System.Web.HttpUtility]::UrlEncode("$status");
$results = Execute-HTTPPostCommand "http://twitter.com/statuses/update.xml" "status=$encstatus";
Process-TwitterStatus $results $raw;
}
#----------------------------------------------------------------------------
# replies
#----------------------------------------------------------------------------
function Get-TwitterReplies()
{
param([bool]$raw = $false);
$results = Execute-HTTPGetCommand "http://twitter.com/statuses/replies.xml";
Process-TwitterStatus $results $raw;
}
#----------------------------------------------------------------------------
# destroy
#----------------------------------------------------------------------------
function Destroy-TwitterStatus()
{
param([string]$id = $null);
if ( $id )
{
Execute-HTTPPostCommand "http://twitter.com/statuses/destroy/$id.xml", "id=$id";
}
}
function Process-TwitterStatus()
{
param([string]$sxml = $null, [bool]$raw = $false);
if ( $sxml )
{
if ( $raw )
{
$sxml;
}
else
{
[xml]$xml = $sxml;
if ( $xml.statuses.status )
{
$stats = $xml.statuses.status;
}
elseif ($xml.status )
{
$stats = $xml.status;
}
$stats | Foreach-Object -process {
$info = "by " + $_.user.screen_name + ", " + $_.created_at;
if ( $_.source ) { $info = $info + " via " + $_.source; }
if ( $_.in_reply_to_screen_name ) { $info = $info + " in reply to " + $_.in_reply_to_screen_name; }
"-------------------------";
$_.text;
$info;
};
"-------------------------";
}
}
}
#EOS
No comments:
Post a Comment