Url shortener project using Python

urlshorter using Python- www.devpyjp.com

Hello guys! Welcome to another great tutorial on Python. I did This URL shortener project using python was inspired by bit.ly. I hope you definitely enjoy this project.

What you need

  • Basics of python
  • knowledge on Dictionary and functions
  • knowledge on Classes and objects.

If you don’t know about classes and objects, here is my great tutorial on classes and objects in python.

A Beginner guide to Python classes

I am approching this project in 2 ways. one is very simple and i used function and dictionaries, by using functions we are limited. In second way i am using Classes in python, It helps a lot us. you definitely enjoyed it.

Ways to build URL shortener using python

  1. Using functions
  2. Using classes

ok, i think you are ready to walk with me. lets open your code editors and start coding along with me.

Method – 1:

import random 
import string


def create_shortURL(link):
    
    chars = string.ascii_lowercase
    # chars = ['a','c','z']
    short_url = ''.join(random.choice(chars) for i in range(5))
    d ={}
    URL = 'https://www.jp.ly/'+short_url
    
    d[URL] = link
    
   # This is optional

    if URL in d:
        print('Your URL:',d[URL])
        print('Shorten URL:',URL)
    else:
        print('No Short URL')
        

create_shortURL('https://www.youtube.com/c/jpnan')

here i used 2 simple and most used modules in python called strings and random.

I used dictionary to store short url and original url. we stored the short url as keys and original url as valeus in python dictionary.

https://www.jp.ly/: This is our domain URL. This is an example URL. we never purchase this, but if you want to implement this project and deploy on the server, then you need to purchase this your custom URL.

short_url: here we are generating 5 random lowercase characters and we append them to our domain URL, so that becomes our shortner URL.

Eg: https://www.jp.ly/

Method-2:

Classes help us to more customizations for this URL shortener using python project. I added 2 other features like check the short URL exists and creating customized or update URL.

Here is my full code. I created a class called UrlShort(). I took the same domain which i mentioned in the above code.

class UrlShort():
    domain = 'https://www.jp.py/'
    
    def __init__(self):
        
        self.data = {}
    
    def create_shortURL(self,url):
        
        
        chars = string.ascii_lowercase
        s_url = ''.join(random.choice(chars) for i in range(5))
        short_url = self.domain+s_url
            
        if not short_url  in self.data:
            self.data[short_url] = url
        else:
            for self.i in range(5):
                try:
                    s_url = ''.join(random.choice(chars) for i in range(5))
                    short_url = self.domain+s_url        
                
                    if not short_url  in self.data:
                       self.data[short_url] = url
                       break
                except:
                    pass
                    
            
        print('------------ Your short URL was created!...............')
        
        print('Your URL:',url)
        print('Shorten URL:',short_url)
        
        return short_url
        
        #print(self.data)
    def check_URL(self,url):
        if url in self.data:
            print('\n................. Here is yours...................')
            print('Your original url:',self.data[url.strip()])
        else:
            print('No url found..!')
    
    def update_url(self,url):
        if url in self.data:
            self.data.pop(url.strip())
            
            main_url = input('Enter Your original URL:')
            short_url = input('enter www.jp.py/')
            short_url = self.domain+short_url
            
            if short_url not in self.data and not main_url=='':
                self.data.update(short_url=main_url)
                print('\n.............Successfully updated..!..................')
                print('customized url:',short_url)
            else:
                print('Alredy taken..!')
        else:
            print('No data found...!')
            
            
s = UrlShort()
url = s.create_shortURL('http://devpyjp.com/clean-your-desktop-using-python-python-project/') 
s.data  

s.check_URL(url)

s.update_url(url)  

I took an empty dictionary to store the short URL and the original URL. I created 3 methods for creating short URLs, checking short URLs, and finally customized short urls and update the URLs.

create_shortURL(self,url): It allows us to create short URLs for our original URL. Here we store the original URL and generate a short URL in a python dictionary.

Here we restricted the random URLs if they already existed in our dictionary by implementing the for loop and try block.

check_URL(self,url): It allows to check our short urls. It will give you the stored original url if its exisites in our dictionary.

update_url(self,url): It allows us to customize the short URL or update URL. Here are overriding the randomly generated short URL.

I hope you enjoy this project. If you have any doubts please comment bellow. Subscribe to our newsletter to get more interesting projects using python notifications. Thank you…!

Leave a Reply