I want to be able to take a shortened or non-shortened URL and return its un-shortened form. How can I make a python program to do this?
Additional Clarification:
- Case 1: shortened --> unshortened
- Case 2: unshortened --> unshortened
e.g. bit.ly/silly
in the input array should be google.com
in the output array
e.g. google.com
in the input array should be google.com
in the output array
http://github.com/stef/urlclean
Send an HTTP HEAD request to the URL and look at the response code. If the code is 30x, look at the
Location
header to get the unshortened URL. Otherwise, if the code is 20x, then the URL is not redirected; you probably also want to handle error codes (4xx and 5xx) in some fashion. For example:Unshorten.me has an api that lets you send a JSON or XML request and get the full URL returned.
Here a src code that takes into account almost of the useful corner cases:
The src code is on github @ https://github.com/amirkrifa/UnShortenUrl
comments are welcome ...
To unshort, you can use requests. This is a simple solution that works for me.
Using requests: