BizTalk Utilities CV ,   Jobs ,   Code library
 
Go to the front page to continue learning about XML or select below:

Contents

ReBlogger Contents

Previous posts in .NET XML, System.XML

 
 
Page 1084 of 20224

I Didn't Appreciate that TinyURL had an API

Blogger : MSDN Blogs
All posts : All posts by MSDN Blogs
Category : .NET XML, System.XML
Blogged date : 2008 Nov 10

When I created my PowerShell script I opted for SnipUrl as it has a nice API that returns a POX response with your shiny new shortened link (Digsby uses SnipUrl and as I use Digsby for most of my Twittering, most of my URLs are SnipUrls). However, Duncan mentioned he was having trouble accessing my shortened URLs and it was then I discovered that TinyURL has an API that would significantly simplify my PowerShell script. The TinyUrl API looks like this:

http://tinyurl.com/api-create.php?url=http://mikeo.co.uk

and it returns the shortened URL in the response. This means I can do away with my XmlReader and the response parsing code. I also tidied up the SubmitWebRequest function so that the request body and content-type are only set on a POST.

############################################################################## 
## 
## Tweet-Blogpost.ps1  original code by Mike Ormond (http://mikeo.co.uk)
## 
## Take a blogpost URL and post title and post a Twitter status update
## Uses TinyUrl to get a link to the post
## 
## Example Usage: 
##        $postlink = "http://blogs.msdn.com/mikeormond/archive/2008/11/07/a-bit-of-an-experiment.aspx"
##        $posttitle = "A Bit of an Experiment"
##        $twitusername = "Twitter username here"
##        $twitpassword = "Twitter password here"
##
##        .\tweet-blogpost.ps1 $postlink $post.title $twitusername $twitpassword
## 
############################################################################## 

param (    [string] $postlink, 
            [string] $posttitle, 
            [string] $twitusername, 
            [string] $twitpassword) 

[System.Reflection.Assembly]::LoadWithPartialName(System.Web") | Out-Null

function SubmitWebRequest(    [string] $RequestUrl, 
                                            [string] $RequestMethod, 
                                            [string] $RequestContentType, 
                                            [string] $PostString, 
                                            [string] $Username, 
                                            [string] $Password)
{
    $request = [System.Net.WebRequest]::Create($RequestUrl)
    
    if ($Username)
    {
        $request.Credentials = new-object System.Net.NetworkCredential($Username, $Password)
    }
    
    $request.Method = $RequestMethod
    
    if ($RequestMethod -ieq "POST")
    {
        $request.ContentType = $RequestContentType        

        $formdata = [System.Text.Encoding]::UTF8.GetBytes($PostString)
        $request.ContentLength = $formdata.Length
        $requestStream = $request.GetRequestStream()
        $requestStream.Write($formdata, 0, $formdata.Length)
        $requestStream.Close()
    }

    $response = $request.GetResponse()

    $reader = new-object System.IO.StreamReader($response.GetResponseStream())
    $returnvalue = $reader.ReadToEnd()
    $reader.Close()
    
    return $returnvalue
}

##
## Generate the snip URL from snipurl.com
##

$tinyurlrequest = "http://tinyurl.com/api-create.php?url=" `
                            + [System.Web.HttpUtility]::UrlEncode($postlink)

write-progress "Tweeting" "Getting tiny URL" -cu $tinyurlrequest

$tinyurl = SubmitWebRequest $tinyurlrequest "GET"

write-debug "Tweeting - Received tiny URL Response: $($tinyurl)"

##
## Post update to Twitter
##

## Check if we need to truncate post title as we're limited to 140 chars total
if ($posttit<