-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
356 lines (266 loc) · 11.3 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
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
from flask import Flask, request, jsonify
import mysql.connector
import math
import datetime
from contextlib import contextmanager
HOST = "db4free.net"
USER = "hobrien17"
PWORD = "dbpword1"
DB_NAME = "ei8htideas"
EMPTY = ""
P = math.pi/180
DIST_CALC = "12742 * ASIN(SQRT(0.5 - COS((latitude - {}) * {})/2 " \
"+ COS({} * {}) * COS(latitude * {}) * " \
"(1 - COS((longitude - {}) * {}))/2))"
app = Flask(__name__)
@contextmanager
def open_db():
cnx = mysql.connector.connect(user=USER, password=PWORD, host=HOST, database=DB_NAME)
cursor = cnx.cursor()
yield cursor
cnx.commit()
cnx.close()
def get_age(dob):
if dob is None:
return -1
today = datetime.date.today()
return (today - dob) // datetime.timedelta(days=365.2425)
@app.route('/addacq')
def add_acq():
my_id = request.args.get('id', default=-1, type=int)
their_id = request.args.get('user', default=-1, type=int)
with open_db() as cursor:
query = f"INSERT INTO acquaintances (user_from, user_to, confirmed) " \
f"VALUES ({my_id}, {their_id}, 0)"
cursor.execute(query)
return EMPTY
@app.route('/confirmacq')
def confirm_acq():
my_id = request.args.get('id', default=-1, type=int)
their_id = request.args.get('user', default=-1, type=int)
with open_db() as cursor:
query = f"UPDATE acquaintances " \
f"SET confirmed = 1 " \
f"WHERE user_from = {their_id} AND user_to = {my_id}"
cursor.execute(query)
return EMPTY
@app.route('/delreq')
def del_req():
my_id = request.args.get('id', default=-1, type=int)
their_id = request.args.get('user', default=-1, type=int)
with open_db() as cursor:
query = f"DELETE FROM acquaintances " \
f"WHERE user_from = {their_id} AND user_to = {my_id} "
cursor.execute(query)
return EMPTY
@app.route('/checkrequests')
def check_requests():
my_id = request.args.get('id', default=-1, type=int)
my_lat = request.args.get('lat', default=-1, type=float)
my_long = request.args.get('long', default=-1, type=float)
with open_db() as cursor:
query = f"SELECT id, name, latitude, longitude, title, dob, gender, description FROM users " \
f"WHERE id IN (" \
f"SELECT user_from FROM acquaintances " \
f"WHERE user_to = {my_id} AND confirmed = 0 " \
f")"
return jsonify(execute(cursor, query, my_lat, my_long))
@app.route('/writelatlong')
def write_lat_long():
user = request.args.get('id', default=-1, type=int)
lat = request.args.get('lat', default=-1, type=float)
long = request.args.get('long', default=-1, type=float)
with open_db() as cursor:
query = f"UPDATE users " \
f"SET latitude = {lat}, longitude = {long} " \
f"WHERE id = {user}"
print(query)
cursor.execute(query)
print(cursor)
return EMPTY
def calculate_distance(lat1, lat2, long1, long2):
return 12742 * math.asin(math.sqrt(0.5 - math.cos((lat2 - lat1) * P)/2 + math.cos(lat1 * P) *
math.cos(lat2 * P) * (1 - math.cos((long2 - long1) * P))/2))
def execute(cursor, query, my_lat, my_long):
cursor.execute(query)
result = []
for id, name, latitude, longitude, title, dob, gender, description in cursor:
print(id, name, latitude, longitude, title, dob, gender, description)
latitude = float(latitude)
longitude = float(longitude)
d = {
'id': id,
'name': name,
'latitude': latitude,
'longitude': longitude,
'distance': calculate_distance(my_lat, latitude, my_long, longitude),
'title': title,
'age': get_age(dob),
'gender': gender,
'description': description
}
result.append(d)
return result
def gen_order(order, my_lat, my_long):
order_by = ""
if order == "distance":
print("dist")
order_by = f"ORDER BY ({DIST_CALC.format(my_lat, P, my_lat, P, P, my_long, P)})"
print(order_by)
elif order == "name":
print("name")
order_by = f"ORDER BY name"
return order_by
@app.route('/newaccount')
def new_account():
username = request.args.get('username', default="", type=str)
pword = request.args.get('pword', default="", type=str)
name = request.args.get('name', default="", type=str)
lat = request.args.get('lat', default=0, type=float)
long = request.args.get('long', default=0, type=float)
dob = request.args.get('dob', default='2000-01-01', type=str)
title = request.args.get('title', default="", type=str)
desc = request.args.get('desc', default="", type=str)
with open_db() as cursor:
src_query = f"SELECT username FROM users " \
f"WHERE username = \"{username}\""
cursor.execute(src_query)
for username, in cursor:
print(username)
return jsonify([])
query = f"INSERT INTO users (username, password, name, latitude, longitude, dob, title, description) " \
f"VALUES (\"{username}\", \"{pword}\", \"{name}\", {lat}, {long}, {dob}, \"{title}\", \"{desc}\")"
cursor.execute(query)
id_query = f"SELECT id FROM users " \
f"WHERE username = \"{username}\""
cursor.execute(id_query)
id = None
for i, in cursor:
id = i
d = {
'id': id,
'username': username,
'pword': pword,
'name': name,
'age': get_age(datetime.datetime.strptime(dob, '%Y-%m-%d')),
'title': title,
'description': desc
}
return jsonify([d])
@app.route('/verifylogin')
def verify_pword():
# username is encrypted using MD5, pword encrypted using SHA1
# TODO: use better encrpytion techniques
username = request.args.get('username', default="", type=str)
pword = request.args.get('pword', default="", type=str)
with open_db() as cursor:
query = f"SELECT id, name, latitude, longitude, title, dob, gender, description, username, password FROM users " \
f"WHERE username = \"{username}\""
cursor.execute(query)
for id, name, latitude, longitude, title, dob, gender, description, u, p in cursor:
latitude = float(latitude)
longitude = float(longitude)
if p == pword:
d = {
'id': id,
'name': name,
'latitude': latitude,
'longitude': longitude,
'distance': 0,
'age': get_age(dob),
'title': title,
'description': description,
'gender': gender
}
return jsonify([d])
return jsonify([])
@app.route('/searchallacqs')
def search_all_acqs():
my_lat = request.args.get('lat', default=-1, type=float)
my_long = request.args.get('long', default=-1, type=float)
my_id = request.args.get('id', default=-1, type=int)
order = request.args.get('order', default="", type=str)
with open_db() as cursor:
query = f"SELECT id, name, latitude, longitude, title, dob, gender, description FROM users " \
f"WHERE id <> {my_id} " \
f"AND id IN (" \
f"SELECT user_to FROM acquaintances " \
f"WHERE user_from = {my_id} AND confirmed = 1" \
f")"
query += gen_order(order, my_lat, my_long)
return jsonify(execute(cursor, query, my_lat, my_long))
@app.route('/searchacqs')
def search_acqs():
my_lat = request.args.get('lat', default=-1, type=float)
my_long = request.args.get('long', default=-1, type=float)
my_id = request.args.get('id', default=-1, type=int)
order = request.args.get('order', default="", type=str)
search = request.args.get('search', default="", type=str)
with open_db() as cursor:
query = f"SELECT id, name, latitude, longitude, title, dob, gender, description FROM users " \
f"WHERE name = \"{search}\" AND id <> {my_id} " \
f"AND id IN (" \
f"SELECT user_to FROM acquaintances " \
f"WHERE user_from = {my_id} AND confirmed = 1" \
f")"
query += gen_order(order, my_lat, my_long)
return jsonify(execute(cursor, query, my_lat, my_long))
@app.route('/searchallusers')
def search_all_users():
my_lat = request.args.get('lat', default=-1, type=float)
my_long = request.args.get('long', default=-1, type=float)
my_id = request.args.get('id', default=-1, type=int)
order = request.args.get('order', default="", type=str)
with open_db() as cursor:
query = f"SELECT id, name, latitude, longitude, title, dob, gender, description FROM users " \
f"WHERE id <> {my_id} " \
f"AND id NOT IN (" \
f"SELECT user_to FROM acquaintances " \
f"WHERE user_from = {my_id}" \
f")"
query += gen_order(order, my_lat, my_long)
return jsonify(execute(cursor, query, my_lat, my_long))
@app.route('/searchusers')
def search_users():
my_lat = request.args.get('lat', default=-1, type=float)
my_long = request.args.get('long', default=-1, type=float)
my_id = request.args.get('id', default=-1, type=int)
order = request.args.get('order', default="", type=str)
search = request.args.get('search', default="", type=str)
with open_db() as cursor:
query = f"SELECT id, name, latitude, longitude, title, dob, gender, description FROM users " \
f"WHERE name = \"{search}\" AND id <> {my_id} " \
f"AND id NOT IN (" \
f"SELECT user_to FROM acquaintances " \
f"WHERE user_from = {my_id}" \
f")"
query += gen_order(order, my_lat, my_long)
return jsonify(execute(cursor, query, my_lat, my_long))
@app.route('/details')
def get_details():
my_id = request.args.get('id', default=-1, type=int)
with open_db() as cursor:
query = f"SELECT id, name, latitude, longitude, title, dob, gender, description FROM users " \
f"WHERE id = {my_id}"
return jsonify(execute(cursor, query, 0, 0))
@app.route('/nearbyacqs')
def get_nearby():
my_lat = request.args.get('lat', default=-1, type=float)
my_long = request.args.get('long', default=-1, type=float)
my_id = request.args.get('id', default=-1, type=float)
src_range = request.args.get('range', default=0, type=float)
with open_db() as cursor:
max_lat = my_lat + src_range
min_lat = my_lat - src_range
max_long = my_long + src_range
min_long = my_long - src_range
query = f"SELECT id, name, latitude, longitude, title, dob, gender, description FROM users " \
f"WHERE (latitude BETWEEN {min_lat} AND {max_lat}) AND (longitude BETWEEN {min_long} AND {max_long}) " \
f"AND id IN (" \
f"SELECT user_to FROM acquaintances " \
f"WHERE user_from = {my_id}" \
f")"
query += gen_order("distance", my_lat, my_long)
return jsonify(execute(cursor, query, my_lat, my_long))
if __name__ == "__main__":
app.run()