forked from AvgBlank/URLShortener
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
441 lines (389 loc) · 15.6 KB
/
app.py
File metadata and controls
441 lines (389 loc) · 15.6 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
# Starting of the program
#! --------------------------------------------------
#! ---------- Credits
#! --------------------------------------------------
# region credits
# * ---- Made by:
# * ------- Aaloke Eppalapalli
# * ------- Husain Khorakiwala
# * ---- Source Code:
# * ------- https://github.com/AvgBlank/URLShortener
# endregion
#! --------------------------------------------------
#! ---------- Imports
#! --------------------------------------------------
# region import
# ? Flask --> For the backend of HTML
from flask import (
Flask,
render_template,
make_response,
request,
redirect,
url_for,
)
# ? MongoDB --> For storing URL databases
from pymongo import MongoClient, UpdateOne
# ? DateTime --> For getting current date
from datetime import datetime, timedelta
# ? HashIDS --> For hashing URLs
from hashids import Hashids
# ? DotENV --> For storing keys
from dotenv import load_dotenv
# ? OS --> For getting .env file
from os import environ
# ? Random, String --> Last resort fail safe.
from random import choice as randchoice
from string import ascii_letters, digits, punctuation
# ? Pytz --> For handling timezone
from pytz import timezone
# ? Authlib --> For Google Authentication
from authlib.integrations.flask_client import OAuth
# endregion
#! --------------------------------------------------
#! --------------------------------------------------
#! --------------------------------------------------
#! ---------- Running the Program
#! --------------------------------------------------
# region Running the Program
# ? Required.
app = Flask(__name__)
app.config["SECRET_KEY"] = environ.get("SECRET_KEY")
if environ.get("DOMAIN"):
domain = environ.get("DOMAIN")
else:
domain = "https://trim.lol/"
hasUsedApp = False
# ? Google OAuth Client ID, Secret, and Redirect URI
google_client_id = environ.get("google_client_id")
google_client_secret = environ.get("google_client_secret")
# ? Setting OAuth App
oauth = OAuth(app)
google = oauth.register(
name="google",
client_id=google_client_id,
client_secret=google_client_secret,
access_token_url="https://accounts.google.com/o/oauth2/token",
access_token_params=None,
authorize_url="https://accounts.google.com/o/oauth2/auth",
authorize_params=None,
api_base_url="https://www.googleapis.com/oauth2/v1/",
server_metadata_url="https://accounts.google.com/.well-known/openid-configuration",
client_kwargs={"scope": "email profile"},
)
# ? Connecting to the Mongo DB Database
load_dotenv()
mongoLink = environ.get("link")
client = MongoClient(mongoLink)
db = client["URLShorteners"]
# ? Connecting to Collections
URLsColl = db["urls"]
usersColl = db["users"]
# ? Create indexes (if needed)
URLsColl.create_index("ID")
URLsColl.create_index("Timestamp")
URLsColl.create_index("OriginalURL")
URLsColl.create_index("ShortenedURL", unique=True)
URLsColl.create_index("Clicks")
URLsColl.create_index("UserID")
usersColl.create_index("ID", unique=True)
usersColl.create_index("UserID", unique=True)
# endregion
#! --------------------------------------------------
#! --------------------------------------------------
#! --------------------------------------------------
#! ---------- Functions
#! --------------------------------------------------
# region Functions
@app.route("/signup", methods=("GET", "POST"))
def signUp():
global userID
if usersColl.find_one({"UserID": request.cookies.get("userID")}) == None:
if request.method == "POST":
load_dotenv()
id = usersColl.count_documents({}) + 1
users = usersColl.distinct("UserID")
while True:
key = ""
for _ in range(10):
key += randchoice(ascii_letters + digits)
hashids = Hashids(
min_length=10,
salt=key,
alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890",
)
userID = hashids.encode(id)
if userID in users:
continue
break
usersColl.insert_one({"ID": id, "UserID": userID})
output = render_template(
"signup.html", userID=userID, buttonVisible="buttoninvis"
)
resp = make_response(output)
expiration_date = datetime.now() + timedelta(days=30)
expiration_timestamp = expiration_date.timestamp()
resp.set_cookie("userID", userID, expires=expiration_timestamp)
return resp
else:
return render_template("signup.html", userID="Your User ID")
else:
return redirect("/generateurl")
@app.route("/login", methods=["GET", "POST"])
def logIn():
global userID
if usersColl.find_one({"UserID": request.cookies.get("userID")}) == None:
if request.method == "POST":
userID = request.form["userid"]
userPresent = usersColl.find_one({"UserID": userID})
if userPresent:
output = redirect("/generateurl")
resp = make_response(output)
expiration_date = datetime.now() + timedelta(days=30)
expiration_timestamp = expiration_date.timestamp()
resp.set_cookie("userID", userID, expires=expiration_timestamp)
return resp
else:
return render_template("login.html", ErrorValid="errorTrue")
else:
return render_template("login.html")
else:
return redirect("/generateurl")
# login for google
@app.route("/login/google")
def login_google():
google = oauth.create_client("google")
redirect_uri = url_for("authorize_google", _external=True)
return google.authorize_redirect(redirect_uri)
# authorize for google
@app.route("/authorize/google")
def authorize_google():
google = oauth.create_client("google")
google.authorize_access_token()
resp = google.get("userinfo")
user_info = resp.json()
userID = user_info.get("email")
# Check if user already exists, if not, insert into usersColl
if not usersColl.find_one({"UserID": userID}):
new_user_id = usersColl.count_documents({}) + 1
usersColl.insert_one({"ID": new_user_id, "UserID": userID})
# Save user ID in cookies and redirect to /generateurl
response = make_response(redirect("/generateurl"))
expiration_date = datetime.now() + timedelta(days=30)
response.set_cookie("userID", userID, expires=expiration_date.timestamp())
return response
@app.route("/", methods=["GET", "POST"])
def home():
if usersColl.find_one({"UserID": request.cookies.get("userID")}) != None:
return redirect("/stats")
else:
response = make_response(render_template("index.html"))
if request.cookies.get("userID") != None:
response.set_cookie("userID", "", expires=0)
return response
@app.route("/generateurl", methods=["GET", "POST"])
def generateurl():
global hasUsedApp
if usersColl.find_one({"UserID": request.cookies.get("userID")}) != None:
if request.method == "POST" and hasUsedApp == False:
customURL = ""
url = request.form["long_url"]
customURL = request.form["custom_url"]
if " " in url:
if customURL == "":
return render_template(
"generateurl.html",
new_url="Returns your new shortened URL",
error_url="errorTrue",
error_url_statement="Invalid URL. Please try again.",
)
else:
return render_template(
"generateurl.html",
new_url="Returns your new shortened URL",
error_url="errorTrue",
custom_url=customURL,
error_url_statement="Invalid URL. Please try again.",
)
elif "trim.lol" in url or "bit.ly" in url or "tinyurl.com" in url:
if customURL == "":
return render_template(
"generateurl.html",
new_url="Returns your new shortened URL",
error_url="errorTrue",
error_url_statement="URL cannot be shortened. Please try again.",
)
else:
return render_template(
"generateurl.html",
new_url="Returns your new shortened URL",
error_url="errorTrue",
custom_url=customURL,
error_url_statement="URL cannot be shortened. Please try again.",
)
now = datetime.now(timezone("Asia/Kolkata"))
id = URLsColl.count_documents({}) + 1
time = now.strftime("%d/%m/%Y %H:%M:%S")
userID = request.cookies.get("userID")
if url != "" and "http" not in url:
url = "http://" + url
if url == "":
return render_template(
"generateurl.html",
new_url="Returns your new shortened URL",
error_url="errorTrue",
error_url_statement="Please enter a URL and try again.",
)
elif customURL != "":
existingURLs = []
for document in URLsColl.find({}, {"ShortenedURL": 1}):
existingURLs.append(document["ShortenedURL"])
if customURL in existingURLs:
return render_template(
"generateurl.html",
new_url="Returns your new shortened URL",
old_url=url,
error_custom_url="errorTrue",
error_custom_url_statement="Custom URL already claimed. Please try again.",
)
elif (
" " in customURL
or any(char in punctuation for char in customURL)
or "www" in customURL
or "http" in customURL
):
return render_template(
"generateurl.html",
new_url="Returns your new shortened URL",
old_url=url,
error_custom_url="errorTrue",
error_custom_url_statement="Invalid custom URL. Please try again.",
)
else:
URLsColl.insert_one(
{
"ID": id,
"Timestamp": time,
"OriginalURL": url,
"ShortenedURL": customURL,
"Clicks": 0,
"UserID": userID,
}
)
hasUsedApp = True
return render_template(
"generateurl.html",
old_url=url,
new_url=domain + customURL,
custom_url=customURL,
completed="True",
)
else:
hashid = Hashids(min_length=5, salt=userID)
newURL = hashid.encode(id)
existingURLs = []
for document in URLsColl.find({}, {"ShortenedURL": 1}):
existingURLs.append(document["ShortenedURL"])
while newURL in existingURLs:
salt = ""
for _ in range(10):
salt += randchoice(ascii_letters + digits)
hashid = Hashids(
min_length=7,
salt=salt,
)
newURL = hashid.encode(id)
URLsColl.insert_one(
{
"ID": id,
"Timestamp": time,
"OriginalURL": url,
"ShortenedURL": newURL,
"Clicks": 0,
"UserID": userID,
}
)
hasUsedApp = True
return render_template(
"generateurl.html",
old_url=url,
new_url=domain + newURL,
custom_url=" ",
completed="True",
)
elif request.method == "POST" and hasUsedApp == True:
hasUsedApp = False
return redirect(url_for("generateurl"))
else:
hasUsedApp = False
return render_template(
"generateurl.html",
clearForms="True",
new_url="Returns your new shortened URL",
)
else:
response = make_response(redirect("/"))
if request.cookies.get("userID") != None:
response.set_cookie("userID", "", expires=0)
return response
@app.route("/stats", methods=["GET", "POST"])
def stats():
userID = request.cookies.get("userID")
if userID and usersColl.find_one({"UserID": userID}) != None:
urls = list(URLsColl.find({"UserID": userID}, {"_id": 0}))
return render_template("stats.html", urls=urls, userID=userID, domain=domain)
else:
response = make_response(redirect("/"))
response.set_cookie("userID", "", expires=0)
return response
@app.route("/delete", methods=["POST"])
def delete():
userID = request.cookies.get("userID")
if usersColl.find_one({"UserID": userID}) is not None:
data = request.get_json()
shortened_url = data.get("shortened_url")
if not shortened_url:
return "Shortened URL not found.", 400 # Bad Request if no URL provided
URLsColl.delete_one({"ShortenedURL": shortened_url, "UserID": userID})
# Fetch all documents excluding "_id"
urls = list(URLsColl.find({}, {"_id": 0}))
length = len(urls)
# Bulk update with new IDs
update_operations = [
UpdateOne(
{
"ShortenedURL": urls[i - 1]["ShortenedURL"],
"UserID": urls[i - 1]["UserID"],
},
{"$set": {"ID": i}},
)
for i in range(length, 0, -1)
]
if update_operations:
URLsColl.bulk_write(update_operations)
return redirect(url_for("stats"))
else:
response = make_response(redirect("/"))
response.set_cookie("userID", "", expires=0)
return response
@app.route("/<id>")
@app.route("/<id>/")
def url_redirection(id):
info = URLsColl.find_one({"ShortenedURL": id})
if info:
url = info["OriginalURL"]
URLsColl.update_one({"ShortenedURL": id}, {"$inc": {"Clicks": 1}})
return redirect(url)
else:
return redirect("/404")
@app.route("/logout")
def logout():
response = make_response(redirect("/"))
response.set_cookie("userID", "", expires=0, path="/", max_age=0)
return response
@app.route("/404")
def error_404():
return render_template("404.html")
# endregion
if __name__ == "__main__":
app.run(debug=True)