diff --git a/projects/A UrlShortener/README.md b/projects/A UrlShortener/README.md new file mode 100644 index 000000000..90e13f805 --- /dev/null +++ b/projects/A UrlShortener/README.md @@ -0,0 +1,52 @@ +# Development +Please have python3 installed to run this project on terminal: +[Python3 Installation](https://www.python.org/downloads/) + +# Project Title + +A URL Shortener is a tool that creates a short, unique URL that will redirect to the specific website of your choosing + +## Prerequisites + +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 + +Steps: + +Run the app + + flask run + +## Screenshots of the project + +![Home Page](ShortenerHome.png) +![Result Page](ShortenerResult.png) + +## Author Name + +[Japneet Rajput](https://github.com/JapneetRajput) + +[Portfolio Website](https://japneetrajput.github.io) \ No newline at end of file diff --git a/projects/A UrlShortener/ShortenerHome.png b/projects/A UrlShortener/ShortenerHome.png new file mode 100644 index 000000000..66c29ab1b Binary files /dev/null and b/projects/A UrlShortener/ShortenerHome.png differ diff --git a/projects/A UrlShortener/ShortenerResult.png b/projects/A UrlShortener/ShortenerResult.png new file mode 100644 index 000000000..756ed151a Binary files /dev/null and b/projects/A UrlShortener/ShortenerResult.png differ diff --git a/projects/A UrlShortener/app.py b/projects/A UrlShortener/app.py new file mode 100644 index 000000000..4d96fb934 --- /dev/null +++ b/projects/A UrlShortener/app.py @@ -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 register" + 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) + + \ No newline at end of file diff --git a/projects/A UrlShortener/initialize.sql b/projects/A UrlShortener/initialize.sql new file mode 100644 index 000000000..2c11a1287 --- /dev/null +++ b/projects/A UrlShortener/initialize.sql @@ -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`)); \ No newline at end of file diff --git a/projects/A UrlShortener/requirements.txt b/projects/A UrlShortener/requirements.txt new file mode 100644 index 000000000..48cbd723c --- /dev/null +++ b/projects/A UrlShortener/requirements.txt @@ -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 diff --git a/projects/A UrlShortener/static/style.css b/projects/A UrlShortener/static/style.css new file mode 100644 index 000000000..325f304fe --- /dev/null +++ b/projects/A UrlShortener/static/style.css @@ -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; +} diff --git a/projects/A UrlShortener/static/tabIcon.png b/projects/A UrlShortener/static/tabIcon.png new file mode 100644 index 000000000..c16227cf1 Binary files /dev/null and b/projects/A UrlShortener/static/tabIcon.png differ diff --git a/projects/A UrlShortener/templates/history.html b/projects/A UrlShortener/templates/history.html new file mode 100644 index 000000000..66d1a2424 --- /dev/null +++ b/projects/A UrlShortener/templates/history.html @@ -0,0 +1,86 @@ + + + + + + + + + + +
+
+
+
+
    +
    + {% for i in range(len1) %} +
  1. + {% for j in range(0,4) %} + {% if r[i][2]==username %} + {% if j!=2 %} + {% if j==0 %} +

    Old Link: {{r[i][j]}}


    + {% endif %} + {% if j==1 %} +

    Shortened Link: {{r[i][j]}}


    + {% endif %} + {% if j==3 %} +

    Link Nickname: {{r[i][j]}}


    + {% endif %} + {% endif %} + {% endif %} + {% endfor %} +
    +
  2. + {% endfor %} +
    +
+ + \ No newline at end of file diff --git a/projects/A UrlShortener/templates/home.html b/projects/A UrlShortener/templates/home.html new file mode 100644 index 000000000..9a4e7b3bc --- /dev/null +++ b/projects/A UrlShortener/templates/home.html @@ -0,0 +1,12 @@ + + + + + + + + + +

Registered Successfully!

+ + \ No newline at end of file diff --git a/projects/A UrlShortener/templates/login.html b/projects/A UrlShortener/templates/login.html new file mode 100644 index 000000000..e126f323a --- /dev/null +++ b/projects/A UrlShortener/templates/login.html @@ -0,0 +1,48 @@ + + + + + + + + + +
+

Register Page

+
+ + +
+
+
+ + +
+
+ +
+ + +
+
+ +
+ + +
+
+ +
+ + +
+
+ + +
+ +

Already registered? Login now

+ +
+ + \ No newline at end of file diff --git a/projects/A UrlShortener/templates/profile.html b/projects/A UrlShortener/templates/profile.html new file mode 100644 index 000000000..b85d87a4f --- /dev/null +++ b/projects/A UrlShortener/templates/profile.html @@ -0,0 +1,67 @@ + + + + + + + + + + +
+
+
+
+
    +
    + +
    +
+ + \ No newline at end of file diff --git a/projects/A UrlShortener/templates/url.html b/projects/A UrlShortener/templates/url.html new file mode 100644 index 000000000..a4e85684e --- /dev/null +++ b/projects/A UrlShortener/templates/url.html @@ -0,0 +1,72 @@ + + + + + + + + + + +
+
+
+
+
+

Url Shortener

+
+ + +
+
+ + +
+ + +
+ + \ No newline at end of file diff --git a/projects/A UrlShortener/templates/urlResult.html b/projects/A UrlShortener/templates/urlResult.html new file mode 100644 index 000000000..e75a90b56 --- /dev/null +++ b/projects/A UrlShortener/templates/urlResult.html @@ -0,0 +1,66 @@ + + + + + + + + + + +
+
+
+
+
+ {% for key,value in result.items() %} + {% if key!="linkCode" %} +

{{key}}: {{value}}

+ {% endif %} + {% endfor %} +
+ + \ No newline at end of file diff --git a/projects/A UrlShortener/templates/urlShortener.html b/projects/A UrlShortener/templates/urlShortener.html new file mode 100644 index 000000000..31177e685 --- /dev/null +++ b/projects/A UrlShortener/templates/urlShortener.html @@ -0,0 +1,27 @@ + + + + + + + + + +
+

Login Page

+
+ + +
+
+
+ + +
+
+ +
+

Not registered? Register now

+
+ + \ No newline at end of file