Tutorialspoint.dev

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:

    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)))

    Output:

    Code to Expand URL:



    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)))

    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:

      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)))

      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:

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

    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:

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

    Output:

References:

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


This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter