How To: Shorten URLs from the Command Line with Python

Shorten URLs from the Command Line with Python

How to Shorten URLs from the Command Line with Python

Shortening URLs has become a necessity in this age of Twitter and limited-character status updates. It not only allows us to cram more words into our oh-so-important Twitter statuses, but it also helps us with a few other things.

Some Benefits of URL Shorteners

We can create temporary URLs. The URLs created by a URL shortener are deleted after a certain period of inactivity. This is good for a link that we just want to show to a group of friends quickly.

Some URL shortening services allow analysis of link traffic (Google and Bitly come to mind). This can be good for website owners, or people who make money off of webhits. It could help them analyze where their traffic is coming from for a particular link, rather than analyze a website as a whole.

Posting longer status updates. People like me who use Twitter to promote their blog, or users who post links to news require every precious letter possible. Links tend to take up quite a few characters, so having URL shortener can be a godsend. It allows us to get our point across without spiraling downward into incoherent e-gibberish.

I often want to shorten my URLs from the command line. Opening another browser tab can take up too many resources for my liking. It also can load rather slow, and the JavaScript on the page that loads the shortened URL will often load improperly and not display the new URL. A URL shortener from the command line could also potentially allow us to convert big lists of links, as well as single ones, in a quick and simple manner. This Null Byte will help us do just that! Let's hack together some code in the Python programming language to shorten our URLs from the command line. This will go great in conjunction with our newly made Twitter command-line application.

Requirements

Python installed on your system.

Picking Our API

First off, we need to pick which API that we are going to code our Python application to communicate with. An API (Application Programming Interface) is basically the protocol for communicating with a web application's interface. This means that there is a specific standard and method for us to connect to the interface with that we must comply with.

After a bit of research, the easiest API would be the Google URL shortener. I picked it because it does not require registration, an account, or any public API keys. This will keep everything simple so you can easily understand it.

Code the Application

Here is what I came up with for a finished, cleanly coded and simplistic result. I named it url.py.

How to Shorten URLs from the Command Line with Python

Grab the source code here.

To use the program, we simply type the following command into a terminal while in the same directory as the saved url.py source code.
    python url.py "http://paste.url/between/quotes.kthx"

Code Breakdown

In the main function, gurl = is simply "curling" the data from the API URL. This is essentially all that we need.

Assign all of the appropriate variables needed.

The data returned from curling the Goo.gl API is printed out to the screen.

If you think coding looks cool, maybe you should try to join the Community Byte activites that we host weekly here at Null Byte.

Become one with Null Byte! Twitter, Google+ and IRC chat!

Image via solobizcoach

Be the First to Comment

Share Your Thoughts

  • Hot
  • Latest