URL shortner in PythonFirst of all, let us understand what is a URL shortener. Users have relied on connections as their primary form of transit since the internet's inception. URL shortening is a Web method for making a Uniform Resource Locator (URL) significantly shorter while still directing to the desired website. This is accomplished by the use of a redirect, which points to a web page with a lengthy URL. They let users navigate from one website to another in a matter of seconds by just clicking on a URL. Unfortunately, connections typically become lengthy and complex when tracking parameters are added and site structures expand to handle hundreds of pages. Long URLs became a concern as social media grew in popularity. Twitter used to limit messages to a maximum of 140 characters, and every character in a link was tallied. As a result, a long URL may take up the entirety of your tweet. URL shortener programs have evolved as a response to this difficulty of sharing. The benefits of URL shorteners:
When you connect your own custom domain to a URL shortener that functions as a basis for all the short links you make, it's called a custom or branded URL shortener. You may choose your own domain instead of selecting a generic one like bit.ly or rebrand.ly. Operations a Link Shortner performs:
Now let us have a look at the code to create a URL shortener in python, we will be needing an API that will actually perform the operation of reducing the URL, so for that, we will be using two different API providers and will be writing two different python code, both of them doing the same task of URL shortening just using different APIs to perform the task. The two API providers we are going to use are Bitly URL Shortener and Cuttly URL Shortener. Let us have a look at the code of the Cuttly URL Shortener. Another amazing URL shortener we may utilize is Cuttly URL Shortener. It is likewise rather straightforward to use, albeit it does necessitate 2-3 additional lines of code. However, it does not require installation, making it overall easier. First and foremost, go to Cuttly and create a new account. After that, go to Edit Profile and Generate New API Keys. We'd get a new set of API keys as a result of this. Take a note of the API keys. As a result, we can get right into programming without having to install anything. Though we only require one easy installation, I believe most of us already have it. Still, if you don't have the required library the command to install is: Now since our required library is in place, let us start writing code for it. Code:Output: Now let us have a look at the output of the written code, all the output generated by the code at the time of execution is shown below with all the standard outputs along with errors and exceptions if any. please choose any one of the operations from the listed below the list of operations:: 1. To enter the API key for the service account. 2. To enter the URL which you want to shorten. 3. To perform the operation of shortening the URL. 4. To print the URL which is shortened. 5. To exit from the code execution. 1 enter the API key of the service account:: UIWN56645G#YW To keep on going with code execution, type [y] otherwise [n]. y please choose any one of the operations from the listed below the list of operations:: 1. To enter the API key for the service account. 2. To enter the URL which you want to shorten. 3. To perform the operation of shortening the URL. 4. To print the URL which is shortened. 5. To exit from the code execution. 2 Enter the URL that you want to shorten. https://www.mywebsite.com/dashboard/stats/barchart To keep on going with code execution, type [y] otherwise [n]. y please choose any one of the operations from the listed below the list of operations:: 1. To enter the API key for the service account. 2. To enter the URL which you want to shorten. 3. To perform the operation of shortening the URL. 4. To print the URL which is shortened. 5. To exit from the code execution. 3 URL shortened successfully. To keep on going with code execution, type [y] otherwise [n]. y please choose any one of the operations from the listed below the list of operations:: 1. To enter the API key for the service account. 2. To enter the URL which you want to shorten. 3. To perform the operation of shortening the URL. 4. To print the URL which is shortened. 5. To exit from the code execution. 4 Shortened URL:https://cut.ly/i4tdxaE To keep on going with code execution, type [y] otherwise [n]. Y please choose any one of the operations from the listed below the list of operations:: 1. To enter the API key for the service account. 2. To enter the URL which you want to shorten. 3. To perform the operation of shortening the URL. 4. To print the URL which is shortened. 5. To exit from the code execution. 2 Enter the URL that you want to shorten. https://website. To keep on going with code execution, type [y] otherwise [n]. y please choose any one of the operations from the listed below the list of operations:: 1. To enter the API key for the service account. 2. To enter the URL which you want to shorten. 3. To perform the operation of shortening the URL. 4. To print the URL which is shortened. 5. To exit from the code execution. 3 URL shortened successfully. To keep on going with code execution, type [y] otherwise [n]. y please choose any one of the operations from the listed below the list of operations:: 1. To enter the API key for the service account. 2. To enter the URL which you want to shorten. 3. To perform the operation of shortening the URL. 4. To print the URL which is shortened. 5. To exit from the code execution. 4 URL shortening got an exception None. please choose any one of the operations from the listed below the list of operations:: 1. To enter the API key for the service account. 2. To enter the URL which you want to shorten. 3. To perform the operation of shortening the URL. 4. To print the URL which is shortened. 5. To exit from the code execution. 5 In the above-written code the object of the above-written class is created and that object is used to call all the above-written functions inside that class, the user is given multiple options like to enter the API key of the service account, to enter the URL which the user wants to shorten, to perform the actual shortening operation of the specified input URL, once the sorting operation is performed successfully the user can print the URL which is shortened, and in the last option user can accept the code execution by opting the last and final option, appropriate input is taken from the user and appropriate output is given according to the option which is selected by the user to perform the operation. Let us have a look at the code of the Bitly URL Shortener. The Bitly URL Shortener is really easy to use. All you have to do is create a Bitly account. Then go to Group Settings and select Advanced Settings from the drop-down menu. The API option may be found there. Because API is no longer supported, choose OAuth instead. After that, create the OAuth Token. Make a copy of the token. Bitly is a URL shortening service and a link management platform that was founded in 2008 by Bitly, Inc. It is situated in New York City and is privately held. Bitly shortens 600 million URLs every month[4] for social media, text messaging, and email. Bitly earns money by charging for access to aggregate data generated by a large number of users utilizing abbreviated URLs. Code:Output: Now let us have a look at the output of the written code, all the output generated by the code at the time of execution is shown below with all the standard outputs along with errors and exceptions if any. please choose any one of the operations from the listed below the list of operations:: 1. To enter the API token for the service account. 2. To enter the URL which you want to shorten. 3. To perform the operation of shortening the URL. 4. To print the URL which is shortened. 5. To enter the URL which you want to expand. 6. To perform the expansion operation of the URL. 7. To print the URL which is expanded. 8. To exit from the code execution. 1 enter the api token for the bitly service account:: CJX5E9OLE$H6 To keep on going with code execution, type [y] otherwise [n]. y please choose any one of the operations from the listed below the list of operations:: 1. To enter the API token for the service account. 2. To enter the URL which you want to shorten. 3. To perform the operation of shortening the URL. 4. To print the URL which is shortened. 5. To enter the URL which you want to expand. 6. To perform the expansion operation of the URL. 7. To print the URL which is expanded. 8. To exit from the code execution. 2 Enter the URL that you want to shorten. https://www.mywebsite.com/dashboard/stats/barchart To keep on going with code execution, type [y] otherwise [n]. y please choose any one of the operations from the listed below the list of operations:: 1. To enter the API token for the service account. 2. To enter the URL which you want to shorten. 3. To perform the operation of shortening the URL. 4. To print the URL which is shortened. 5. To enter the URL which you want to expand. 6. To perform the expansion operation of the URL. 7. To print the URL which is expanded. 8. To exit from the code execution. 3 URL shortened successfully. To keep on going with code execution, type [y] otherwise [n]. y please choose any one of the operations from the listed below the list of operations:: 1. To enter the API token for the service account. 2. To enter the URL which you want to shorten. 3. To perform the operation of shortening the URL. 4. To print the URL which is shortened. 5. To enter the URL which you want to expand. 6. To perform the expansion operation of the URL. 7. To print the URL which is expanded. 8. To exit from the code execution. 4 Shortened URL:https://cut.ly/i4tdxaE To keep on going with code execution, type [y] otherwise [n]. y please choose any one of the operations from the listed below the list of operations:: 1. To enter the API token for the service account. 2. To enter the URL which you want to shorten. 3. To perform the operation of shortening the URL. 4. To print the URL which is shortened. 5. To enter the URL which you want to expand. 6. To perform the expansion operation of the URL. 7. To print the URL which is expanded. 8. To exit from the code execution. 5 Enter the URL that you want to expand. https://cut.ly/i4tdxaE To keep on going with code execution, type [y] otherwise [n]. y please choose any one of the operations from the listed below the list of operations:: 1. To enter the API token for the service account. 2. To enter the URL which you want to shorten. 3. To perform the operation of shortening the URL. 4. To print the URL which is shortened. 5. To enter the URL which you want to expand. 6. To perform the expansion operation of the URL. 7. To print the URL which is expanded. 8. To exit from the code execution. 6 URL expanded successfully. To keep on going with code execution, type [y] otherwise [n]. y please choose any one of the operations from the listed below the list of operations:: 1. To enter the API token for the service account. 2. To enter the URL which you want to shorten. 3. To perform the operation of shortening the URL. 4. To print the URL which is shortened. 5. To enter the URL which you want to expand. 6. To perform the expansion operation of the URL. 7. To print the URL which is expanded. 8. To exit from the code execution. 7 Expanded URL:https://www.mywebsite.com/dashboard/stats/barchart. To keep on going with code execution, type [y] otherwise [n]. y please choose any one of the operations from the listed below the list of operations:: 1. To enter the API token for the service account. 2. To enter the URL which you want to shorten. 3. To perform the operation of shortening the URL. 4. To print the URL which is shortened. 5. To enter the URL which you want to expand. 6. To perform the expansion operation of the URL. 7. To print the URL which is expanded. 8. To exit from the code execution. 8 In the above-written code the object of the above-written class is created and that object is used to call all the above-written functions inside that class, the user is given multiple options like to enter the API key of the service account, to enter the URL which the user wants to shorten, to perform the actual shortening operation of the specified input URL, once the sorting operation is performed successfully the user can print the URL which is shortened, and in the last option user can accept the code execution by opting the last and final option, appropriate input is taken from the user and appropriate output is given according to the option which is selected by the user to perform the operation. Advantages:
So, in this article, we understood how to implement a URL shortener in python. Next TopicAutomate Instagram Messages using Python |