Python | URL shortener using tinyurl API
There are multiple APIs (e.g-bitly API, etc.) available for URL shortening service but in this article, we will be using Tinyurl API to shorten URLs.
Though tinyURL has not officially released its API but we are going to use it unofficially. Here, we can put as input any number of URLs at a time and get the shortened URLs at a time.
For Example, if in command line we write
$ python filename URL1 URL2 URL3
then output will be:
['shortened url1', 'shortened url2', 'shortened url3']
- First we have to import 7 libraries.
- We could have used just one library to work, but in order to make good url shortener we have to use 7 libraries.
The code snippet is as follows:
from __future__ import with_statement import contextlib try: from urllib.parse import urlencode except ImportError: from urllib import urlencode try: from urllib.request import urlopen except ImportError: from urllib2 import urlopen import sys |
Step 2:
Here encoding of url and appending it to API is done and then we open request_url using urlopen. Then we convert the response to UTF-8, since urlopen() returns a stream of bytes rather than a string.
The code snippet is as follows:
def make_tiny(url): with contextlib.closing(urlopen(request_url)) as response: return response.read().decode('utf-8 ') |
Step 3:
In the last step we are calling main() and we are getting user input by using sys.argv
We have not limited ourself to only one url as input, instead, we are saying that give us as many urls as you want, we will make them tiny. map() is a simple way of looping over a list and passing it to a function one by one.
The code snippet is as follows:
def main(): for tinyurl in map(make_tiny, sys.argv[1:]): print(tinyurl) if __name__ == '__main__': main() |
Finally, the complete code is below:
#!/usr/bin/env python 3 from __future__ import with_statement import contextlib try: from urllib.parse import urlencode except ImportError: from urllib import urlencode try: from urllib.request import urlopen except ImportError: from urllib2 import urlopen import sys def make_tiny(url): with contextlib.closing(urlopen(request_url)) as response: return response.read().decode('utf-8 ') def main(): for tinyurl in map(make_tiny, sys.argv[1:]): print(tinyurl) if __name__ == '__main__': main() |
Output:

Recommended Posts:
- Important differences between Python 2.x and Python 3.x with examples
- Python | Convert list to Python array
- Python | Index of Non-Zero elements in Python list
- Reading Python File-Like Objects from C | Python
- Python | Merge Python key values to list
- Python | Add Logging to Python Libraries
- Python | Add Logging to a Python Script
- Python | Sort Python Dictionaries by Key or Value
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Visualizing O(n) using Python
- JavaScript vs Python : Can Python Overtop JavaScript by 2020?
- pow() in Python
- Python Set | pop()
- max() and min() in Python
- SHA in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


