-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
67 lines (60 loc) · 1.97 KB
/
main.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
import collections
import googlemaps
from flask import Flask, request, render_template, Response, make_response, url_for
import json
app = Flask(__name__)
'''
app.config['MONGODB_SETTINGS'] = {
'db': 'medhacks',
'host': ''
}
'''
#client = pymongo.MongoClient("mongodb+srv://admin:<password>@medhacks-s3dxi.gcp.mongodb.net/test?retryWrites=true&w=majority")
#db = client.test
#mydoc = db.medhacks.find()
#for x in mydoc:
# print(x)
@app.route("/home", methods = ["GET"])
def home():
return render_template("home.html")
@app.route("/", methods = ["GET", "POST", "OPTIONS"])
def home_page():
if request.method == "OPTIONS":
response = make_response()
response.headers["Access-Control-Allow-Methods"] = "*"
response.headers["Access-Control-Allow-Origin"] = "*"
response.headers["Access-Control-Allow-Headers"] = "*"
response.headers["Access-Control-Max-Age"] = 86400
response.headers["Content-Type"] = "application/json"
return response
import json
data = json.loads(str(request.data).replace('\\n', '')[2:-1])
lat = data['latitude']
lon = data['longitude']
client = googlemaps.Client(key= 'AIzaSyB2ekBwXRcw-dI5afMnf4Nck7JwXpNEDXI')
geocode = client.reverse_geocode((lat, lon))
data['location'] = geocode[0]['formatted_address']
store = collections.defaultdict(list)
try:
with open("store.json") as file:
store = json.load(file)
except FileNotFoundError:
pass
with open("store.json", "w") as file:
if data:
store['data'].append(data)
json.dump(store, file)
return Response(status=200)
@app.route('/info')
def info():
store = None
try:
with open("store.json") as file:
store = json.load(file)
except FileNotFoundError:
pass
if not store:
return Response(status=404)
return render_template('list.html', info = store)
if __name__ == '__main__':
app.run(debug=True, port=8080)