-
Notifications
You must be signed in to change notification settings - Fork 1
/
flask-server
311 lines (258 loc) · 8.99 KB
/
flask-server
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
from flask import Flask, flash, redirect, render_template, request, session, abort , url_for,jsonify,Response,session,request,g
from flask_socketio import SocketIO
import time,glob,random,sqlite3,os,requests
from Messanger import main
from googletrans import Translator
app = Flask(__name__)
app.secret_key=os.urandom(24)
socketio = SocketIO(app)
t=Translator()
@app.route('/',methods=['POST','GET'])
def login():
if request.method=='GET':
if g.user:
return redirect(url_for('oindex'))
else:
return render_template('login.html')
if request.method=='POST':
session.pop('user',None)
if request.form['pass']==check(request.form['user']):
session['user']=request.form['user']
return redirect(url_for('oindex'))
return render_template('login.html')
@app.route("/reg",methods=['GET','POST'])
def reg():
if(request.method=='GET'):
return render_template('register.html')
if(request.method=='POST'):
u=request.form['user']
p=request.form['pass']
n=request.form['num']
s=request.form['state']
l=request.form['lang']
cr=request.form.getlist('crop')
#print(c[0])
print("INSERT INTO profile (user,num,state,lang,pass) VALUES('"+u+"','"+n+"','"+s+"','"+l+"','"+p+"')")
conn = sqlite3.connect('data.db')
c=conn.cursor()
h=(c.execute("SELECT user FROM profile WHERE user = ?", (u,))).fetchall()
if len(h)!=0 or len(n)!=10:
return redirect(url_for('reg',a="false"))
else:
c.execute("INSERT INTO profile (User,Num,State,Lang,Pass) VALUES('"+u+"','"+n+"','"+s+"','"+l+"','"+p+"')")
for o in cr:
c.execute("INSERT INTO crop (User,Crop) VALUES('"+u+"','"+o+"')")
conn.commit()
c.close()
return redirect(url_for('login'))
@app.route("/oindex")
def oindex():
if g.user:
conn = sqlite3.connect('data.db')
c=conn.cursor()
p=(c.execute("SELECT State FROM profile WHERE user = ?", (g.user,))).fetchall()
print(str(p[0])[2:-3])
h=(c.execute("SELECT * FROM predicted_price WHERE State = ?", (str(p[0])[2:-3],))).fetchall()
print(h)
return render_template('index.html',user=g.user,cr=h)
return redirect(url_for('login'))
@app.before_request
def before():
g.user=None
if 'user' in session:
g.user=session['user']
@app.route("/update")
def update():
r=random.randint(1,1000)
return jsonify(r=r)
@app.route("/profile",methods=['POST','GET'])
def prof():
if g.user:
if request.method=='GET':
conn = sqlite3.connect('data.db')
c=conn.cursor()
lr=c.execute("SELECT * FROM login where user=?",(g.user,))
return render_template('updateprofile.html',item=lr.fetchall(),user=g.user)
#if request.method=='POST':
return redirect(url_for('login'))
@app.route("/pest")
def pest():
return render_template('pestattack.html')
@app.route("/loanRate")
def loan():
if g.user:
conn = sqlite3.connect('data.db')
c=conn.cursor()
lr=c.execute("SELECT * FROM loan")
print(g.user)
#c.close()
return render_template('banks.html',item=lr.fetchall(),user=g.user)
return redirect(url_for('login'))
@app.route("/logout")
def out():
session.pop('user',None)
return redirect(url_for('login'))
@app.route("/ecomm")
def ecomm():
conn = sqlite3.connect('e_com.db')
c=conn.cursor()
lr=c.execute("SELECT * FROM LISTINGS")
#c.close()
#return render_template('loan.html',item=lr.fetchall())
return render_template('e_com.html',data=lr.fetchall(),user=g.user)
@app.route("/order")
def order():
if g.user:
return render_template("orders.html")
else:
return redirect(url_for('login'))
@app.route("/add/<id>")
def add(id):
op=random.uniform(0,99999.0)
data=id.split(" ")
conn = sqlite3.connect('e_com.db')
c=conn.cursor()
c.execute("CREATE TABLE IF NOT EXISTS LISTINGS(Region TEXT,Quantity TEXT,Price TEXT,Id TEXT)")
c.execute("INSERT INTO LISTINGS VALUES('{}','{}','{}','{}')".format(data[0],data[1],data[2],str(op)))
print(c.execute("SELECT * FROM LISTINGS").fetchall())
conn.commit()
conn.close()
return jsonify(res="success")
@app.route("/del/<id>")
def delete(id):
print(id)
conn = sqlite3.connect('e_com.db')
c=conn.cursor()
id=str(id)
c.execute("DELETE FROM LISTINGS where Id='{}'".format(id))
print(c.execute("SELECT * FROM LISTINGS").fetchall())
conn.commit()
conn.close()
#return render_template('loan.html',item=lr.fetchall())
#return render_template('e_com.html',data=lr.fetchall())
r="ok"
return jsonify(res=r)
@app.route("/upd/<data>",methods=['POST'])
def upd(data):
print(data)
return jsonify(data=1)
@app.route("/updu/<data>",methods=['POST'])
def updu(data):
print(data)
return jsonify(data=1)
def check(k):
conn = sqlite3.connect('data.db')
c=conn.cursor()
lr=c.execute("SELECT Pass FROM profile where User=?",(k,))
h=(lr.fetchall())
print(h)
if len(h)==1:
lr=c.execute("SELECT Lang FROM profile where User=?",(k,))
session['lan']=(lr.fetchall())[0][0]
print(session['lan'])
return h[0][0]
#print(len(h))
#if(lr.fetchall()[0][0]):
#return h
return redirect(url_for('login'))
@app.route('/getd/<data>')
def getd(data):
page=data
print(page)
print("HELLO")
api_key = "a9170a3ab84748cc681f9fe929b19808"
name=str(page)
x=name.split('*')
conn = sqlite3.connect('data.db')
c=conn.cursor()
lr=c.execute("SELECT State FROM profile where User=?",(x[0],))
h=(lr.fetchall())
base_url = "http://api.openweathermap.org/data/2.5/weather?"
aa=name[2:]
choi=name[0:1]
l=[]
result=""
if "*" in page:
print(str(h[0])[2:-3]+'*'+x[1])
main(str(h[0])[2:-3]+'*'+x[1])
exit()
elif(choi=="1"):
city_name =aa
complete_url = base_url + "appid=" + api_key + "&q=" + city_name
response = requests.get(complete_url)
x = response.json()
if x["cod"] != "404":
y = x["main"]
current_temperature = y["temp"]
current_pressure = y["pressure"]
current_humidiy = y["humidity"]
z = x["weather"]
weather_description = z[0]["description"]
l=[str(current_temperature-273),str(current_pressure),str(current_humidiy), str(weather_description)]
result="\nTemperature= "+str(current_temperature-273)+" Current Pressure= "+str(current_pressure)+" Humidity= "+str(current_humidiy)+" Weather description= "+str(weather_description)
else:
l=[" City Not Found "]
result="City Not Found"
elif(choi=='2'):
pestname=aa
list=[["Algicides","Algae"],["algaecides","Algae"],["Avicides","Birds"],
["Bactericides","Bacteria"],["Fungicides","Fungi"],["Herbicides","Plant"],
["Insecticides","Insects"],["Miticides","Mites"],["Molluscicides","Snails"],
["Nematicides","Nematodes"],["Rodenticides","Rodents"],["Slimicides","Algae/Bacteria/Fungi/Slime molds"],
["Virucides","Viruses"]]
for i in range(len(list)):
if(pestname.casefold() in list[i][1].casefold()):
l=["Chitty suggests you to use ",list[i][0]," as the pesticite."]
result="Chitty suggests you to use "+list[i][0]+" as the pesticide."
break
else:
l=["Pest not found"]
result="Pest not found"
elif(choi=='3'):
fert=aa
li=[["cotton","N: 89.55kg/ha, P2O5: 22.6kg/ha, K2O: 4.8kg/ha"],
["groundnut","N: 24.4kg/ha, P2O5: 39.3kg/ha, K2O: 12.9kg/ha"],
["Jute","N: 38kg/ha, P2O5: 11.5kg/ha, K2O: 5kg/ha"],
["Maize","N: 41.7kg/ha, P2O5: 14.7kg/ha, K2O: 3.8kg/ha"],
["Paddy","N: 81.7kg/ha, P2O5: 24.3kg/ha, K2O: 13.1kg/ha"],
["Wheat","N: 99.6kg/ha, P2O5: 30.2kg/ha, K2O: 6.9kg/ha"],
["Sugarcane","N: 124.8kg/ha, P2O5: 44kg/ha, K2O: 38.3kg/ha"],
["Pearl millet","N: 21.9kg/ha, P2O5: 5.5kg/ha, K2O: 0.8kg/ha"]]
for i in range(len(li)):
if(fert.casefold() in li[i][0].casefold()):
l=["Chitty suggests you to use ",li[i][1]," as the fertlizer."]
result="Chitty suggests you to use "+li[i][1]+" as the fertlizer."
break
else:
l=["Crop not found"]
result="Crop not found"
else:
l=["Invalid input"]
print(l)
print(result)
return jsonify(r=result)
@app.route("/chat")
def chat():
return render_template('session.html',user=g.user)
def messageReceived(methods=['GET', 'POST']):
print('message was received!!!')
@socketio.on('my event')
def handle_my_custom_event(json, methods=['GET', 'POST']):
print(session['lan'])
if session['lan']=="Hindi":
la="hi"
if session['lan']=="Bengali":
la="bn"
if session['lan']=="Marathi":
la="mr"
if session['lan']=="Tamil":
la="ta"
if session['lan']=="Punjabi":
la="pa"
if session['lan']=="English":
la="en"
json['message']=t.translate(json.get('message'),dest=la).text
print('received my event: ' + str(json.get('message')))
socketio.emit('my response', json, callback=messageReceived)
if __name__ == "__main__":
app.run(debug = True)