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'] 
Step #1:

  • 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

def make_tiny(url):
    request_url = ('http://tinyurl.com/api-create.php?' + urlencode({'url':url}))   
    with contextlib.closing(urlopen(request_url)) as response:                      
        return response.read().decode('utf-8 ')       

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

def main():                                                                
    for tinyurl in map(make_tiny, sys.argv[1:]):                    
        print(tinyurl)
  
if __name__ == '__main__':
    main()
  
  

chevron_right


Finally, the complete code is below:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/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):
    request_url = ('http://tinyurl.com/api-create.php?' + urlencode({'url':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()

chevron_right


Output:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.