-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
199 lines (175 loc) · 5.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
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
from flask import Flask, jsonify, request
from flask_cors import CORS
from pymongo import MongoClient
from bson.json_util import dumps
from bson.objectid import ObjectId
from bson.errors import InvalidId
import re
app = Flask(__name__)
CORS(app)
#Copypaste the connection string from Mongodb Atlas
mongo = MongoClient("connection string")
#Select database
db = mongo['enem_db']
#Select collection
schools = db['schools']
#get school by search parameters
@app.route('/api/schools', methods=['GET'])
def get_school_by_name():
#named parameters ?q and filter $year
name = request.args.get('q')
year = request.args.get('year')
state = request.args.get('state')
limit = 10
#prepare regex, case insensitive
search_string = '.*{}.*'.format(name)
rgx = re.compile(search_string, re.IGNORECASE)
if state:
#ommit the _id to prevent error
query = schools.find({"ESCOLA" : rgx, "NU_ANO" : year, "UF" : state}).sort('ESCOLA').limit(limit)
else:
#query collection with regex
query = schools.find({"ESCOLA" : rgx, "NU_ANO" : year}).sort('ESCOLA').limit(limit)
#Process the data from the query cursor creating a dict
output = {}
output['number_of_results'] = query.count()
#Results key
results = []
for school in query:
results.append(
{
'id' : str(school['_id']),
'year' : school['NU_ANO'],
'region' : school['REGIAO'],
'state' : school['UF'],
'city' : school['MUNICIPIO'],
'name' : school['ESCOLA'],
'type' : school['TIPOESCOLA'],
'avg_ch' : school['MEDIA CH'],
'avg_cn' : school['MEDIA CN'],
'avg_lc' : school['MEDIA LC'],
'avg_math' : school['MEDIA MT'],
'avg_essay' : school['MEDIA REDACAO']
}
)
output['results'] = results
return jsonify(output)
#get school by id
@app.route('/api/schools/<school_id>', methods=['GET'])
def get_school_by_id(school_id):
try:
query = schools.find_one({'_id': ObjectId(school_id)}, {'_id':0})
#output = [query]
result = []
result.append(
{
'city': query['MUNICIPIO'],
'year': query['NU_ANO'],
'region': query['REGIAO'],
'type': query['TIPOESCOLA'],
'state': query['UF'],
'school_name': query['ESCOLA'],
'avg_ch': query['MEDIA CH'],
'avg_cn': query['MEDIA CN'],
'avg_lc': query['MEDIA LC'],
'avg_mt': query['MEDIA MT'],
'avg_essay': query['MEDIA REDACAO']
}
)
except InvalidId:
result = 'ID not found'
return jsonify({'results' : result})
@app.route('/api/schools/year', methods=['GET'])
def get_school_grades():
#search parameters
name = request.args.get('name')
city = request.args.get('city')
state = request.args.get('state')
#school name regex
search_string = '.*{}.*'.format(name)
rgx = re.compile(search_string, re.IGNORECASE)
#city regex
city_rgx = re.compile('.*{}.*'.format(city), re.IGNORECASE)
query = schools.find({"ESCOLA" : rgx, "MUNICIPIO": city_rgx, "UF" : state}).sort('NU_ANO')
output = {}
output['number_of_results'] = query.count()
#Results key
results = []
for result in query:
results.append(
{
'id' : str(result['_id']),
'year' : result['NU_ANO'],
'region' : result['REGIAO'],
'state' : result['UF'],
'city' : result['MUNICIPIO'],
'name' : result['ESCOLA'],
'type' : result['TIPOESCOLA'],
'avg_ch' : result['MEDIA CH'],
'avg_cn' : result['MEDIA CN'],
'avg_lc' : result['MEDIA LC'],
'avg_math' : result['MEDIA MT'],
'avg_essay' : result['MEDIA REDACAO']
}
)
output['results'] = results
return jsonify(output)
@app.route('/api/states', methods=['GET'])
def get_grades_by_state():
col = db['states']
state = request.args.get('state')
year = request.args.get('year')
if state:
if year:
query = col.find({"UF" : state, "NU_ANO" : year}).sort('NU_ANO')
else:
query = col.find({"UF" : state}).sort('NU_ANO')
else:
query = col.find().sort('NU_ANO')
output = {}
output['number_of_results'] = query.count()
#Results key
results = []
for result in query:
results.append(
{
'id' : str(result['_id']),
'year' : result['NU_ANO'],
'state' : result['UF'],
'avg_ch' : result['MEDIA CH'],
'avg_cn' : result['MEDIA CN'],
'avg_lc' : result['MEDIA LC'],
'avg_math' : result['MEDIA MT'],
'avg_essay' : result['MEDIA REDACAO']
}
)
output['results'] = results
return jsonify(output)
@app.route('/api/national', methods=['GET'])
def get_nation_grades():
col = db['national']
year = request.args.get('year')
if year:
query = col.find({"NU_ANO" : year}).sort('NU_ANO')
else:
query = col.find().sort("NU_ANO")
output = {}
output['number_of_results'] = query.count()
#Results key
results = []
for result in query:
results.append(
{
'id' : str(result['_id']),
'year' : result['NU_ANO'],
'avg_ch' : result['MEDIA CH'],
'avg_cn' : result['MEDIA CN'],
'avg_lc' : result['MEDIA LC'],
'avg_math' : result['MEDIA MT'],
'avg_essay' : result['MEDIA REDACAO']
}
)
output['results'] = results
return jsonify(output)
if __name__ == "__main__":
app.run(debug=True)