-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
151 lines (130 loc) · 4.69 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
from flask import Flask, render_template, request, url_for
from flask_sqlalchemy import SQLAlchemy
from werkzeug.utils import redirect
import string, random
import os
from qrCode import detectFromImage, validateURL, detectLiveFeed, createQRcode
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///urls.db"
db = SQLAlchemy(app)
UPLOAD_FOLDER = "./static"
ALLOWED_EXTENSIONS = {"png", "jpg", "jpeg"}
app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
class Urls(db.Model):
id_ = db.Column("id_", db.Integer, primary_key=True)
long = db.Column("long", db.String())
short = db.Column("short", db.String(3))
def __init__(self, long, short):
self.long = long
self.short = short
@app.before_first_request
def create_table():
db.create_all()
def shorten_url():
letters = string.ascii_lowercase + string.ascii_uppercase
while True:
rand_letters = random.choices(letters, k=3)
rand_letters = "".join(rand_letters)
short_url = Urls.query.filter_by(short=rand_letters).first()
if not short_url:
return rand_letters
@app.route("/", methods=["GET", "POST"])
def home():
if request.method == "POST":
url_received = request.form["nm"]
# check if url exists
found_url = Urls.query.filter_by(long=url_received).first()
if found_url:
# return found_url.short
return redirect(url_for("display_short_url", url=found_url.short))
else:
short_url = shorten_url()
new_url = Urls(long=url_received, short=short_url)
db.session.add(new_url)
db.session.commit()
# return short_url
return redirect(url_for("display_short_url", url=short_url))
else:
return render_template("home.html")
@app.route("/display/<url>")
def display_short_url(url):
return render_template("shorturl.html", short_url_display=url)
@app.route("/<short_url>")
def redirection(short_url):
long_url = Urls.query.filter_by(short=short_url).first()
if long_url:
return redirect(long_url.long)
else:
return "Url Doesn't Exist"
@app.route("/qrcode", methods=["GET", "POST"])
def qrPage():
context = None
if request.method == "POST":
# print(request.form["cam"])
if request.files:
# print("File Found")
f = request.files["file"]
path = os.path.join(app.config["UPLOAD_FOLDER"], f.filename)
f.save(path)
saved_filename = path
result = detectFromImage(path=saved_filename)
if not result:
context = "No QR Code Found"
else:
context = {
"url": None,
"data": None,
"cam": False,
"file": True,
"create": False,
}
if validateURL(result):
context["url"] = result
else:
context["data"] = result
elif request.form["data"]:
if request.form["data"] != "none":
# print("Creating QR")
# print(request.form["data"])
received_data = request.form["data"]
if len(received_data) < 1:
context = None
else:
createQRcode(received_data)
context = {
"url": None,
"data": None,
"cam": False,
"file": False,
"create": True,
}
elif request.form["cam"] == "cam":
context = {
"url": None,
"data": None,
"cam": True,
"file": False,
"create": False,
}
# print("Opening cam")
# print("cam_val", request.form["cam"])
res = detectLiveFeed()
for i in res:
if validateURL(i):
if context["url"] == None:
context["url"] = [i]
else:
context["url"].append(i)
else:
if context["data"] == None:
context["data"] = [i]
else:
context["data"].append(i)
else:
print("In Else")
return render_template("qrCode.html", context=context)
else:
return render_template("qrCode.html", context=context)
if __name__ == "__main__":
# app.run()
app.run(debug=True)