PoshCode Archive  Artifact Content

Artifact c33371e25ba94d936da05cd9efee9c8477cdf4bb87a0ad2f279b06b30cfc1c65:

  • File Resolve-Url.ps1 — part of check-in [92ddb09d7a] at 2018-06-10 13:02:16 on branch trunk — Figure out the real url’s behind the shortened forms created by snipurl, tinyurl, twurl, is.gd, ... (user: Joel Bennett size: 1726)

# 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 $_ }
   }
}