-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
80 lines (58 loc) · 2.22 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
from flask import Flask, render_template, request, redirect, Response
from pymongo import MongoClient
app = Flask(__name__)
def get_database(DB):
# Provide the mongodb atlas url to connect python to mongodb using pymongo
# CONNECTION_STRING = "mongodb://localhost:27017"
CONNECTION_STRING = "mongodb+srv://admin:[email protected]/learning?retryWrites=true&w=majority"
client = MongoClient(CONNECTION_STRING, connect=False)
return client[DB]
db_name = get_database('short_url')
collection = db_name["all_urls_data"]
def is_keyword_present(keyword):
present=0
for item in collection.find():
if(item['short_url']==keyword):
present=1
return present
def url_shortener(a):
return "hLOO"
@app.route('/', methods=['POST','GET'])
def begin():
status=2
if request.method == 'POST':
url_received = request.form["entered_url"]
keyword_received = request.form["entered_keyword"]
# shortened_url = url_shortener(url_received)
keyword_status = is_keyword_present(keyword_received)
if(keyword_status==1):
status = 0
else:
new_item = {
"short_url" : keyword_received,
"long_url": url_received,
"clicks":0
}
collection.insert_one(new_item)
status = 1
return render_template("form.html", status=status,link="https://harshithurl.up.railway.app/" + str(keyword_received))
else:
return render_template("form.html", status=status)
@app.route('/all_urls')
def all_urls():
return render_template("all_urls.html")
@app.route('/<received_url>')
def reroute(received_url):
link_status=0
for item in collection.find():
if(item['short_url']==received_url):
redirection = item['long_url']
# filter = { 'short_url': received_url }
# newvalues = { "$set": { 'clicks': item['clicks']+1 } }
# collection.update_one(filter, newvalues)
link_status=1
if(link_status==0):
return "Link Not Found" #Link not found, wanna create one with this name? click here
return redirect(redirection, code=302)
if __name__ == '__main__':
app.run(debug=True)