Friday, June 10, 2011

Shorten a URL on bit.ly using CURL

I often have to shorten URLs when I'm tweeting or simply sending a URL to a friend via email or on instant messenger. Since I generally have a terminal window open I figured it would be expeditious to come up with a shell script to do this for me.

The first thing you have to do is sign up for an account on bit.ly if you haven't already, next you have to obtain an API key. You can sign up for a bit.ly account at: http://bit.ly/a/sign_up if you already have an account you can get your API key at: http://bit.ly/a/your_api_key.

First create a shell script called shorten.sh:

#!/bin/bash

curl -s "http://api.bitly.com/v3/shorten?login=YOURLOGIN&apiKey=YOURAPIKEY&longUrl=$1&format=txt" | pbcopy

This script uses curl in silent mode to connect to the bit.ly API and shorten the url specified by longUrl. Since we want to shorten a variety of URLs I've used $1, which refers to the first argument the script was invoked with. You'll also notice the format parameter, this can be set to xml, json or txt, we're using txt so we don't have to do any parsing. We are piping the output of our curl command into pbcopy a command line utility provided by OS X which provides copying to the clipboard. Pbcopy basically copies all output it receives into the clipboard.

Make sure to fill in YOURLOGIN with your bit.ly login and YOURAPIKEY with your bit.ly API key. Save the shell script somewhere it's not going to be deleted by accident or moved. If you use bash open your .bash_profile file in your home directory. You may have to create it if you haven't made any changes to it before. Add the following line:

alias shorten='/bin/bash /PATHTOSCRIPT/shorten.sh'

Where PATHTOSCRIPT is the path to where you have stored your script. This alias will allow us to invoke our script from anywhere using "shorten" instead of the full path to the script. In order for your .bash_profile to be reloaded and the alias to become active, you need to either type source .bash_profile or open a new terminal window.

Now when you type:

shorten YOURURL

Where YOURURL is any URL you wish to shorten, the shortened version will be copied directly into your machine's clipboard.

3 comments:

  1. effin awesome.


    this comment uncrowded by many not-quite-on-topic questions (motivated by personal projects, whose daydreams are often set bombastic soundtracks, & references aloud often end with "& then all will bow down before me,") to save its author (me) the embarrassing realization of just how silly they in fact might very well seem. that would hurt my, uh, score.

    the above metaphor nonetheless allows information to be disclosed on the suspicion that you know exactly what i mean.

    thanks! you made my day -- rereading man pages does not always work, perhaps especially when one starts out only knowing the nothing sandbox that corporate eulas drool to keep invisibly locked.... and besides, it was not so much the commands as the strategies --

    be seeing you...

    ReplyDelete
  2. Great stuff! Just added my own domain to bitly and this will make it way more simpler to shorten URLs.

    ReplyDelete
  3. Cool. I could extend it a little bit :) Instead of using $1, use pbpaste. It will paste the original url into the script. One step less :) Heres the code:

    url=$(pbpaste)
    curl -s "http://api.bitly.com/v3/shorten?login=YOURLOGIN&apiKey=YOURAPIKEY&longUrl=$url&format=txt" | pbcopy

    Additional, if you put this into an automator app, you can access es via spotlight or Alfred & Co.

    ReplyDelete