-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
125 lines (53 loc) · 2.2 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
from Rewriter import Rewriter
from pymongo import MongoClient
import json
import os
import random
import string
from flask import Flask,render_template,url_for,request,flash,jsonify
templates = os.path.join(os.getcwd(),"templates/")
app = Flask(__name__, template_folder=templates)
app.config['SECRET_KEY'] = "secrestsArePrivate"
client = MongoClient(os.environ.get("MONGO_URL"))
db = client["rewiter"]
collection = db["api"]
@app.route("/",methods=['POST','GET'])
def index():
text = request.form.get("text")
print(text)
if text != None:
rewriter = Rewriter(text)
rewritten = rewriter.main(2)[0]
return render_template("index.html",retext=rewritten,originaltxt=text)
return render_template("index.html")
@app.route("/v1/api")
def api():
text = request.args.get("text", default = "*", type = str)
apikey = request.args.get("apikey", default = "NOAPI", type = str)
if text != "*" and apikey != "NOAPI":
rewriter = Rewriter(text)
rewritten = rewriter.main()[0]
return jsonify(status="Successful", text=rewritten, author="Vaibhav Chandra")
else:
return jsonify(status="Unsuccessful", text=None, author="Vaibhav Chandra")
@app.route("/api_key", methods=["GET", "POST"])
def api_key():
name = request.form.get("name")
email = request.form.get("email")
apikey = ''.join(random.choices(string.ascii_uppercase + string.digits + string.ascii_lowercase, k=10))
if request.method in ["POST", "GET"]:
if "name" in request.form and "email" in request.form:
doc = {"name": name, "email": email, "apikey" : apikey}
if collection.find_one({"email": email}) == None:
flash("API Key Generated Successfully!")
return render_template("api.html",apikey=apikey)
else:
flask("Email Already Registered")
return render_template("api.html")
return render_template("api.html")
@app.errorhandler(404)
def not_found(e):
return render_template("404.html")
if __name__ == "__main__":
# debug = true will enable the debugger
app.run(debug=True)