For more information regarding the security incident at F5, the actions we are taking to address it, and our ongoing efforts to protect our customers, click here.

Ps Twitter Api

Problem this snippet solves:

Here is a set of PowerShell functions that give access to the Twitter web based APIs allowing you to interact with the Twitter service from within Windows PowerShell.

How to use this snippet:

Twitter Functions

Internal Helpers

function Shrink-Url()
function Shrink-Links()
function Set-TwitterCredentials()
function Get-TwitterCredentials()
function Execute-HTTPGetCommand()
function Execute-HTTPPostCommand()
function Process-TwitterStatus()

Status

function Get-TwitterPublicTimeline()
function Get-TwitterFriendsTimeline()
function Get-TwitterUserTimeline()
function Get-TwitterStatus()
function Set-TwitterStatus()
function Get-TwitterReplies()
function Destroy-TwitterStatus()

User

function Process-TwitterUsers()
function Get-TwitterFriends()
function Get-TwitterFollowers()
function Get-TwitterUser()

Direct Messages

function Process-TwitterDirectMessages()
function Get-TwitterDirectMessages()
function Get-TwitterSentDirectMessages()
function New-TwitterDirectMessage()
function Destroy-TwitterDirectMessage()

Friendship

function Get-TwitterFriendshipExists
function Process-Ids()
function Get-TwitterSocialGraphFriendIds()
function Get-TwitterSocialGraphFollowerIds()

Account

function Is-ValidHexColor()
function Verify-TwitterCredentials()
function End-TwitterSession()
function Set-TwitterDeliveryDevice()
function Set-TwitterProfileColors()
function Set-TwitterProfileImage()
function Set-TwitterProfileBackgroundImage()
function Get-TwitterRateLimitStatus()
function Set-TwitterProfile()

Favorites

function Get-TwitterFavorites()
function Create-TwitterFavorite()
function Destroy-TwitterFavorite()

Notification

function Follow-TwitterUser()
function Leave-TwitterUser()

Block

function Create-TwitterBlock()
function Destroy-TwitterBlock()

Help

function Get-TwitterHelpTest()
function Get-TwitterDowntimeSchedule()

3rd Party Services

function Get-TwitterCounterStats()
function Get-GroupMemberList()

Code :

#----------------------------------------------------------------------------
# The contents of this file are subject to the "END USER LICENSE AGREEMENT 
# FOR F5 Software Development Kit for iControl"; you may not use this file 
# except in compliance with the License. The License is included in the 
# iControl Software Development Kit.
#
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
# the License for the specific language governing rights and limitations
# under the License.
#
# The Original Code is iControl Code and related documentation
# distributed by F5.
#
# The Initial Developer of the Original Code is F5 Networks,
# Inc. Seattle, WA, USA. Portions created by F5 are Copyright (C) 1996-2008 
# F5 Networks, Inc. All Rights Reserved.  iControl (TM) is a registered 
# trademark of F5 Networks, Inc.
#
# Alternatively, the contents of this file may be used under the terms
# of the GNU General Public License (the "GPL"), in which case the
# provisions of GPL are applicable instead of those above.  If you wish
# to allow use of your version of this file only under the terms of the
# GPL and not to allow others to use your version of this file under the
# License, indicate your decision by deleting the provisions above and
# replace them with the notice and other provisions required by the GPL.
# If you do not delete the provisions above, a recipient may use your
# version of this file under either the License or the GPL.
#----------------------------------------------------------------------------
[void] [Reflection.Assembly]::LoadWithPartialName("System.Web") 
[void] [Reflection.Assembly]::LoadWithPartialName("System.Text") 
[System.Net.ServicePointManager]::Expect100Continue = $false;

$script:g_creds = $null;
#============================================================================
# Shared Functions
#============================================================================
#----------------------------------------------------------------------------
# function Shrink-Url
#----------------------------------------------------------------------------
function Shrink-Url()
{
  param([string]$longurl = $null, [string]$provider = "tinyurl");
  $shorturl = $null;
  if ( $longurl )
  {
    switch ($provider.ToLower())
    {
      "is.gd" {
        $shorturl = Execute-HTTPGetCommand "http://is.gd/api.php?longurl=$longurl";
      }
      "tinyurl" {
        $shorturl = Execute-HTTPGetCommand "http://tinyurl.com/api-create.php?url=$longurl";
      }
      "snipurl" {
        $shorturl = Execute-HTTPGetCommand "http://snipurl.com/site/snip?r=simple&link=$longurl";
      }
      default {
        $shorturl = Execute-HTTPGetCommand "http://tinyurl.com/api-create.php?url=$longurl";
      }
    }
  }
  $shorturl;
}

#----------------------------------------------------------------------------
# function Shrink-Links
#----------------------------------------------------------------------------
function Shrink-Links()
{
  param([string]$text = $null, [string]$provider = "tinyurl");

  [regex]$regex="(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?"

  if ($text -match $regex)
  {
    $link = $matches[0]
  }
  
  #if an embedded url was found, convert it to a shortened link
  if ($link)
  {
    $shrunklink = Shrink-Url $link
    $text = $text.Replace($link, $shrunklink)
  }
  $text;
}

#----------------------------------------------------------------------------
# function Set-TwitterCredentials
#----------------------------------------------------------------------------
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
#----------------------------------------------------------------------------
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;
}

#----------------------------------------------------------------------------
# function Execute-HTTPGetCommand
#----------------------------------------------------------------------------
function Execute-HTTPGetCommand()
{
  param([string] $url = $null);
  if ( $url )
  {
    [System.Net.WebClient]$webClient = New-Object System.Net.WebClient
    if ( $url.ToLower().Contains("twitter.com") )
    {
      $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
#----------------------------------------------------------------------------
function Execute-HTTPPostCommand()
{
  param([string] $url = $null, [string] $data = $null);

  if ( $url -and $data )
  {
    [System.Net.WebRequest]$webRequest = [System.Net.WebRequest]::Create($url);
    $webRequest.ServicePoint.Expect100Continue = $false;
    if ( $url.ToLower().Contains("twitter.com") )
    {
      $webRequest.Credentials = Get-TwitterCredentials
      $webRequest.PreAuthenticate = $true;
    }
    $webRequest.ContentType = "application/x-www-form-urlencoded";
    $webRequest.Method = "POST";
    $webRequest.Headers.Add("X-Twitter-Client", "PoshTweet");
    $webRequest.Headers.Add("X-Twitter-Version", "1.0");
    $webRequest.Headers.Add("X-Twitter-URL", "http://devcentral.f5.com/s/poshtweet");
  
    [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;
  }
}

#============================================================================
# Status Functions
#============================================================================
#----------------------------------------------------------------------------
# Helper functions
#----------------------------------------------------------------------------
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;
      };
      "-------------------------";
    }
  }
}

#----------------------------------------------------------------------------
# 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 (
    [string]$since = $null, 
    [int]$since_id = $null,
    [int]$count = $null,
  [int]$page = $null,
    [bool]$raw = $false
  );
  if ( $since )
  {
    $args = "?since=$since";
  }
  if ( $since_id )
  {
    if ( $args ) { $args = "${args}&" } else { $args = "?" }
    $args = "${args}since_id=$since_id";
  }
  if ( $count )
  {
    if ( $args ) { $args = "${args}&" } else { $args = "?" }
    $args = "${args}count=$count";
  }
  if ( $page )
  {
    if ( $args ) { $args = "${args}&" } else { $args = "?" }
    $args = "${args}page=$page";
  }
  
  Write-Host "Requesting URI: http://twitter.com/statuses/friends_timeline.xml${args}";
  $results = Execute-HTTPGetCommand "http://twitter.com/statuses/friends_timeline.xml${args}";
  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, [int]$in_reply_to_status_id = $null, [bool]$raw = $false);
  $encstatus = [System.Web.HttpUtility]::UrlEncode("$status");

  $args = "status=$encstatus";
  if ( $in_reply_to_status_id )
  {
    $args += "&in_reply_to_status_id=${in_reply_to_status_id}";
  }
  $results = Execute-HTTPPostCommand "http://twitter.com/statuses/update.xml" $args;
  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";
  }
}

#============================================================================
# User Functions
#============================================================================
#----------------------------------------------------------------------------
# Helper functions
#----------------------------------------------------------------------------
function Process-TwitterUsers()
{
  param([string]$sxml = $null, [bool]$raw = $false);
  if ( $sxml )
  {
    if ( $raw )
    {
      $sxml;
    }
    else
    {
      [xml]$xml = $sxml;
      if ( $xml.users.user )
      {
        $user = $xml.users.user;
      }
      elseif ($xml.user )
      {
        $user = $xml.user;
      }
      $user | Foreach-Object -process {
        $id = $_.id;
        $screen_name = $_.screen_name;
        $name = $_.name;
        $desc = $_.description;
        $url = $_.url;
        $followers_count = $_.followers_count;
        $last_status = $_.status.text;
        
        "-------------------------";
        "$screen_name / $name (# $id , $followers_count followers) / $url";
        "Bio: $desc";
        "Tweet: $last_status";
      };
      "-------------------------";
    }
  }
}

#----------------------------------------------------------------------------
# friends
#----------------------------------------------------------------------------
function Get-TwitterFriends()
{
  param([string]$id_or_screenname, [string]$page = $null, [bool]$raw = $false);
  if ( $id_or_screenname )
  {
    $id_or_screenname = "/$id_or_screenname";
  }
  if ( $page )
  {
    $page = "?page=$page";
  }
  Write-Host "Requesting: http://twitter.com/statuses/friends$id_or_screenname.xml$page";
  $results = Execute-HTTPGetCommand "http://twitter.com/statuses/friends$id_or_screenname.xml$page";
  Process-TwitterUsers $results $raw;
}

#----------------------------------------------------------------------------
# followers
#----------------------------------------------------------------------------
function Get-TwitterFollowers()
{
  param([string]$id_or_screenname, [string]$page = $null, [bool]$raw = $false);
  if ( $id_or_screenname )
  {
    $id_or_screenname = "/$id_or_screenname";
  }
  if ( $page )
  {
    $page = "?page=$page";
  }
  Write-Host "Requesting: http://twitter.com/statuses/followers$id_or_screenname.xml$page";
  $results = Execute-HTTPGetCommand "http://twitter.com/statuses/followers$id_or_screenname.xml$page";
  Process-TwitterUsers $results $raw;
}

#----------------------------------------------------------------------------
# show
#----------------------------------------------------------------------------
function Get-TwitterUser()
{
  param([string]$id = $null, [string]$email = $null, [bool]$raw = $false);
  $url = $null;
  if ( $id )
  {
    $url = "http://twitter.com/users/show/$id.xml";
  }
  elseif ( $email )
  {
    $url = "http://twitter.com/users/show.xml?email=$email";
  }
  if ( $url )
  {
    $results = Execute-HTTPGetCommand $url;
    PRocess-TwitterUsers $results $raw;
  }
}

#============================================================================
# Direct Message Functions
#============================================================================
#----------------------------------------------------------------------------
# Helper functions
#----------------------------------------------------------------------------
function Process-TwitterDirectMessages()
{
  param([string]$sxml = $null, [bool]$raw = $false);
  if ( $sxml )
  {
    if ( $raw )
    {
      $sxml;
    }
    else
    {
      [xml]$xml = $sxml;
      if ( $xml.{direct-messages}.direct_message )
      {
        $dm = $xml.{direct-messages}.direct_message;
      }
      elseif ($xml.direct_message )
      {
        $dm = $xml.direct_message;
      }
      $dm | Foreach-Object -process {
        $id = $_.id;
        $text = $_.text;
        $sender_screen_name = $_.sender_screen_name;
        $name = $_.sender.name;
        $created_at = $_.created_at;
        "-------------------------";
        "(DM #$id) $text";
        "by $sender_screen_name / $name on $created_at";
      };
      "-------------------------";
    }
  }
}

#----------------------------------------------------------------------------
# direct_messages
#----------------------------------------------------------------------------
function Get-TwitterDirectMessages()
{
  param([string]$since = $null, [string]$since_id = $null, [string]$page = $null, [bool]$raw = $false);
  $args = $null;
  if ( $since )
  {
    $args = "?since=$since";
  }
  elseif ( $since_id )
  {
    $args = "?since_id=$since_id";
  }
  elseif ( $page )
  {
    $args = "?page=$page";
  }
  $results = Execute-HTTPGetCommand "http://twitter.com/direct_messages.xml$args";
  Process-TwitterDirectMessages $results $raw;
}

#----------------------------------------------------------------------------
# sent
#----------------------------------------------------------------------------
function Get-TwitterSentDirectMessages()
{
  param([string]$since = $null, [string]$since_id = $null, [string]$page = $null, [bool]$raw = $false);
  $args = $null;
  if ( $since )
  {
    $args = "?since=$since";
  }
  elseif ( $since_id )
  {
    $args = "?since_id=$since_id";
  }
  elseif ( $page )
  {
    $args = "?page=$page";
  }
  $results = Execute-HTTPGetCommand "http://twitter.com/direct_messages/sent.xml$args";
  Process-TwitterDirectMessages $results $raw;
}

#----------------------------------------------------------------------------
# new
#----------------------------------------------------------------------------
function New-TwitterDirectMessage()
{
  param([string]$user = $null, [string]$text = $null, [bool]$raw = $false);
  if ( $user -and $text )
  {
    $enctext = [System.Web.HttpUtility]::UrlEncode("$text");
    $results = Execute-HTTPPostCommand "http://twitter.com/direct_messages/new.xml" "user=$user&text=$enctext"
    Process-TwitterDirectMessages $results $raw;
  }
}

#----------------------------------------------------------------------------
# destroy
#----------------------------------------------------------------------------
function Destroy-TwitterDirectMessage()
{
  param([string]$id = $null);
  if ( $id )
  {
    $results = Execute-HTTPPostCommand "http://twitter.com/direct_messages/destroy/$id.xml" "id=$id";
  }
}

#============================================================================
# Friendship Functions
#============================================================================
#----------------------------------------------------------------------------
# create
#----------------------------------------------------------------------------
function Create-TwitterFriendship()
{
  param([string]$id = $null, [bool]$follow = $true, [bool]$raw = $false);
  if ( $id )
  {
  $follow = $follow.ToString().ToLower();
    $results = Execute-HTTPPostCommand "http://twitter.com/friendsips/create/${id}.xml" "id=${id}&follow=${follow}"
    Process-TwitterUsers $results $raw;
  }
}

#----------------------------------------------------------------------------
# destroy
#----------------------------------------------------------------------------
function Destroy-TwitterFriendship()
{
  param([string]$id = $null, [bool]$raw = $false);
  if ( $id )
  {
    $results = Execute-HTTPPostCommand "http://twitter.com/friendships/destroy/${id}.xml" "id=${id}"
    $results;
    Process-TwitterUsers $results $raw;
  }
}

#----------------------------------------------------------------------------
# exists
#----------------------------------------------------------------------------
function Get-TwitterFriendshipExists
{
  param([string]$user_a = $null, [string]$user_b = $null);
  if ( $user_a -and $user_b )
  {
    $results = Execute-HTTPGetCommand "http://twitter.com/friendships/exists.xml?user_a=${user_a}&user_b=${user_b}";
  $results;
  }
}

#============================================================================
# Social Graph Functions
#============================================================================
#----------------------------------------------------------------------------
# Helper Functions
#----------------------------------------------------------------------------
function Process-Ids()
{
  param([string]$sxml = $null, [bool]$raw = $false);
  if ( $sxml )
  {
    if ( $raw )
    {
      $sxml;
    }
    else
    {
      [xml]$x = $sxml;
      $i = 1;
      $x.ids.id | ForEach-Object -Process {
        $info = "[$i] : " + $_;
        $info;
        $i++;
      }
    }
  }
}

#----------------------------------------------------------------------------
# ids (friends)
#----------------------------------------------------------------------------
function Get-TwitterSocialGraphFriendIds()
{
  param([string]$id = $null, [bool]$raw = $false);
  $args = "";
  if ( $id )
  {
    $args = "?id=${id}";
  }
  $results = Execute-HTTPGetCommand "http://twitter.com/friends/ids.xml$args";
  Process-Ids $results $raw;
}


#----------------------------------------------------------------------------
# ids (followers)
#----------------------------------------------------------------------------
function Get-TwitterSocialGraphFollowerIds()
{
  param([string]$id = $null, [bool]$raw = $false);
  $args = "";
  if ( $id )
  {
    $args = "?id=${id}";
  }
  $results = Execute-HTTPGetCommand "http://twitter.com/followers/ids.xml$args";
  Process-Ids $results $raw;
}


#============================================================================
# Account Functions
#============================================================================
#----------------------------------------------------------------------------
# helper functions
#----------------------------------------------------------------------------
function Is-ValidHexColor()
{
  param([string]$color_in_hex = $null)
  [bool]$valid = $false;
  if ( $color_in_hex )
  {
    [regex]$regex3 = "[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]";
    [regex]$regex6 = "[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]";
    if ( ($color_in_hex.Length -eq 3) -and ($color_in_hex -match $regex3) )
    {
      $valid = $true;
    }
    elseif ( ($color_in_hex.Length -eq 6) -and ($color_in_hex -match $regex6) )
    {
      $valid = $true;
    }
  }
  $valid;
}

#----------------------------------------------------------------------------
# verify_credentials
#----------------------------------------------------------------------------
function Verify-TwitterCredentials()
{
  $results = Execute-HTTPGetCommand "http://twitter.com/account/verify_credentials.xml";
  $results;
}

#----------------------------------------------------------------------------
# end_session
#----------------------------------------------------------------------------
function End-TwitterSession()
{
  $results = Execute-HTTPPostCommand "http://twitter.com/account/end_session.xml" "";
  $results;
}

#----------------------------------------------------------------------------
# update_delivery_device
#----------------------------------------------------------------------------
function Set-TwitterDeliveryDevice()
{
  param([string]$device = $null);
  if ( $device )
  {
    if ( $device.Equals("sms") -or $device.Equals("im") -or $device.Equals("none") )
    {
      $results = Execute-HTTPPostCommand "http://twitter.com/account/update_delivery_device.xml?device=$device" "device=$device";
      $results;
    }
    else
    {
        Write-Error "device must be either sms, im, or none!"
    }
  }
}

#----------------------------------------------------------------------------
# update_profile_colors
#----------------------------------------------------------------------------
function Set-TwitterProfileColors()
{
  param(
    [string]$background_color = $null,
    [string]$text_color = $null,
    [string]$link_color = $null,
    [string]$sidebar_fill_color = $null,
    [string]$sidebar_border_color = $null);
  $args = $null;
  if ( $background_color )
  {
    if ( Is-ValidHexColor $background_color )
    {
      if ( $args ) { $args = "$args&"; }
      $args = "${args}profile_background_color=$background_color";
    }
    else
    {
      Write-Error "background_color '$background_color' must be a 3 or 6 valid hexadecimal value!";
    }
  }
  if ( $text_color )
  {
    if ( Is-ValidHexColor $text_color )
    {
      if ( $args ) { $args = "$args&"; }
      $args = "${args}profile_text_color=$text_color";
    }
    else
    {
      Write-Error "text_color '$text_color' must be a 3 or 6 valid hexadecimal value!";
    }
  }
  if ( $link_color )
  {
    if ( Is-ValidHexColor $link_color )
    {
      if ( $args ) { $args = "$args&"; }
      $args = "${args}profile_link_color=$link_color";
    }
    else
    {
      Write-Error "link_color '$link_color' must be a 3 or 6 valid hexadecimal value!";
    }
  }
  if ( $sidebar_fill_color )
  {
    if ( Is-ValidHexColor $sidebar_fill_color )
    {
      if ( $args ) { $args = "$args&"; }
      $args = "${args}profile_sidebar_fill_color=$sidebar_fill_color";
    }
    else
    {
      Write-Error "sidebar_fill_color '$sidebar_fill_color' must be a 3 or 6 valid hexadecimal value!";
    }
  }
  if ( $sidebar_border_color )
  {
    if ( Is-ValidHexColor $sidebar_border_color )
    {
      if ( $args ) { $args = "$args&"; }
      $args = "${args}profile_sidebar_border_color=$sidebar_border_color";
    }
    else
    {
      Write-Error "sidebar_border_color '$sidebar_border_color' must be a 3 or 6 valid hexadecimal value!";
    }
  }
  if ( $args )
  {
    Write-Host "http://twitter.com/account/update_profile_colors.xml - $args";
    $results = Execute-HTTPPostCommand "http://twitter.com/account/update_profile_colors.xml" $args;
    $results;
  }
}

#----------------------------------------------------------------------------
# update_profile_image
#----------------------------------------------------------------------------
function Set-TwitterProfileImage()
{
  param([string]$image = $null);
  if ( $image )
  {
    $results = Execute-HTTPPostCommand "http://twitter.com/account/update_profile_image.xml" "image=$image";
    $results;
  }
}

#----------------------------------------------------------------------------
# update_profile_background_image
#----------------------------------------------------------------------------
function Set-TwitterProfileBackgroundImage()
{
  param([string]$image = $null);
  if ( $image )
  {
    $results = Execute-HTTPPostCommand "http://twitter.com/account/update_profile_background_image.xml" "image=$image";
    $results;
  }
}

#----------------------------------------------------------------------------
# rate_limit_status
#----------------------------------------------------------------------------
function Get-TwitterRateLimitStatus()
{
  param([bool]$raw = $false);
  $sXml = Execute-HTTPGetCommand "http://twitter.com/account/rate_limit_status.xml";
  if ( $sXml )
  {
    if ( $raw )
  {
    $sXml;
  }
  else
  {
    [xml]$results = $sXml;
      $remaining_hits = $results.hash.{remaining-hits}.get_InnerText();
      $hourly_limit = $results.hash.{hourly-limit}.get_InnerText();
      $reset_time_in_seconds = $results.hash.{reset-time-in-seconds}.get_InnerText();
      $reset_time = $results.hash.{reset-time}.get_InnerText();
      "Remaining Hits: $remaining_hits";
      "Hourly Limit  : $hourly_limit";
      "Reset Time (s): $reset_time_in_seconds s.";
      "Reset Time    : $reset_time";
    }
  }
}

#----------------------------------------------------------------------------
# update_profile
#----------------------------------------------------------------------------
function Set-TwitterProfile()
{
  param(
    [string]$name = $null,
    [string]$email = $null,
    [string]$url = $null,
    [string]$location = $null,
    [string]$description = $null);
  $args = $null;
  if ( $name )
  {
    if ( $args ) { $args = "$args&"; }
    $args = "${args}name=$name";
  }
  if ( $email )
  {
    if ( $args ) { $args = "$args&"; }
    $args = "${args}email=$email";
  }
  if ( $url )
  {
    if ( $args ) { $args = "$args&"; }
    $args = "${args}url=$url";
  }
  if ( $location )
  {
    if ( $args ) { $args = "$args&"; }
    $args = "${args}location=$location";
  }
  if ( $description )
  {
    if ( $args ) { $args = "$args&"; }
    $args = "${args}description=$description";
  }
  if ( $args )
  {
    $results = Execute-HTTPPostCommand "http://twitter.com/account/update_profile.xml" $args;
    $results;
  }
}

#============================================================================
# Favorite Functions
#============================================================================
#----------------------------------------------------------------------------
# favorites
#----------------------------------------------------------------------------
function Get-TwitterFavorites()
{
  param([string]$id, [string]$page = $null, [bool]$raw = $false);
  if ( $id )
  {
    $id = "/$id";
  }
  if ( $page )
  {
    $page = "?page=$page";
  }
  $results = Execute-HTTPGetCommand "http://twitter.com/favorites${id}.xml${page}";
  Process-TwitterStatus $results $raw;
}

#----------------------------------------------------------------------------
# create
#----------------------------------------------------------------------------
function Create-TwitterFavorite()
{
  param([string]$id, [bool]$raw = $false);
  if ( $id )
  {
    $id = "/$id";
  }
  $results = Execute-HTTPPostCommand "http://twitter.com/favorites/create${id}.xml" "id=${id}";
  Process-TwitterStatus $results $raw;
}

#----------------------------------------------------------------------------
# destroy
#----------------------------------------------------------------------------
function Destroy-TwitterFavorite()
{
  param([string]$id, [bool]$raw = $false);
  if ( $id )
  {
    $id = "/$id";
  }
  $results = Execute-HTTPPostCommand "http://twitter.com/favorites/destroy${id}.xml" "id=${id}";
  Process-TwitterStatus $results $raw;
}


#============================================================================
# Notification Functions
#============================================================================
#----------------------------------------------------------------------------
# follow
#----------------------------------------------------------------------------
function Follow-TwitterUser()
{
  param([string]$id, [bool]$raw = $false);
  if ( $id )
  {
    $results = Execute-HTTPPostCommand "http://twitter.com/notifications/follow/${id}.xml" "id=${id}";
    Process-TwitterUsers $results $raw;
  }
}

#----------------------------------------------------------------------------
# leave
#----------------------------------------------------------------------------
function Leave-TwitterUser()
{
  param([string]$id, [bool]$raw = $false);
  if ( $id )
  {
    $results = Execute-HTTPPostCommand "http://twitter.com/notifications/leave/${id}.xml" "id=${id}";
    Process-TwitterUsers $results $raw;
  }
}

#============================================================================
# Block Functions
#============================================================================
#----------------------------------------------------------------------------
# create
#----------------------------------------------------------------------------
function Create-TwitterBlock()
{
  param([string]$id, [bool]$raw = $false);
  if ( $id )
  {
    $results = Execute-HTTPPostCommand "http://twitter.com/blocks/create/${id}.xml" "id=${id}";
    Process-TwitterUsers $results $raw;
  }
}

#----------------------------------------------------------------------------
# destroy
#----------------------------------------------------------------------------
function Destroy-TwitterBlock()
{
  param([string]$id, [bool]$raw = $false);
  if ( $id )
  {
    $results = Execute-HTTPPostCommand "http://twitter.com/blocks/destroy/${id}.xml" "id=${id}";
    Process-TwitterUsers $results $raw;
  }
}


#============================================================================
# Help Functions
#============================================================================
#----------------------------------------------------------------------------
# test
#----------------------------------------------------------------------------
function Get-TwitterHelpTest()
{
  $results = Execute-HTTPGetCommand "http://twitter.com/help/test.xml";
  $results;
}

#----------------------------------------------------------------------------
# downtime_schedule
#----------------------------------------------------------------------------
function Get-TwitterDowntimeSchedule()
{
  $results = Execute-HTTPGetCommand "http://twitter.com/help/downtime_schedule.xml";
  $results;
}

#============================================================================
# TwitterCounter
#============================================================================
#----------------------------------------------------------------------------
# Get-TwitterCounterStats
#----------------------------------------------------------------------------
function Get-TwitterCounterStats()
{
  param([string]$username = $null, [int]$results = 14, [bool]$raw = $false);
  $output = "xml";
  $stats = $null;
  if ( $username )
  {
    $stats = Execute-HTTPGetCommand "http://twittercounter.com/api/?username=$username&output=$output&results=$results"
  }
  if ( !$raw )
  {
    $stats = [xml]$stats;
    $stats.twittercounter
  }
  else
  {
    $stats;
  }
}

#============================================================================
# Utilities
#============================================================================
function Get-GroupMemberList()
{
  param([string]$url = $null, [string]$tagstart = $null);
  if ( $tagstart -eq $null ) { $tagstart = "entrybody"; }
  
  # Posh Twitter Users "http://www.mindofroot.com/powershell-twitterers/";  
  if ( $url )
  {
    $site = Execute-HTTPGetRequest $url;
    
    $previous = @()
    $start = $site.IndexOf('
') $site = $site.substring($start) $start = $site.IndexOf('
    ') $site = $site.substring($start) $end = $site.IndexOf('
') + 5 [xml]$doc = $site.substring(0,$end) $results = $doc.ul.li | select @{name='Name';Expression={$_.a.'#text'}}, @{name='TwitterURL';Expression={$_.a.href}}, @{name='UserName';Expression={$_.a.href -replace 'http://twitter.com/'}} $results = $results[1..($results.count-1)] return $results } }
Published Mar 09, 2015
Version 1.0
No CommentsBe the first to comment