URL Shorteners and its API in Python | Set-2

Prerequisite: URL Shorteners and its API | Set-1

Let’s discuss few more URL shorteners using the same pyshorteners module.

  1. Bitly URL Shortener: We have seen Bitly API implementation. Now let’s see how this module can be used to shorten a URL using Bitly Services.

    Code to Shorten URL:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    from pyshorteners import Shortener
      
    ACCESS_TOKEN = '82e8156..e4e12c6'
      
    url_shortener = Shortener('Bitly', bitly_token = ACCESS_TOKEN)
      
    print ("Short URL is {}".format(url_shortener.short(url)))

    chevron_right

    
    

    Output:

    Code to Expand URL:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    from pyshorteners import Shortener
    ACCESS_TOKEN = '82e8156...c1dce4e12c6'
      
    url_expander = Shortener('Bitly', bitly_token = ACCESS_TOKEN)
      
    print ("Long URL is {}".format(url_expander.expand(url)))

    chevron_right

    
    

    Output:

  2. Adf.ly URL Shortener:This is the URL Shortening Service that pays on the basis of number of visitors that visit your shortened URL. You need to Signup and create an API Key in order to use this service.
    1. You will get a link to Signup on the top rightmost corner of the Adf.ly webpage. There is an option to create either type of account: Create account to shorten URL and earn money or to Create account and Pay to advertise your website on Adf.ly Webpage.
    2. Once You will signup and confirm your email address, you will be redirected to the login page.
    3. After successful login, dashboard of your account will open up and you see an option Tools.
    4. Under Tools page, you will get your client id and API Key.
    5. Note: There will be a message, Your API Access is currently disabled. Click Here to enable it.

      Code to Shorten the URL:

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      from pyshorteners import Shortener
        
      # uid means User Id and key means API Key.
        
      url_shortener = Shortener('Adfly', uid ='20727891', key ='b8de8e0...5a2381241c', type ='int')
        
      print ("Short URL is {}".format(url_shortener.short(url)))

      chevron_right

      
      

      Output:

  3. Osdb URL Shortener: This is another simple URL shortening service. You don’t need an API Key to use this. Just type the url and see the magic. This site does not provide features like tracking the visitors on the shortened URL.

    Code to Shorten the URL:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    from pyshorteners import Shortener
      
      
    url_shortener = Shortener('Osdb')
      
    print ("SHORT URL is {}".format(url_shortener.short(url)))

    chevron_right

    
    

    Output:

  4. da.gd URL Shortener: This URL shortening service provides you the option to customise the shorten URL just like Bitly.

    Code to Shorten URL:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    from pyshorteners import Shortener
      
      
    url_shortener = Shortener('Dagd')
      
    print ("Short URL is {}".format(url_shortener.short(url)))

    chevron_right

    
    

    Output:

References:

  • https://pypi.org/project/pyshorteners/
  • https://adf.ly/


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.