forked from ashimdahal/jelly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
125 lines (112 loc) · 4.95 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
from flask import Flask, render_template, request, jsonify, make_response
from flask_sqlalchemy import SQLAlchemy
import json
import os
import datetime
app = Flask(__name__)
db = SQLAlchemy(app)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
USER_METRICS = False
if USER_METRICS:
app.config['SQLALCHEMY_DATABASE_URI']= "postgresql://lmplraoaopfjik:f5646a3cb32a8e8a64424eca55b0b3013206851a4e00b92f162d65bbac835a98@ec2-54-85-56-210.compute-1.amazonaws.com:5432/d8cg969jasbkcr"
class Users(db.Model):
sno = db.Column(db.Integer, nullable=True, primary_key=True)
newUsers = db.Column(db.Integer, nullable=True)
dailyUsers = db.Column(db.Integer, nullable=True)
weeklyUsers = db.Column(db.Integer, nullable=True)
monthlyUsers = db.Column(db.Integer, nullable=True)
@app.route('/', methods=['GET', 'POST'])
def api():
if 'cookieToken' in request.cookies:
if USER_METRICS:
# Fetching the cookie
cookie= request.cookies.get('cookieToken')
# fetching data from database
User = Users.query.filter_by(sno=1).first()
if 'dayCookie' in request.cookies:
day=User.dailyUsers+1
User.dailyUsers=day
week=User.weeklyUsers+1
User.weeklyUsers=week
month=User.monthlyUsers+1
User.monthlyUsers=month
db.session.commit()
elif 'weekCookie' in request.cookies:
day=User.dailyUsers+1
User.dailyUsers=day
week=User.weeklyUsers+1
User.weeklyUsers=week
month=User.monthlyUsers+1
User.monthlyUsers=month
db.session.commit()
elif 'monthCookie' in request.cookies:
day=User.dailyUsers+1
User.dailyUsers=day
week=User.weeklyUsers+1
User.weeklyUsers=week
month=User.monthlyUsers+1
User.monthlyUsers=month
db.session.commit()
else:
# setting the cookie for new monthly user
cookieToken = make_response(render_template('index.html'))
cookieToken.set_cookie('dayCookie', f'{cookie}' , expires=datetime.datetime.now() + datetime.timedelta(days=1))
cookieToken.set_cookie('weekCookie', f'{cookie}' , expires=datetime.datetime.now() + datetime.timedelta(days=7))
cookieToken.set_cookie('monthCookie', f'{cookie}' , expires=datetime.datetime.now() + datetime.timedelta(days=30))
new=User.newUsers+1
User.newUsers=new
day=User.dailyUsers+1
User.dailyUsers=day
week=User.weeklyUsers+1
User.weeklyUsers=week
month=User.monthlyUsers+1
User.monthlyUsers=month
db.session.commit()
return cookieToken
return render_template('index.html')
if request.method == 'POST':
# seting the cookie
token = request.form.get('token')
# main cookie
expire_date= datetime.datetime.now() + datetime.timedelta(days=365)
cookieToken = make_response(render_template('index.html'))
cookieToken.set_cookie('cookieToken', f'{token}', expires=expire_date)
if USER_METRICS:
User = Users.query.filter_by(sno=1).first()
cookieToken.set_cookie('dayCookie', f'{token}' , expires=datetime.datetime.now() + datetime.timedelta(days=1))
cookieToken.set_cookie('weekCookie', f'{token}' , expires=datetime.datetime.now() + datetime.timedelta(days=7))
cookieToken.set_cookie('monthCookie', f'{token}' , expires=datetime.datetime.now() + datetime.timedelta(days=30))
new=User.newUsers+1
User.newUsers=new
day=User.dailyUsers+1
User.dailyUsers=day
week=User.weeklyUsers+1
User.weeklyUsers=week
month=User.monthlyUsers+1
User.monthlyUsers=month
db.session.commit()
return cookieToken
return render_template('api.html')
# Gets user's input, runs it through the talk function in conversation.py and generates the output
@app.route('/getresponse',methods=['POST'])
def response():
from conversation import talk
token = request.cookies.get('cookieToken')
user_inp = request.get_json()
text = user_inp['text']
prev_conv = user_inp['previous_conversaion']
out = talk(text,prev_conv,token)
return jsonify(out)
@app.route('/removetoken')
def remove():
resp = make_response(render_template('api.html'))
resp.set_cookie('cookieToken', expires=0)
resp.set_cookie('dayCookie', expires=0)
resp.set_cookie('weekCookie', expires=0)
resp.set_cookie('monthCookie', expires=0)
return resp
@app.route('/copyright')
def copyright():
return render_template('copyright.html')
if __name__ == '__main__':
app.run(debug=True)