1: param(
2: [string]$longurl = $null,
3: [string]$provider = "tinyurl"
4: );
5:
6: $script:DIGG_APPKEY = "http://devcentral.f5.com/PoshShrink";
7: $script:BITLY_LOGIN = "";
8: $script:BITLY_KEY = "";
9:
10: #----------------------------------------------------------------------------
11: # function Set-BitlyServiceInfo
12: #----------------------------------------------------------------------------
13: function Set-BitlyServiceInfo()
14: {
15: param(
16: [string]$login = $null,
17: [string]$key = $null
18: );
19: if ( $login -and $key )
20: {
21: $script:BITLY_LOGIN = $login;
22: $script:BITLY_KEY = $key;
23: }
24: else
25: {
26: Write-Error "Usage: Set-BitlyServiceInfo -login login -key key";
27: }
28: }
29:
30: #----------------------------------------------------------------------------
31: # function Shrink-Url
32: #----------------------------------------------------------------------------
33: function Shrink-Url()
34: {
35: param(
36: [string]$longurl = $null,
37: [string]$provider = "tinyurl"
38: );
39:
40: $shorturl = $null;
41: if ( $longurl )
42: {
43: switch ($provider.ToLower())
44: {
45: "is.gd" {
46: $shorturl = Execute-HTTPGetCommand "http://is.gd/api.php?longurl=$longurl";
47: }
48: "tinyurl" {
49: $shorturl = Execute-HTTPGetCommand "http://tinyurl.com/api-create.php?url=$longurl";
50: }
51: "snurl" {
52: $shorturl = Execute-HTTPGetCommand "http://snipr.com/site/snip?r=simple&link=$longurl";
53: }
54: "digg" {
55: [xml]$results = Execute-HTTPGetCommand `
56: "http://services.digg.com/url/short/create?url=${longurl}&appkey=${script:DIGG_APPKEY}";
57: $shorturl = $results.shorturls.shorturl.short_url;
58: }
59: "tr.im" {
60: [xml]$results = Execute-HTTPGetCommand "http://api.tr.im/api/trim_url.xml?url=$longurl";
61: $shorturl = $results.trim.url;
62: }
63: "bit.ly" {
64: if ( !$BITLY_LOGIN -or !$BITLY_KEY )
65: {
66: Write-Error "ERROR: You must configure your bit.ly LOGIN and APIKEY!"
67: exit;
68: }
69: else
70: {
71: $results = Execute-HTTPGetCommand `
72: "http://api.bit.ly/shorten?version=2.0.1&longUrl=$longurl&login=$script:BITLY_LOGIN&apiKey=$script:BITLY_KEY";
73: $shorturl = $results.Split("`n")[7].Split("""")[3];
74: }
75: }
76: default {
77: $shorturl = Execute-HTTPGetCommand "http://tinyurl.com/api-create.php?url=$longurl";
78: }
79: }
80: }
81: else
82: {
83: Write-Host "ERROR: Usage: Shrink-Url -longurl http://www.foo.com";
84: }
85: $shorturl;
86: }
87:
88: #----------------------------------------------------------------------------
89: # function Execute-HTTPGetCommand
90: #----------------------------------------------------------------------------
91: function Execute-HTTPGetCommand()
92: {
93: param([string] $url = $null);
94:
95: $user_agent = "PoshShrink";
96:
97: if ( $url )
98: {
99: $request = [System.Net.HttpWebRequest]::Create($url);
100: $request.UserAgent = $user_agent;
101: $request.Credentials = [System.Net.CredentialCache]::DefaultCredentials;
102: if ( $request.Proxy )
103: {
104: $request.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials;
105: }
106: $response = $request.GetResponse();
107: $rs = $response.GetResponseStream();
108: [System.IO.StreamReader]$sr = New-Object System.IO.StreamReader -argumentList $rs;
109: $sr.ReadToEnd();
110: }
111: }
112:
113: Shrink-Url -longurl $longurl -provider $provider;