URL SHORTENER PYTHON PROJECT

GitHub: https://github.com/shivamm-verma/URL-SHORTENER-

 ReadME;💻💻💻

==================ABOUT US=====================================

hey yo,

this project is done by Shivam and Renuka, helped by Aseem Mishra(CS Teacher).

SCHOOL: Escorts World School,kanpur,uttar pradhesh,india.


SHIVAM VERMA

Class 12th-S1 student, opted PCM+CS


RENUKA KUMARI

Class 12th-S2 student, opted PCB+CS

================================================================


=================ABOUT PROGRAM==================================

Started Date: 14 February,2022

End(Completion) Date: 20 February,2022 


The AIM of this program is to Shorten lengthy/moderate links to shortened link.



SERVER NAME: https://www.dwarflink.com/

================================================================


=================REQUIREMENTS===================================

> python library, mysql.connector

> MySQL Connection with python. (established)



================================================================


=================CONTAINED PROGRAMS=============================

> main program.py

(This is main python program for this project.)

(This contains all the functions and code to execute.)

(this will also check that if record for that input link is existing or not,

if existing then that it'll return it's linked link from the database)

(Debugging of this program has been done!~)

(we might create .exe type of this program in future, if needed.)


> function-wise main.py

(This function would create a connection between MySQL, 

input a origional link and provide a shortlink.)

(This function also would upload distinct(not retundant/duplicate) to 

MySQL database named dwarflink(auto-creatable) and in table links(auto-creatable).)


> Shortlink Returner 

(function that returns random website links in the form https://www.dwarflink.com/XXXXXX)

(where XXXXXX is the unique code for a linked website with this link.)

(296,010 such links possible at this stage& can be increased if unique code is increased by a unit length "X" to 888,030, and 2,220,075, and so on too ♾️ or as system performance.)


> MySQL Query Firer

(while query not equals to n, this program will run endlessly and execute it's querries.)


> misc.

(this program is just for random doubt clearation regarding a concept in python.)


==================================================================


=================ALGORITHM========================================

(not discussed yet!)


==================================================================

 CODE;💻💻💻



def shortlink_ret():           # program to return a random short link
    import random
    list_alpha = ['a','b','c','d','e','f','g','h','i','j','k','l',\
        'm','n','o','p','q','r','s','t','u','v','w','x','y','z']
    lst_shortlink_code = []
    i = 1
    while i <= 6:                 # password unique-code length = 6
        lst_elem = random.choice(list_alpha)
        str_case = random.randint(0,1)
        if str_case == 1:
            lst_elem = lst_elem.upper()
        lst_shortlink_code.append(lst_elem)
        i += 1
    shortlink_code = "".join(lst_shortlink_code)
    shortlink = []
    shortlink.append("https://www.dwarflink.com/")       # server name(OUR)
    shortlink.append(shortlink_code)
    shortlink = "".join(shortlink)
    return shortlink

def record_link_dev():
    cursor.execute("select * from links")
    list_rec = []
    for i in cursor:
        i = list(i)
        list_rec.append(i)
    return list(list_rec)

import mysql.connector as mc
mydb = mc.connect(host = "localhost",user = "root",passwd = "<YOUR PASSWORD>")
print("_________________________________________________________________")
print(f"Your connection ID: {mydb}")
print()
orig_link = input("Enter Link: \n")
orig_link = orig_link.strip()
print()
cursor = mydb.cursor()

try:            # if database is not existing, it'd create a database named dwarflink
    cursor.execute("create database dwarflink")
    cursor.execute("commit")
except:
    cursor.execute("use dwarflink")
    cursor.execute("commit")

if orig_link.startswith("https://www.") or  orig_link.startswith("https://") or  orig_link.startswith("www."):
    # check if shortlink is already exsiting in database or not
    true_count  = 0
    for i in record_link_dev():
        global shortlink
        i = list(i)    
        if i[0] == orig_link:          
            true_count = 1
            # shortlink already exisiting
            print("shortlink already exisiting!~~~~")
            shortlink = i[1]
            print(f"Shortlink: {shortlink}")
            break
    if true_count == 0:
        shortlink = str(shortlink_ret())
        print(f"Shortlink: {shortlink}")
else:
    print("Enter a Valid Link.")


try:            # creates table named links if not existing else pass and finally, and adding data to it.
    cursor.execute("create table links (longlink varchar(500) UNIQUE NOT NULL, shortlink varchar(50))")
    cursor.execute("commit")
    #print("Table,links, created.")
except:
    #print("Table ,links, is already existing.")
    pass
finally:
    try:
        cursor.execute(f"insert into links values ('{orig_link}','{shortlink}')")
        cursor.execute("commit")
        print("~~data is added to database!~~")
        pass
    except Exception as e:
        print("__ __ __ __ __ __ __ __ __ __")
        print("~~data couldn't be added to database!~~")
        print(f"Reason: {e}")
        print("__ __ __ __ __ __ __ __ __ __")        
        pass

print()
print("_________________________________________________________________")
# program is done to input origional link and return shortlink on valid conditions(ref. line 48)

def record_links():       # for printing it to user
    print("______________database records________________")
    print()
    cursor.execute("select * from links")
    for i in cursor:
        print(i)
    print("______________________________________________")


if __name__ == "__main__":
    #record_links()
    #record_link_dev()
    pass


Comments

Popular posts from this blog

Who Is Shivam Verma?

Contact/Inquiry