This repository has been archived by the owner on Apr 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.py
106 lines (89 loc) · 3.46 KB
/
app.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
from flask import Flask, request, jsonify
import json
import elasticsearch as es
from worksites import WorkSiteName, geocode_worksites
from queries import get_match_response, request_elastic, check_if_same_score
app = Flask(__name__, static_url_path='')
ELASTIC_URL = "http://elasticsearch-master:9200"
conn = es.Elasticsearch(ELASTIC_URL)
geo_threshold = 0.7
@app.route('/test')
def dates():
test = {"hello": "world!"}
return jsonify(test)
@app.route('/match', methods=['POST'])
def get_match():
siret = request.json["siret"]
name = request.json["name"]
postcode = request.json["postcode"] if "postcode" in request.json else None
city = request.json["city"] if "city" in request.json else None
address = request.json["address"]
wks = [WorkSiteName(**{
"siret":siret,
"name": name,
"postcode": postcode,
"cityname": city,
"address": address
})]
geowk = geocode_worksites(wks)[0]
answer, found, useGeocode, useAddress = get_match_response(conn, geoworksite=geowk, siret_truth=geowk.siret, geo_threshold=geo_threshold)
if answer:
if found:
return jsonify(
{
"match": True,
"with_geocoding": True if useGeocode else False,
"with_address": True if useAddress else False,
"message": "Found",
"original_name": name,
"found_name": answer["_source"]["nom_complet"],
"original_siret": siret,
"found_siret": answer["_source"]["siret"]
}
)
else:
return jsonify(
{
"match": False,
"message": "Not found but results",
"original_name": name,
"best_found_name": answer["_source"]["nom_complet"],
"original_siret": siret,
"best_found_siret": answer["_source"]["siret"]
}
)
else:
return jsonify(
{
"match": False,
"message": "Not found and no result at all",
"original_name": name,
"original_siret": siret,
}
)
@app.route('/search', methods=['POST'])
def get_info_siret():
name = request.json["name"]
postcode = request.json["postcode"] if "postcode" in request.json else None
city = request.json["city"] if "city" in request.json else None
address = request.json["address"] if "address" in request.json else None
if "use_geocode" in request.json:
use_geocode = False if request.json["use_geocode"] == "false" else True
else:
use_geocode = True
if "use_address" in request.json:
use_address = False if request.json["use_address"] == "false" else True
else:
use_address = True
wks = [WorkSiteName(**{
"name": name,
"postcode": postcode,
"cityname": city,
"address": address
})]
geowk = geocode_worksites(wks)[0]
output, is_use_geocode, is_use_address = request_elastic(conn, geowk, geo_threshold=geo_threshold, use_geo=use_geocode, use_address=use_address)
output = check_if_same_score(output)
return jsonify([o["_source"] for o in output] if output else [])
if __name__ == '__main__':
app.run(host='0.0.0.0')