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.
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.
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"
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.
Want to start making money as a white hat hacker? Jump-start your hacking career with our 2020 Premium Ethical Hacking Certification Training Bundle from the new Null Byte Shop and get over 60 hours of training from cybersecurity professionals.
Other worthwhile deals to check out: