-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
155 lines (117 loc) · 3.72 KB
/
api.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
import csv
from flask import Flask, jsonify, request, render_template
app = Flask(__name__)
filenames = ['art_and_culture', 'beaches',
'kid_friendly', 'museums', 'outdoors']
def fetch_values(filename):
"""
Fetch Destination details from files.
...
Parameters:
----------
filename: file to fetch values from
limit: limit the number of rows we fetch \n
offset: rows we skip
"""
fetched_rows = []
with open('data/{}.csv'.format(filename), newline='', encoding='utf-8') as f:
values = request.get_json()
if not values:
response = {'message': 'No data found!'}
return jsonify(response), 400
required_fields = ['limit', 'offset']
if not all(f in values for f in required_fields):
response = {'message': 'Required data missing!'}
return jsonify(response), 400
limit = values['limit']
offset = values['offset']
reader = csv.DictReader(f)
for i in range(offset):
try:
next(reader)
except StopIteration:
return jsonify({'response': 'No more records'}), 400
for index, row in enumerate(reader):
fetched_rows.append(row)
if index >= limit:
break
response = [row for row in fetched_rows]
return jsonify(response), 200
@app.route('/', methods=['GET'])
def index():
"""
Index page
"""
return 'Welcome to the index page!'
@app.route('/policy', methods=['GET'])
def policy():
return render_template('policy.html')
@app.route('/categories', methods=['GET'])
def categories():
"""
Fetch Destination categories
"""
categories = []
for file in filenames:
with open('data/{}.csv'.format(file), newline='', encoding='utf-8') as f:
reader = csv.reader(f)
next(reader)
row_count = sum(1 for row in reader)
categories.append({'categories': file, 'length': row_count})
return jsonify(categories), 200
@app.route('/searchdestination', methods=['POST'])
def searchdestination():
"""
Search a destination from the files.
...
"""
values = request.get_json()
if not values:
response = {'message': 'No data found!'}
return jsonify(response), 400
required_fields = ['query']
if not all(f in values for f in required_fields):
response = {'message': 'Required data missing!'}
return jsonify(response), 400
query = values['query']
results=[]
destinations=[]
for file in filenames:
with open('data/{}.csv'.format(file), newline='', encoding='utf-8') as f:
reader = csv.DictReader(f)
results.append([row for row in reader if query.lower() in row['title'].lower()])
for r in results:
[destinations.append(row) for row in r if row not in destinations and row!=[]]
return jsonify(destinations), 200
@app.route('/art_and_culture', methods=['POST'])
def art_and_culture():
"""
Fetch Art and Culture Destinations:
"""
return fetch_values('art_and_culture')
@app.route('/beaches', methods=['POST'])
def beaches():
"""
Fetch Beach Destinations:
"""
return fetch_values('beaches')
@app.route('/kid_friendly', methods=['POST'])
def kid_friendly():
"""
Fetch Kid friendly Destinations:
"""
return fetch_values('kid_friendly')
@app.route('/museums', methods=['POST'])
def museums():
"""
Fetch Museum Destinations:
"""
return fetch_values('museums')
@app.route('/outdoors', methods=['POST'])
def outdoors():
"""
Fetch Outdoors Destinations:
"""
return fetch_values('outdoors')
if __name__ == '__main__':
app.run()