1
|
|
|
import fsutil |
2
|
|
|
from benedict import benedict |
3
|
|
|
from slugify import slugify |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
def _slugify_names(*names): |
7
|
|
|
return sorted(set(filter(bool, [slugify(name) for name in names]))) |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
def _update_countries_data(): |
11
|
|
|
# https://www.anagrafenazionale.interno.it/il-progetto/strumenti-di-lavoro/tabelle-decodifica/ |
12
|
|
|
data_url = "https://www.anagrafenazionale.interno.it/wp-content/uploads/2021/03/tabella_2_statiesteri.xlsx" |
13
|
|
|
data = benedict.from_xls(data_url) |
14
|
|
|
data.standardize() |
15
|
|
|
# print(data.dump()) |
16
|
|
|
|
17
|
|
|
def map_item(item): |
18
|
|
|
if not item: |
19
|
|
|
return None |
20
|
|
|
code = item.get_str("codat").upper() |
21
|
|
|
if not code: |
22
|
|
|
return None |
23
|
|
|
assert len(code) == 4, f"Invalid code: '{code}'" |
24
|
|
|
|
25
|
|
|
name = item.get_str("denominazione").title() |
26
|
|
|
assert name != "", f"Invalid name: '{name}'" |
27
|
|
|
name_alt = item.get_str("denominazioneistat").title() |
28
|
|
|
name_alt_en = item.get_str("denominazioneistat_en").title() |
29
|
|
|
name_slugs = _slugify_names(name, name_alt, name_alt_en) |
30
|
|
|
|
31
|
|
|
province = "EE" |
32
|
|
|
|
33
|
|
|
date_created = item.get_datetime("datainiziovalidita") |
34
|
|
|
date_deleted = item.get_datetime("datafinevalidita") |
35
|
|
|
date_deleted_raw = item.get_str("datafinevalidita") |
36
|
|
|
if "9999" in date_deleted_raw: |
37
|
|
|
date_deleted = "" |
38
|
|
|
|
39
|
|
|
return { |
40
|
|
|
"active": False if date_deleted else True, |
41
|
|
|
"code": code, |
42
|
|
|
"date_created": date_created, |
43
|
|
|
"date_deleted": date_deleted, |
44
|
|
|
"name": name, |
45
|
|
|
"name_alt": name_alt, |
46
|
|
|
"name_alt_en": name_alt_en, |
47
|
|
|
"name_slugs": name_slugs, |
48
|
|
|
"province": province, |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
output_data = list( |
52
|
|
|
filter(bool, [map_item(benedict(item)) for item in data["values"]]) |
53
|
|
|
) |
54
|
|
|
output_data = sorted(output_data, key=lambda item: item["name"]) |
55
|
|
|
output_path = "../codicefiscale/data/countries.json" |
56
|
|
|
output_abspath = fsutil.join_path(__file__, output_path) |
57
|
|
|
fsutil.write_file_json(output_abspath, output_data, indent=4, sort_keys=True) |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
def _update_municipalities_data(): |
61
|
|
|
# https://www.anagrafenazionale.interno.it/il-progetto/strumenti-di-lavoro/tabelle-decodifica/ |
62
|
|
|
data_url = "https://www.anagrafenazionale.interno.it/wp-content/uploads/ANPR_archivio_comuni.csv" |
63
|
|
|
data = benedict.from_csv(data_url) |
64
|
|
|
data.standardize() |
65
|
|
|
|
66
|
|
|
def map_item(item): |
67
|
|
|
|
68
|
|
|
status = item.get("stato", "").upper() |
69
|
|
|
assert len(status) == 1 and status in ["A", "C"], f"Invalid status: '{status}'" |
70
|
|
|
active = status == "A" |
71
|
|
|
|
72
|
|
|
code = item.get_str("codcatastale").upper() |
73
|
|
|
assert code == "ND" or len(code) == 4, f"Invalid code: '{code}'" |
74
|
|
|
|
75
|
|
|
name = item.get_str("denominazione_it").title() |
76
|
|
|
assert name != "", f"Invalid name: {name}" |
77
|
|
|
|
78
|
|
|
name_trans = item.get_str("denomtraslitterata").title() |
79
|
|
|
name_alt = item.get_str("altradenominazione").title() |
80
|
|
|
name_alt_trans = item.get_str("altradenomtraslitterata").title() |
81
|
|
|
name_slugs = _slugify_names(name, name_trans, name_alt, name_alt_trans) |
82
|
|
|
|
83
|
|
|
province = item.get("siglaprovincia", "").upper() |
84
|
|
|
assert len(province) == 2, f"Invalid province: '{province}'" |
85
|
|
|
|
86
|
|
|
date_created = item.get_datetime("dataistituzione") |
87
|
|
|
date_deleted = item.get_datetime("datacessazione") |
88
|
|
|
date_deleted_raw = item.get_str("datacessazione") |
89
|
|
|
if "9999" in date_deleted_raw: |
90
|
|
|
date_deleted = "" |
91
|
|
|
|
92
|
|
|
return { |
93
|
|
|
"active": active, |
94
|
|
|
"code": code, |
95
|
|
|
"date_created": date_created, |
96
|
|
|
"date_deleted": date_deleted, |
97
|
|
|
"name": name, |
98
|
|
|
"name_trans": name_trans, |
99
|
|
|
"name_alt": name_alt, |
100
|
|
|
"name_alt_trans": name_alt_trans, |
101
|
|
|
"name_slugs": name_slugs, |
102
|
|
|
"province": province, |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
output_data = list( |
106
|
|
|
filter(bool, [map_item(benedict(item)) for item in data["values"]]) |
107
|
|
|
) |
108
|
|
|
output_data = sorted(output_data, key=lambda item: item["name"]) |
109
|
|
|
output_path = "../codicefiscale/data/municipalities.json" |
110
|
|
|
output_abspath = fsutil.join_path(__file__, output_path) |
111
|
|
|
fsutil.write_file_json(output_abspath, output_data, indent=4, sort_keys=True) |
112
|
|
|
|
113
|
|
|
|
114
|
|
|
def main(): |
115
|
|
|
_update_countries_data() |
116
|
|
|
_update_municipalities_data() |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
if __name__ == "__main__": |
120
|
|
|
main() |
121
|
|
|
|