-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
80 lines (60 loc) · 1.92 KB
/
server.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
import http
import json
import logging
import sys
import flask
from flask_cors import CORS
import src.eventbrite
import src.events_cache
import src.events_filter
import src.weather
APP = flask.Flask(__name__,
static_url_path='',
static_folder='frontend/build')
#CORS(APP)
def send_success(data):
return json.dumps(data)
def send_error(message):
return json.dumps({
'_error': message
})
@APP.route('/')
def root():
return APP.send_static_file('index.html')
@APP.route('/conditions', methods=['GET'])
def get_current_conditions():
conditions = src.weather.get_weather_by_city('sydney').get_json()
try:
return send_success({
"conditions": conditions
})
except Exception as e:
return send_error(e.args)
@APP.route('/events/recommended', methods=['POST'])
def get_recommended_events():
if flask.request.content_type != 'application/json':
flask.abort(http.HTTPStatus.BAD_REQUEST)
request_data = flask.request.json
tags = set()
try:
tags = set(request_data['tags'])
except KeyError:
logging.error("Failed to retrieve tags from malformed request body.")
flask.abort(http.HTTPStatus.BAD_REQUEST)
collected_events = src.events_cache.load_events()
filtered_events = src.events_filter.filter_events(collected_events, tags)
try:
return send_success([event.get_serializable_json() for event in filtered_events])
except ValueError as e:
return send_error(e.args)
@APP.route('/events/get_all', methods=['GET'])
def get_all_events():
events = list(map(lambda event: event.get_json(), src.eventbrite.get_events()))
try:
return send_success({
"events": events
})
except ValueError as e:
return send_error(e.args)
if __name__ == '__main__':
APP.run()