forked from alltheplaces/alltheplaces
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdict_parser.py
262 lines (216 loc) · 7.33 KB
/
dict_parser.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
from locations.items import Feature
class DictParser:
ref_keys = ["ref", "id", "store-id", "storeNumber", "shop-number", "LocationID", "slug"]
name_keys = ["name", "store-name", "display-name", "title"]
house_number_keys = ["house-number", "house-no", "street-number", "street-no"]
street_address_keys = [
# EN
"street-address",
"address1",
"address-line1",
"line1",
"address-line-one",
# JP
"町域以下住所", # "address below town limits"
]
city_keys = [
# EN
"address-locality",
"city",
"address-city",
"town",
"locality",
"suburb",
"city-name",
# JP
"市区町村", # "municipality"
]
region_keys = [
# EN
"address-region",
"region",
"state",
"state-province",
"province",
"state-code",
"county",
"state-name",
# JP
"都道府県", # "prefecture"
]
country_keys = [
"country-code",
"address-country",
"country",
"country-name",
]
isocode_keys = [
"iso-code",
]
postcode_keys = [
# EN
"postal-code",
"post-code",
"postcode",
"zip",
"zipcode",
"address-post-code",
"postal",
"zip-code",
# JP
"郵便番号", # "post code"
]
email_keys = ["email", "contact-email", "email-address", "email1"]
phone_keys = [
"phone-number",
"phone",
"telephone",
"tel",
"telephone-number",
"telephone1",
"contact-number",
"phone-no",
]
lat_keys = [
"latitude",
"lat",
"display-lat",
"yext-display-lat",
"mapLatitude",
]
lon_keys = [
"longitude",
"lon",
"long",
"lng",
"display-lng",
"yext-display-lng",
"mapLongitude",
]
website_keys = ["url", "website", "permalink", "store-url", "storeURL"]
@staticmethod
def parse(obj) -> Feature:
item = Feature()
item["ref"] = DictParser.get_first_key(obj, DictParser.ref_keys)
item["name"] = DictParser.get_first_key(obj, DictParser.name_keys)
location = DictParser.get_first_key(
obj, ["location", "geo-location", "geo", "geo-point", "geocodedCoordinate", "coordinates"]
)
# If not a good location object then use the parent
if not location or not isinstance(location, dict):
location = obj
item["lat"] = DictParser.get_first_key(location, DictParser.lat_keys)
item["lon"] = DictParser.get_first_key(location, DictParser.lon_keys)
address = DictParser.get_first_key(obj, ["address", "addr", "storeaddress"])
if address and isinstance(address, str):
item["addr_full"] = address
if not address or not isinstance(address, dict):
address = obj
item["housenumber"] = DictParser.get_first_key(address, DictParser.house_number_keys)
item["street"] = DictParser.get_first_key(address, ["street", "street-name"])
item["street_address"] = DictParser.get_first_key(address, DictParser.street_address_keys)
item["city"] = DictParser.get_first_key(address, DictParser.city_keys)
item["state"] = DictParser.get_first_key(address, DictParser.region_keys)
item["postcode"] = DictParser.get_first_key(address, DictParser.postcode_keys)
country = DictParser.get_first_key(address, DictParser.country_keys)
if country and isinstance(country, dict):
isocode = DictParser.get_first_key(country, DictParser.isocode_keys)
if isocode and isinstance(isocode, str):
item["country"] = isocode
# TODO: Handle other potential country fields inside the dict?
else:
item["country"] = country
contact = DictParser.get_first_key(obj, ["contact"])
if not contact or not isinstance(contact, dict):
contact = obj
item["email"] = DictParser.get_first_key(contact, DictParser.email_keys)
item["phone"] = DictParser.get_first_key(contact, DictParser.phone_keys)
item["website"] = DictParser.get_first_key(contact, DictParser.website_keys)
return item
@staticmethod
def get_first_key(obj, keys):
for key in keys:
variations = DictParser.get_variations(key)
for variation in variations:
if obj.get(variation):
return obj[variation]
@staticmethod
def get_variations(key):
results = set()
results.add(key)
lower = key.lower()
results.add(lower)
upper = key.upper()
results.add(upper)
title = key.title()
results.add(title)
flatcase = key.lower().replace("-", "")
results.add(flatcase)
FLATCASEUPPER = flatcase.upper()
results.add(FLATCASEUPPER)
camelCase = key[0].lower()
i = 1
while i < len(key):
if key[i] == "-":
i += 1
camelCase += key[i].upper()
else:
camelCase += key[i]
i += 1
results.add(camelCase)
PascalCase = camelCase[0].upper() + camelCase[1:]
results.add(PascalCase)
snake_case = key.lower().replace("-", "_")
results.add(snake_case)
SCREAMING_SNAKE_CASE = key.upper().replace("-", "_")
results.add(SCREAMING_SNAKE_CASE)
camel_Snake_Case = key[0].lower()
i = 1
while i < len(key):
if key[i] == "-":
i += 1
camel_Snake_Case += "_"
camel_Snake_Case += key[i].upper()
else:
camel_Snake_Case += key[i]
i += 1
results.add(camel_Snake_Case)
Pascal_Snake_Case = camel_Snake_Case[0].upper() + camel_Snake_Case[1:]
results.add(Pascal_Snake_Case)
return results
@staticmethod
def get_nested_key(obj, key):
"""
Return value for first matching key in an object, traversing lists and dicts.
:param obj: the object to traverse
:param key: the key to match
:return: the first matching value, or None
"""
if isinstance(obj, dict):
for k, v in obj.items():
if k == key:
return v
if val := DictParser.get_nested_key(v, key):
return val
elif isinstance(obj, list):
for x in obj:
if val := DictParser.get_nested_key(x, key):
return val
return None
@staticmethod
def iter_matching_keys(obj, key):
"""
An iterator for values for all matching keys in an object, traversing lists and dicts.
:param obj: the object to traverse
:param key: the key to match
:return: value iterator for all matching keys
"""
if isinstance(obj, dict):
for k, v in obj.items():
if k == key:
yield v
else:
yield from DictParser.iter_matching_keys(v, key)
elif isinstance(obj, list):
for x in obj:
yield from DictParser.iter_matching_keys(x, key)