forked from turicas/covid19-br
-
Notifications
You must be signed in to change notification settings - Fork 0
/
consolida.py
298 lines (272 loc) · 11.5 KB
/
consolida.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
import io
import os
from collections import Counter, defaultdict
from functools import lru_cache
from itertools import groupby
from pathlib import Path
from signal import SIGINT
from urllib.parse import parse_qs, urlparse
import rows
import scrapy
from rows.utils import load_schema
from scrapy.exceptions import CloseSpider
DATA_PATH = Path(__file__).parent / "data"
ERROR_PATH = DATA_PATH / "error"
SCHEMA_PATH = Path(__file__).parent / "schema"
POPULATION_DATA_PATH = DATA_PATH / "populacao-estimada-2019.csv"
POPULATION_SCHEMA_PATH = SCHEMA_PATH / "populacao-estimada-2019.csv"
STATE_LINKS_SPREADSHEET_ID = "1S77CvorwQripFZjlWTOZeBhK42rh3u57aRL1XZGhSdI"
@lru_cache()
def get_cities():
table = rows.import_from_csv(
POPULATION_DATA_PATH, force_types=load_schema(str(POPULATION_SCHEMA_PATH)),
)
cities = defaultdict(dict)
for row in table:
cities[row.state][row.city] = row
return cities
@lru_cache()
def get_city_code(state, city):
return get_cities()[state][city].city_ibge_code
@lru_cache()
def get_city_population(state, city):
return get_cities()[state][city].estimated_population
@lru_cache()
def get_state_code(state):
for city in get_cities()[state].values():
return city.state_ibge_code
@lru_cache()
def get_state_population(state):
return sum(city.estimated_population for city in get_cities()[state].values())
def spreadsheet_download_url(url_or_id, file_format):
parsed = urlparse(url_or_id)
if parsed.netloc == "brasil.io":
return url_or_id
elif url_or_id.startswith("http"):
spreadsheet_id = parse_qs(parsed.query)["id"][0]
else:
spreadsheet_id = url_or_id
return f"https://docs.google.com/spreadsheets/u/0/d/{spreadsheet_id}/export?format={file_format}&id={spreadsheet_id}"
class ConsolidaSpider(scrapy.Spider):
name = "consolida"
start_urls = [spreadsheet_download_url(STATE_LINKS_SPREADSHEET_ID, "csv")]
custom_settings = {
"CONCURRENT_REQUESTS_PER_DOMAIN": 4,
}
def __init__(self, boletim_filename, caso_filename, *args, **kwargs):
super().__init__(*args, **kwargs)
self.boletim_writer = rows.utils.CsvLazyDictWriter(boletim_filename)
self.caso_writer = rows.utils.CsvLazyDictWriter(caso_filename)
self.errors = defaultdict(list)
def parse(self, response):
table = rows.import_from_csv(io.BytesIO(response.body), encoding="utf-8")
for row in table:
yield scrapy.Request(
spreadsheet_download_url(row.link_planilha_consolidada, "xlsx"),
meta={"state": row.uf, "handle_httpstatus_all": True},
callback=self.parse_state_file,
)
def parse_boletim(self, state, data):
self.logger.info(f"Parsing {state} boletim")
try:
boletins = rows.import_from_xlsx(
io.BytesIO(data),
sheet_name="Boletins (FINAL)",
force_types={
"date": rows.fields.DateField,
"url": rows.fields.TextField,
"notes": rows.fields.TextField,
},
)
except Exception as exp:
self.errors[state].append(
("boletim", state, f"{exp.__class__.__name__}: {exp}")
)
return
for boletim in boletins:
boletim = boletim._asdict()
boletim_data = [item for item in boletim.values() if item]
if not boletim_data:
continue
date = boletim["date"]
url = (boletim["url"] or "").strip()
if not url:
message = f"Boletim URL not found for {state} on {date}"
self.errors[state].append(("boletim", state, message))
else:
boletim = {
"date": date,
"state": state,
"url": url,
"notes": boletim["notes"],
}
self.logger.debug(boletim)
self.boletim_writer.writerow(boletim)
def parse_caso(self, state, data):
self.logger.info(f"Parsing {state} caso")
casos = rows.import_from_xlsx(io.BytesIO(data), sheet_name="Casos (FINAL)")
cities = defaultdict(dict)
for caso in casos:
caso = caso._asdict()
caso_data = [
value
for key, value in caso.items()
if key != "municipio" and value is not None
]
if not caso_data:
continue
for key, value in caso.items():
if key == "municipio":
continue
elif key.startswith("confirmados_") or key.startswith("mortes_"):
try:
_, day, month = key.split("_")
except ValueError:
message = f"ERROR PARSING {repr(key)} - {repr(value)} - {caso}"
self.errors[state].append(("caso", state, message))
self.logger.error(message)
continue
date = f"2020-{int(month):02d}-{int(day):02d}"
if key.startswith("confirmados_"):
number_type = "confirmed"
elif key.startswith("mortes_"):
number_type = "deaths"
else:
continue
if date not in cities[caso["municipio"]]:
cities[caso["municipio"]][date] = {}
if value in (None, ""):
value = None
else:
value = str(value)
if value.endswith(".0"):
value = value[:-2]
if value.startswith("=") and value[1:].isdigit():
value = value[1:]
try:
value = int(value)
except ValueError:
message = f"ERROR converting to int: {date} {number_type} {value} {caso}"
self.errors[state].append(("caso", state, message))
self.logger.error(message)
continue
cities[caso["municipio"]][date][number_type] = value
result = []
for city, city_data in cities.items():
for date, date_data in city_data.items():
confirmed = date_data["confirmed"]
deaths = date_data["deaths"]
if confirmed is None and deaths is None:
continue
confirmed = int(confirmed) if confirmed is not None else None
deaths = int(deaths) if deaths is not None else None
row = {
"date": date,
"state": state,
"city": city if city != "TOTAL NO ESTADO" else "",
"place_type": "city" if city != "TOTAL NO ESTADO" else "state",
"confirmed": confirmed,
"deaths": deaths,
}
confirmed = row["confirmed"]
deaths = row["deaths"]
NULL = (None, "")
if (confirmed in NULL and deaths not in NULL) or (
deaths in NULL and confirmed not in NULL
):
message = (
f"ERROR: only one field is filled for {date}, {state}, {city}"
)
self.errors[state].append(("caso", state, message))
result.append(row)
row_key = lambda row: (row["state"], row["city"], row["place_type"])
result.sort(key=row_key)
groups = groupby(result, key=row_key)
for key, row_list_it in groups:
row_list = list(row_list_it)
row_list.sort(key=lambda row: row["date"])
for order_for_place, row in enumerate(row_list, start=1):
row["order_for_place"] = order_for_place
row["is_last"] = False
if row_list:
row_list[-1]["is_last"] = True
for row in result:
if row["place_type"] == "city":
if row["city"] == "Importados/Indefinidos":
row_population = None
row_city_code = None
else:
row_city_code = get_city_code(row["state"], row["city"])
row_population = get_city_population(row["state"], row["city"])
elif row["place_type"] == "state":
row_city_code = get_state_code(row["state"])
row_population = get_state_population(row["state"])
else:
message = f"Invalid row: {row}"
self.errors[state].append(("caso", state, message))
self.logger.error(message)
continue
row_deaths = row["deaths"]
row_confirmed = row["confirmed"]
confirmed_per_100k = (
100_000 * (row_confirmed / row_population)
if row_confirmed and row_population
else None
)
death_rate = (
row_deaths / row_confirmed
if row_deaths is not None and row_confirmed not in (None, 0)
else 0
)
row["estimated_population_2019"] = row_population
row["city_ibge_code"] = row_city_code
row["confirmed_per_100k_inhabitants"] = (
f"{confirmed_per_100k:.5f}" if confirmed_per_100k else None
)
row["death_rate"] = f"{death_rate:.4f}"
self.logger.debug(row)
self.caso_writer.writerow(row)
def parse_state_file(self, response):
state = response.meta["state"]
if response.status >= 400:
self.errors[state].append(
("connection", state, f"HTTP status code: {response.status}")
)
else:
try:
self.parse_boletim(state, response.body)
except Exception as exp:
self.errors[state].append(
("boletim", state, f"{exp.__class__.__name__}: {exp}")
)
try:
self.parse_caso(state, response.body)
except Exception as exp:
self.errors[state].append(
("caso", state, f"{exp.__class__.__name__}: {exp}")
)
if self.errors[state]:
error_counter = Counter(error[0] for error in self.errors[state])
error_counter_str = ", ".join(
f"{error_type}: {count}" for error_type, count in error_counter.items()
)
self.logger.error(
f"{len(self.errors[state])} errors found when parsing {state} ({error_counter_str})"
)
error_header = ("sheet", "state", "message")
errors = rows.import_from_dicts(
[dict(zip(error_header, row)) for row in self.errors[state]]
)
filename = ERROR_PATH / f"errors-{state}.csv"
if not filename.parent.exists():
filename.parent.mkdir(parents=True)
rows.export_to_csv(errors, filename)
def __del__(self):
self.boletim_writer.close()
self.caso_writer.close()
state_errors = [errors for errors in self.errors.values() if errors]
if state_errors:
# Force crawler to stop
os.kill(os.getpid(), SIGINT)
os.kill(os.getpid(), SIGINT)
raise CloseSpider(f"Error found on {len(state_errors)} state(s).")