-
Notifications
You must be signed in to change notification settings - Fork 1
/
kerofis.py
574 lines (444 loc) · 16.8 KB
/
kerofis.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
#!usr/local/bin/python
# -*- coding: utf-8 -*-
#-------------------------------------------------------------------------------
# Name: API Kerofis
#
# Author: Maël REBOUX
#
# Created: 04/2016
# Licence: GNU GPL ?
#
# Provides methods to access to kerofos data data
# data provides from open data files
#
# for web REST API
from flask import Flask
from flask import jsonify
#from flask import json
from flask import Response
from flask import request
from flask import render_template
# for debug and error handling
from flask import abort
from werkzeug.debug import get_current_traceback
# for accessing the postgreSQL database
import psycopg2
#-------------------------------------------------------------------------------
app = Flask(__name__)
#-------------------------------------------------------------------------------
# general config parameters
sConnPostgre = "host='localhost' dbname='kerofis' user='kerofis' password='kerofis'"
licence = "Open Database License (ODbL) v1.0"
attribution_en = "KerOfis by osm-bzh"
attribution_fr = "KerOfis par osm-bzh"
attribution_br = "KerOfis gant osm-bzh"
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
@app.route("/kerofis/")
def index():
return "API KerOfis : index"
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# /kerofis/infos
# -> return global informations on the kerofis database
# last date import / refresh
# array of all the imports
@app.route("/kerofis/infos")
@app.route("/kerofis/infos/")
def infos():
#return "API kerofis : infos"
try:
# get a connection, if a connect cannot be made an exception will be raised here
pgDB = psycopg2.connect(sConnPostgre)
# pgDB.cursor will return a cursor object, you can use this cursor to perform queries
pgCursor = pgDB.cursor()
# the query
pgCursor.execute("""SELECT * FROM infos_file_import LIMIT 1""")
# get only first record
record = pgCursor.fetchone()
date_last_import = str(record[0])
# pass the data to the JSON template
return jsonify(
name = "KerOfis data provided by Ofis publik ar brezhoneg",
licence = licence,
attribution_en = attribution_en,
attribution_fr = attribution_fr,
attribution_br = attribution_br,
last_file_import = date_last_import,
file_imports = {}
)
# closing cursor and connection to the database
pgCursor.close()
pgDB.close()
except Exception,e:
# out the error to the log
track= get_current_traceback(skip=1, show_hidden_frames=True, ignore_system_exceptions=False)
track.log()
# return customized error message in JSON
return jsonify(error = str(e)), 500
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# /kerofis/stats/
@app.route("/kerofis/stats")
@app.route("/kerofis/stats/")
def stats():
return "API kerofis : stats"
# TODO renvoyer plutôt une page HTML explicative des méthodes"""
# stats_municipality_type_of_place --> pour avoir les formes
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# /kerofis/municipalities
# -> return all municipalities
# /kerofis/municipalities/search/?insee={insee}
# /kerofis/municipalities/search/?name:br{name:br}
# /kerofis/municipalities/search/?name:fr{name:fr}
# -> return 0, 1 or more municipality
@app.route("/kerofis/municipalities")
@app.route("/kerofis/municipalities/")
def municipalities_index():
# return a json response with the list of all the municipalities concerned in the database
# datas provided by the stats_municipality table
# insee | kumun | nb
# return "API kerofis : stats : par commune : toutes les communes"
try:
# get a connection, if a connect cannot be made an exception will be raised here
pgDB = psycopg2.connect(sConnPostgre)
# pgDB.cursor will return a cursor object, you can use this cursor to perform queries
pgCursor = pgDB.cursor()
#-------------------------------------------------------------------------------
# first : get count of municipality in the database
pgCursor.execute("""SELECT COUNT(*) AS count FROM stats_municipality""")
oneRecord = pgCursor.fetchone()
NbOfMunicipalities = str(oneRecord[0])
#-------------------------------------------------------------------------------
# second : loop on all the municipalities
# the query
pgCursor.execute("""SELECT * FROM stats_municipality""")
# get all the records
records = pgCursor.fetchall()
# return raw json records in text/html + no attributes name
#return str(json.dumps(records))
# return correct application/json answer BUT no attributes name
#return jsonify({'municipalities':records})
# declare array
json_array = []
# loop on each record to built handmade json
for record in records:
json_str = "{'insee':'" + record[0] + "',"
json_str += "'name:br':'" + record[1] + "',"
json_str += "'name:fr':'" + record[2] + "',"
json_str += "'nb': " + str(record[3]) + "}"
json_array.append(json_str)
# then return a beautiful json
#return jsonify({'municipalities':json_array})
return jsonify(
licence = licence,
attribution_en = attribution_en,
attribution_fr = attribution_fr,
attribution_br = attribution_br,
count = NbOfMunicipalities,
municipalities = json_array
)
#return "json_response"
# closing cursor and connection to the database
pgCursor.close()
pgDB.close()
except Exception,e:
# out the error to the log
track= get_current_traceback(skip=1, show_hidden_frames=True, ignore_system_exceptions=False)
track.log()
# error
return jsonify(error = str(e)), 500
@app.route("/kerofis/municipalities/search/", methods=['GET'])
def municipalities_search():
# search by insee OR name_fr OR name_br
# get all arguments
code_insee = request.args.get('insee', '')
name_br = request.args.get('name:br', '')
name_fr = request.args.get('name:fr', '')
sSQL = ""
if (code_insee != '') :
# search by code insee
# if lenght = 2 -> search by departement
if len(code_insee) == 2 :
sSQL = "SELECT * FROM stats_municipality WHERE insee LIKE '" + code_insee + "%'"
return municipalities_search_query(sSQL)
pass
# if lenght = 5 -> searching one municipality
if len(code_insee) == 5 :
sSQL = "SELECT * FROM stats_municipality WHERE insee = '" + code_insee + "'"
return municipalities_search_query(sSQL)
pass
# else : output error
else :
# error
return jsonify(error = "code insee : bad argument"), 400
pass
if (name_br != '') :
# search by breton name
if len(name_br) < 3 :
# error
return jsonify(error = "name:br argument is too short"), 400
else :
sSQL = "SELECT * FROM stats_municipality WHERE LOWER(name_br) LIKE LOWER('%" + name_br + "%')"
return municipalities_search_query(sSQL)
pass
if (name_fr != '') :
# search by french name
if len(name_fr) < 3 :
# error
return jsonify(error = "name:fr argument is too short"), 400
else :
sSQL = "SELECT * FROM stats_municipality WHERE LOWER(name_fr) LIKE LOWER('%" + name_fr + "%')"
return municipalities_search_query(sSQL)
pass
# if nothing -> error 500
return jsonify(error = "error in municipality search"), 500
pass
def municipalities_search_query(sSQL):
# perform the query
try:
pgDB = psycopg2.connect(sConnPostgre)
pgCursor = pgDB.cursor()
pgCursor.execute(sSQL)
records = pgCursor.fetchall()
# declare array + counter
json_array = []
loop_counter = 0
# loop
for record in records:
json_str = "{'insee':'" + record[0] + "',"
json_str += "'name:br':'" + record[1] + "',"
json_str += "'name:fr':'" + record[2] + "',"
json_str += "'nb': " + str(record[3]) + "}"
json_array.append(json_str)
loop_counter += 1
# then return a beautiful json
return jsonify(
licence = licence,
attribution_en = attribution_en,
attribution_fr = attribution_fr,
attribution_br = attribution_br,
count = loop_counter,
municipalities = json_array
)
# closing cursor and connection to the database
pgCursor.close()
pgDB.close()
pass
except Exception, e:
# out the error to the log
track= get_current_traceback(skip=1, show_hidden_frames=True, ignore_system_exceptions=False)
track.log()
# error
return jsonify(error = str(e)), 500
#return sSQL
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# /kerofis/search/?insee={insee}
# -> return all records for one municipality
#
# /kerofis/search/?
# insee={insee} [mandatory] : '12345' OR *
# lang={br|fr} [mandatory] : langage of the name to request
# name={name} [mandatory] : 'XXXX' OR *
# type={type} [optionnal] : cf table stats_municipality_type_of_place
# sortby={br_asc|br_desc|fr_asc|fr_desc} [optionnal] : sorting key
#
@app.route("/kerofis/search/", methods=['GET'])
def kerofis_search():
# get all arguments
code_insee = request.args.get('insee', '') # mandatory
lang = request.args.get('lang', '') # mandatory
name = request.args.get('name', '') # mandatory
stype = request.args.get('type', '') # optionnal
sortby = request.args.get('sortby', '') # optionnal -> default = requested langage + ascendant
args = "args = code_insee:"+code_insee+" | lang:"+lang+" | name:"+name+" | type:"+stype
# for say if the query args are welcomed
control = False
#
# basic tests : insee
if len(code_insee) == 1 :
if code_insee == '*':
# ok
control = True
pass
else:
# error
control = False
# return customized error message in JSON
return jsonify(error = "bad code insee argument"), 400
elif len(code_insee) == 5 :
# test if integer TODO
# ok
control = True
pass
else:
# error : too short
control = False
# return customized error message in JSON
return jsonify(error = "bad code insee argument"), 400
#
# basic tests : lang
if len(lang) == 2 :
if (lang == "fr") or (lang == "br") :
# ok
control = True
pass
else :
# error
control = False
# return customized error message in JSON
return jsonify(error = "bad langage argument"), 400
else :
# error
control = False
# return customized error message in JSON
return jsonify(error = "bad langage argument"), 400
#
# basic tests : name
# first : trim spaces
name = name.replace(" ","")
# checks
if (name == "*") and (code_insee == "*") :
# not allowed to return all the database
return jsonify(error = "not allowed to return all the database : supply insee or name argument"), 400
if (name != "*") :
if len(name) < 3 :
# error
control = False
# return customized error message in JSON
return jsonify(error = "name argument too short"), 400
elif len(name) > 50 :
# error
control = False
# return customized error message in JSON
return jsonify(error = "name argument too long"), 400
#
# basic tests : type of objects queried
# TODO
if stype :
control = False
return jsonify(error = "type argument not yet managed"), 500
#
# basic tests : sort by
if sortby :
# test all the cases
if (sortby == "fr_asc") or (sortby == "fr_desc") or (sortby == "br_asc") or (sortby == "br_desc") :
control = True
pass
else :
# error
control = False
# return customized error message in JSON
return jsonify(error = "bad sortby argument"), 400
else :
# apply the queried langage + ascendant sort
sortby = lang + "_asc"
# test control, in case of…
if control == False :
# stop
return jsonify(error = "something went wrong, check arguments"), 400
else :
# all is right, let's built the query
sSQL = "SELECT niv, deiziad_degemer, insee, kumun, rummad, stumm_orin, stumm_dibab FROM kerofis WHERE"
# code insee + name
if (code_insee != "*") and (name != "*") : sSQL += " insee='" + str(code_insee) + "' AND"
# code insee alone
elif (code_insee != "*") and (name == "*") : sSQL += " insee='" + str(code_insee) + "'"
# langage impacts column choice for the name request
# comparison is done case insensitive
# TODO : manage accents
# first : define if query an entire municipality or not
if (name != "*") :
if (lang == "fr") : sSQL += " LOWER(stumm_orin) LIKE '%" + name.lower() + "%'"
elif (lang == "br") : sSQL += " LOWER(stumm_dibab) LIKE '%" + name.lower() + "%'"
# no need else
# type
# TODO
# exclude municipality record because we looking for way or POI
sSQL += " AND rummad <> 'Kumun'"
# sorting
if (sortby == "fr_asc") : sSQL += " ORDER BY stumm_orin ASC"
elif (sortby == "fr_desc") : sSQL += " ORDER BY stumm_orin DESC"
elif (sortby == "br_asc") : sSQL += " ORDER BY stumm_dibab ASC"
elif (sortby == "br_desc") : sSQL += " ORDER BY stumm_dibab DESC"
# for test / debug
#return jsonify(query = sSQL, lang = lang), 404
# go to the query
return kerofis_search_query(sSQL)
#pass
#args += " | sortby:"+sortby
#return args + " | control = " + str(control) + " FIN"
pass
def kerofis_search_query(sql):
# perform the query
try:
pgDB = psycopg2.connect(sConnPostgre)
pgCursor = pgDB.cursor()
pgCursor.execute(sql)
records = pgCursor.fetchall()
# declare array + counter
json_array = []
loop_counter = 0
# loop
for record in records:
# store attributes from order of the database attributes
niv = record[0]
deiziad_degemer = record[1]
insee = record[2]
kumun = record[3]
rummad = record[4]
stumm_orin = record[5]
stumm_dibab = record[6]
# building the json
json_str = "{'insee':'" + insee + "',"
json_str += "'municipality':'" + kumun + "',"
json_str += "'validation_date':'" + str(deiziad_degemer) + "',"
json_str += "'id':'" + str(niv) + "',"
json_str += "'type':'" + rummad + "',"
json_str += "'name:br':'" + stumm_orin + "',"
json_str += "'name:fr':'" + stumm_dibab + "}"
json_array.append(json_str)
loop_counter += 1
# then return a beautiful json
return jsonify(
licence = licence,
attribution_en = attribution_en,
attribution_fr = attribution_fr,
attribution_br = attribution_br,
count = loop_counter,
places = json_array
)
# closing cursor and connection to the database
pgCursor.close()
pgDB.close()
pass
except Exception, e:
# out the error to the log
track= get_current_traceback(skip=1, show_hidden_frames=True, ignore_system_exceptions=False)
track.log()
return jsonify(error = str(e)), 500
#raise e
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
@app.errorhandler(400)
def internal_error(error):
strResponse = str(error)
response = Response(strResponse, status=400, mimetype='text/html')
return response
@app.errorhandler(500)
def internal_error(error):
strResponse = str(error)
response = Response(strResponse, status=500, mimetype='text/html')
return response
@app.errorhandler(404)
def internal_error(error):
strResponse = str(error)
response = Response(strResponse, status=404, mimetype='text/html')
return response
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
if __name__ == "__main__":
app.debug = True
app.run()