| By Joe Pruitt | Article Rating: |
|
| June 19, 2009 02:04 PM EDT | Reads: |
565 |
Shrinking your Url’s is all the rage nowadays. If you are on Twitter, then odds are you have used one. Despite CodingHorror’s distaste for them in his recent blog post on Url Shorteners: Destroying the Web since 2002, they are a fact of life when we live in a world of 140 character status updates.
So what’s a URL shrinking service anyway? Well, to put it simply, you supply them with a URL, they then supply you with a shorter URL containing a lookup “key”. When future requests are made to this shorter URL, connections are routed to that services website where they convert the short URL to the original URL and issue a HTTP Redirect back to your browser to send you off to the original long url website.
So, what’s a guy, or gal, to do if they want to set their status programmatically on Twitter, Facebook, FriendFeed, or the other gazillion social networking sites out there today and need to post a very long URL? Most of these shrinking services offer API’s to access their “shrinking” services so it’s just a matter of digging into the various APIs to get them implemented. In my original PowerShell Twitter library PoshTweet, I included support for a couple for shrinking URL’s through a couple of shrinking services but I figured it would be good to separate that functionality out into it’s own library.
So, here’s my first stab at a generic URL shortening library for PowerShell. I’ve included the following services:
Some of these services have advanced features such as statistical reporting on all your submitted links. Those services require users to create accounts and register for API keys. Bit.ly is one of them, so if you want to use that service, you will have to supply this script with your bit.ly username and API key with the Set-BitlyServiceInfo function.
So, here’s the script, hope you all enjoy!
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: $response = $request.GetResponse();
102: $rs = $response.GetResponseStream();
103: [System.IO.StreamReader]$sr = New-Object System.IO.StreamReader -argumentList $rs;
104: $sr.ReadToEnd();
105: }
106: }
107:
108: Shrink-Url -longurl $longurl -provider $provider;
You can download the full contents for this script here: Shrink-Url.ps1
Read the original blog entry...
Published June 19, 2009 Reads 565
Copyright © 2009 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Joe Pruitt
Joe Pruitt is a Sr. Strategic Architect at F5 Networks working with Network and Software Architects to allow them to build network intelligence into their applications.
- The Top 150 Players in Cloud Computing
- 4th International Cloud Computing Conference & Expo Starts Today
- Yahoo! to Keynote 4th Cloud Expo: Accelerating Innovation with Cloud Computing
- Exclusive Q&A with Rich Marcello - Unisys President, Systems & Technology
- The Economics of Cloud Computing Analyzed
- Industry Experts Discuss the State of Cloud Computing
- CIA was Headed to an Enterprise Cloud All Along: Jill Tummler Singer
- Publishing Synergy: Blog, Twitter and Ulitzer
- Akamai Named “Silver Sponsor” of Cloud Computing Expo
- Cloud Computing Expo: Exclusive Q&A with Yahoo! SVP Cloud Computing
- Cloud Computing on Gartner's Top 10 List and SYS-CON Events' 2010 Calendar
- Cloud Expo New York Call for Papers Deadline December 15
- The Top 150 Players in Cloud Computing
- 4th International Cloud Computing Conference & Expo Starts Today
- Cloud CEOs, CTOs & SVPs to Speak at 4th International Cloud Computing Expo
- Yahoo! Named “Platinum Sponsor” of Cloud Computing Expo
- Yahoo! to Keynote 4th Cloud Expo: Accelerating Innovation with Cloud Computing
- SYS-CON.TV: Cloud Computing Expo Power Panel
- Exclusive Q&A with Rich Marcello - Unisys President, Systems & Technology
- The Economics of Cloud Computing Analyzed
- Unisys Named “Platinum Sponsor” of Cloud Computing Expo
- 1st Annual GovIT Expo: Letter from the Technical Chair
- Deputy CIO of the CIA to Keynote 1st Annual GovIT Expo
- Industry Experts Discuss the State of Cloud Computing
- Virtualization Conference Keynote Webcast Live on SYS-CON.TV
- The Top 150 Players in Cloud Computing
- SOA 2 Point Oh No!
- The Top 250 Players in the Cloud Computing Ecosystem
- What is Cloud Computing?
- Cloud Computing Expo Europe 2009 in Prague: Themes & Topics
- IBM's Got Its Head in the Clouds
- Cloud Computing Expo 2009 West: Call for Papers Now Closed
- Red Hat Named "Platinum Sponsor" of Virtualization Conference & Expo
- As Google's SaaS Assault Begins, Move Over Microsoft Office?
- From Enterprise to Cloud, Virtualization Today on SYS-CON.TV
- Twenty-One Experts Define Cloud Computing































