# encoding: ascii
# api: powershell
# title: Resolve-Url
# description: Figure out the real url’s behind the shortened forms created by snipurl, tinyurl, twurl, is.gd, ...
# version: 0.1
# type: function
# author: Joel Bennett
# license: CC0
# function: Resolve-URL
# x-poshcode-id: 187
# x-archived: 2017-05-30T19:02:38
# x-published: 2008-04-26T15:13:00
#
#
###################################
## Figure out the real url behind those shortened forms
function Resolve-URL([string[]]$urls) { 
   [regex]$snip  = "(?:https?://)?(?:snurl|snipr|snipurl)\.com/([^?/ ]*)\b"
   [regex]$tiny  = "(?:https?://)?TinyURL.com/([^?/ ]*)\b"
   [regex]$isgd  = "(?:https?://)?is.gd/([^?/ ]*)\b"
   [regex]$twurl = "(?:https?://)?twurl.nl/([^?/ ]*)\b"

   switch -regex ($urls) {
      $snip {
         write-output $snip.Replace( $_, (new-object System.Net.WebClient
            ).DownloadString("http://snipurl.com/resolveurl?id=$($snip.match( $_ ).groups[1].value)"))
      }
      $tiny {
         $doc = ConvertFrom-Html "http://tinyurl.com/preview.php?num=$($tiny.match( $_ ).groups[1].value)"
         write-output $tiny.Replace( $_, "$($doc.SelectSingleNode(""//a[@id='redirecturl']"").href)" )
      }
      $isgd {
         $doc = ConvertFrom-Html "http://is.gd/$($isgd.match( $_ ).groups[1].value)-"
         write-output $isgd.Replace( $_, "$($doc.SelectSingleNode(""//div[@id='main']/p/a"").href)") 
      }
      $twurl {
         $doc = ConvertFrom-Html "http://tweetburner.com/links/$($twurl.match( $_ ).groups[1].value)"
         write-output $twurl.Replace($_, "$($doc.selectsingleNode(""//div[@id='main-content']/p/a"").href)" )
      }
      default { write-output $_ }
   }
}
