-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnounou2gmaps.py
162 lines (129 loc) · 3.99 KB
/
nounou2gmaps.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
# -*- coding: utf-8 -*-
import bottle
import os
from os import path
import re
bottle.TEMPLATE_PATH.append(path.join(path.dirname(path.realpath(__file__)), "."))
app = bottle.Bottle()
def enum(**enums):
return type('Enum', (), enums)
State = enum(NONE=0, TITLE=1, ADDRESS=2, EXTRA=3)
patterns = [
{
"pattern": r'(0[0-9](\.| )?[0-9][0-9](\.| )?[0-9][0-9](\.| )?[0-9][0-9](\.| )?[0-9][0-9])',
"attribute": "phone",
"clean": "[. -]"
},
{
"pattern": r'(13600 LA CIOTAT)',
"attribute": "city",
"state": State.ADDRESS
},
{
"pattern": r'(13600)',
"attribute": "city",
"state": State.ADDRESS
},
{
"pattern": r'( ([0-9]+|CHE|AV|RUE|IMP|CLOS|VILLA) .*)',
"attribute": "address",
"state": State.ADDRESS
},
{
"pattern": r'((Journée|Périscolaire).*)',
"attribute": "extra",
"state": State.EXTRA
}
]
def nounoupattern(nounou, line):
for p in patterns:
found = re.search(p["pattern"], line)
if found:
if "clean" in p:
nounou[p["attribute"]] += " " + re.sub(p["clean"], "", found.group(1))
else:
nounou[p["attribute"]] += " " + found.group(1)
if "state" in p:
nounou["state"] = p["state"]
return re.sub(p["pattern"], '', line)
return None
def nounou2json(nounou, line):
if nounou["state"] != State.EXTRA:
newline = nounoupattern(nounou, line)
if newline is not None:
line = newline
if not line.strip(" ,\r\n"):
return
if nounou["state"] == State.TITLE:
nounou["title"] += " " + line
elif nounou["state"] == State.ADDRESS:
nounou["address"] += ", " + line
elif nounou["state"] == State.EXTRA:
nounou["extra"] += " " + line
def cleanup(nounou):
if nounou:
del nounou["state"]
if "city" in nounou:
nounou["address"] += ", " + nounou["city"]
del nounou["city"]
nounou["title"] = nounou["title"].strip(" ,\r\n")
nounou["extra"] = nounou["extra"].strip(" ,\r\n")
nounou["phone"] = nounou["phone"].strip(" ,\r\n")
nounou["address"] = re.sub(", ,", ",", re.sub("[\r\n]", "", re.sub(", ,", ",", nounou["address"].strip(" ,\r\n"))))
def nounoufile2json(filename):
nounous = []
nounou = {
"state": State.TITLE,
"title": "",
"city": "",
"phone": "",
"address": "",
"extra": ""
}
with open(filename) as f:
content = f.readlines()
for line in content:
line = line.strip(" ,\r\n")
if not line:
continue
if re.search('Madame', line):
# cleanup nounou
cleanup(nounou)
# create nounou
nounou = {
"state": State.TITLE,
"title": "",
"city": "",
"phone": "",
"address": "",
"extra": ""
}
nounous.append(nounou)
nounou2json(nounou, line)
cleanup(nounou)
return {
'nounous': nounous
}
@app.get("/")
def home():
from bottle import redirect
redirect("/nounous")
@app.get("/nounous")
def index():
from bottle import template
return template(
"nounou2gmaps.html",
geo_key=os.environ.get(
"GOOGLE_GEOCODING_APIKEY",
"Set your Google Geocoding API key"
)
)
@app.get("/nounous/marker.json")
def marker():
nounoudict = nounoufile2json(path.join(path.dirname(path.realpath(__file__)), "ListeAssMat-2015-05-13.txt"))
# for testing API, enable DEBUG and it returns only first 2 records
if os.environ.get("DEBUG", None) == "true":
nounoudict["nounous"] = nounoudict["nounous"][:2]
return nounoudict
if __name__ == "__main__":
app.run(host="0.0.0.0", port=os.environ.get("PORT", 8787))