Skip to content
This repository has been archived by the owner on May 25, 2022. It is now read-only.

Added A Url Shortener #604

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions projects/A UrlShortener/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Development
Please have python3 installed to run this project on terminal:
[Python3 Installation](https://www.python.org/downloads/)

# Project Title
<!--Remove the below lines and add yours -->
A URL Shortener is a tool that creates a short, unique URL that will redirect to the specific website of your choosing

## Prerequisites
<!--Remove the below lines and add yours -->
Modules used in this app are Flask and PyShortener.

Steps:

Start MySQL and Apache server on Xampp.

Initialize Database schema by going to MySQL Server admin page by passing the queries in initialize.sql

Install the Virtual Environment module

pip install virtualenv

Create a virtual environment

virtualenv venv

Activate the Virtual Environment

venv\scripts\activate.bat

Install the required modules

pip install -r requirements.txt

## How to run the project
<!--Remove the below lines and add yours -->
Steps:

Run the app

flask run

## Screenshots of the project
<!--Remove the below lines and add yours -->
![Home Page](ShortenerHome.png)
![Result Page](ShortenerResult.png)

## Author Name

[Japneet Rajput](https://github.com/JapneetRajput)

[Portfolio Website](https://japneetrajput.github.io)
Binary file added projects/A UrlShortener/ShortenerHome.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added projects/A UrlShortener/ShortenerResult.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
123 changes: 123 additions & 0 deletions projects/A UrlShortener/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
from flask import Flask, render_template,request,session,redirect,url_for,g
from pyshorteners import *
# from flaskext.mysqldb import MySQL
import mysql.connector
# from flask.ext.mysqldb import MySQL

shortener = Shortener()

app = Flask(__name__)
# app.config['MYSQL_HOST'] = 'japneet-python-db.mysql.database.azure.com'
# app.config['MYSQL_USER'] = '1FcvJ1A4lW'
# app.config['MYSQL_PASSWORD'] = 'Commando@007'
# app.config['MYSQL_DB'] = '1FcvJ1A4lW'
# mysql = MySQL(app)

app.secret_key="japneet"

@app.route('/')
def index():
return render_template('urlShortener.html')

@app.route('/beforeRegister')
def beforeLogin():
return render_template('login.html')

@app.route('/login',methods=['POST','GET'])
def login():
mydb=mysql.connector.connect(
host="localhost",
user="root",
password="",
database="username"
)
mycursor = mydb.cursor()
if request.method=='POST':
signup=request.form
name = signup['name']
username = signup['username']
email = signup['email']
passw = signup['pass']
rpassw = signup['repass']
mycursor.execute("insert into users values(%s,%s,%s,%s,%s)",(name,username,email,passw,rpassw))
mydb.commit()
mycursor.close()
return render_template('urlShortener.html')


@app.route('/urlShortener',methods=['POST','GET'])
def urlShortener():
mydb=mysql.connector.connect(
host="localhost",
user="root",
password="",
database="username"
)
mycursor = mydb.cursor()
if request.method=='POST':
signnup=request.form
username = signnup['Username']
session['username']=username
passw = signnup['Password']
# mycursor.execute("TRUNCATE TABLE link")
mycursor.execute("select * from users where Username='"+username+"' and Password='"+passw+"'")
r=mycursor.fetchall()
count=mycursor.rowcount
if count ==1:
return render_template('url.html')
elif count>1:
return "More than one user"
else:
return "You are not a member please <a href='/register.html'>register</a>"
else:
return render_template('url.html')
mydb.commit()
mycursor.close()

@app.route('/history')
def history():
mydb=mysql.connector.connect(
host="localhost",
user="root",
password="",
database="username"
)
mycursor = mydb.cursor()
mycursor.execute("select * from link")
r=mycursor.fetchall()
username = session['username']
len1 = len(r)
return render_template('history.html',r=r,len1=len1,username=username)

@app.route('/url',methods=['POST','GET'])
def url():
mydb=mysql.connector.connect(
host="localhost",
user="root",
password="",
database="username"
)
mycursor = mydb.cursor()
if request.method=='POST':
result=request.form.to_dict()
urll = result['Url']
x = shortener.tinyurl.short(urll)
result['shortLink']=x
shortLink = x
longLink=urll
username = session['username']
linkCode = result['linkCode']
mycursor.execute("insert into link values(%s,%s,%s,%s)",(longLink,shortLink,username,linkCode))
mydb.commit()
mycursor.close()
return render_template('urlResult.html',result=result)

@app.route('/logout')
def logout():
return "Logged out successfully"

if __name__ == "__main__":
from waitress import serve
serve(app, host="127.0.0.1", port=5000)


9 changes: 9 additions & 0 deletions projects/A UrlShortener/initialize.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- Insert your database name in place of DATABASE_NAME and table name in place of TABLE_NAME

CREATE DATABASE DATABASE_NAME;

USE DATABASE_NAME;

CREATE TABLE `DATABASE_NAME`.`TABLE_NAME` ( `Name` VARCHAR(100) NOT NULL , `Username` VARCHAR(80) NOT NULL , `Email` VARCHAR(150) NOT NULL , `Password` VARCHAR(100) NOT NULL , `ConfirmPassword` VARCHAR(100) NOT NULL , PRIMARY KEY (`Name`, `Username`), UNIQUE (`Username`));

CREATE TABLE `DATABASE_NAME`.`TABLE_NAME` (`longLink` VARCHAR(300) NOT NULL , `shortLink` VARCHAR(100) NOT NULL , `username` VARCHAR(100) NOT NULL , `linkCode` VARCHAR(100) NOT NULL , PRIMARY KEY (`longLink`), UNIQUE (`linkCode`));
20 changes: 20 additions & 0 deletions projects/A UrlShortener/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
certifi==2021.10.8
charset-normalizer==2.0.12
click==8.1.3
colorama==0.4.4
Flask==2.1.2
Flask-MySQL==1.5.2
gunicorn==20.1.0
idna==3.3
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.1
mysql==0.0.3
mysql-connector-python-rf==2.2.2
mysqlclient==2.1.0
psycopg2==2.9.3
PyMySQL==1.0.2
pyshorteners==1.0.1
requests==2.27.1
urllib3==1.26.9
Werkzeug==2.1.2
56 changes: 56 additions & 0 deletions projects/A UrlShortener/static/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
@import "https://use.fontawesome.com/releases/v5.5.0/css/all.css";
body{
margin: 0;
padding: 0;
font-family: sans-serif;
background-color: black;
background-size: cover;
}
.login-box{
width: 100vh;
position: absolute;
top: 50%;
left: 55%;
transform: translate(-50%,-50%);
color: white;
}
.login-box h1{
float: left;
font-size: 40px;
border-bottom: 6px solid #FFFFFF;
margin-bottom: 50px;
padding: 10px 0;
}
.textbox{
width: 100%;
overflow: hidden;
font-size: 20px;
padding: 8px 0;
margin: 8px 0;
border-bottom: 1px solid #FFFFFF;
}
.textbox i{
width: 26px;
float: left;
text-align: center;
}
.textbox input{
border: none;
outline: none;
background: none;
color: white;
font-size: 18px;
width: 80%;
float: left;
margin: 0 10px;
}
.btn{
width: 100%;
background: none;
border: 2px solid #FFFFFF;
color: white;
padding: 5px;
font-size: 18px;
cursor: pointer;
margin: 12px 0;
}
Binary file added projects/A UrlShortener/static/tabIcon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
86 changes: 86 additions & 0 deletions projects/A UrlShortener/templates/history.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<!DOCTYPE html>
<html lang="en" dir="ltr">
<link rel='icon' href='{{url_for('static',filename='tabIcon.png')}}'/>
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" href="{{url_for('static', filename='style.css')}}">
<style>
body{
margin: 0px;
padding: 0px;
background-color: black;
color: white;
}
.mid{
display: block;
width: 60%;
margin: 2% auto;
}
.navbar{
display: block;
}
.navbar li{
display: inline-block;
font-size: 20px;
padding: 5%;
}
.navbar li a{
color: #FFF;
text-decoration: none;
}
.navbar li a:hover{
color: #777;
border-bottom: 2px solid #777;
}
.mid .navbar .active {
border-bottom: 2px solid #777;
color: #777;
}
.mid .navbar .active:hover {
border-bottom: 2px solid #777;
color: #555;
}
h1{
display:inline;
}
</style>
</head>
<body>
<header class="header">
<div class="mid">
<ul class="navbar">
<ul class="navbar">
<li><a href="/urlShortener">Shortener Tool</a></li>
<li><a href="/history" class="active">History</a></li>
<li><a href="#">Profile</a></li>
<li><a href="#">Settings</a></li>
</ul>
</div>
</header>
<ol>
<div>
{% for i in range(len1) %}
<li>
{% for j in range(0,4) %}
{% if r[i][2]==username %}
{% if j!=2 %}
{% if j==0 %}
<h1>Old Link: <a href="{{r[i][j]}}">{{r[i][j]}}</a></h1><br>
{% endif %}
{% if j==1 %}
<h1>Shortened Link: <a href="{{r[i][j]}}">{{r[i][j]}}</a></h1><br>
{% endif %}
{% if j==3 %}
<h1>Link Nickname: {{r[i][j]}}</h1><br>
{% endif %}
{% endif %}
{% endif %}
{% endfor %}
<br>
</li>
{% endfor %}
</div>
</ol>
</body>
</html>
12 changes: 12 additions & 0 deletions projects/A UrlShortener/templates/home.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en" dir="ltr">
<link rel='icon' href='{{url_for('static',filename='tabIcon.png')}}'/>
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" href="{{url_for('static', filename='style.css')}}">
</head>
<body class="login-box">
<h1>Registered Successfully!</h1>
</body>
</html>
Loading