-
Notifications
You must be signed in to change notification settings - Fork 128
/
ocupacao.py
98 lines (83 loc) · 3.12 KB
/
ocupacao.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
import argparse
import datetime
from pathlib import Path
from rows.utils import CsvLazyDictWriter
from tqdm import tqdm
from covid19br.elasticsearch import ElasticSearch
DOWNLOAD_PATH = Path(__file__).parent / "data" / "ocupacao"
if not DOWNLOAD_PATH.exists():
DOWNLOAD_PATH.mkdir(parents=True)
FIELD_CONVERTERS = {
"estado": {"name": "", "converter": ""},
"estadoSigla": {"name": "", "converter": ""},
"municipio": {"name": "", "converter": ""},
"cnes": {"name": "", "converter": ""},
"dataNotificacaoOcupacao": {"name": "", "converter": ""},
"ocupHospCli": {"name": "", "converter": ""},
"ocupHospUti": {"name": "", "converter": ""},
"ocupSRAGCli": {"name": "", "converter": ""},
"ocupSRAGUti": {"name": "", "converter": ""},
"altas": {"name": "", "converter": ""},
"obitos": {"name": "", "converter": ""},
"ocupacaoInformada": {"name": "", "converter": ""},
"algumaOcupacaoInformada": {"name": "", "converter": ""},
"nomeCnes": {"name": "", "converter": ""},
"ofertaRespiradores": {"name": "", "converter": ""},
"ofertaHospCli": {"name": "", "converter": ""},
"ofertaHospUti": {"name": "", "converter": ""},
"ofertaSRAGCli": {"name": "", "converter": ""},
"ofertaSRAGUti": {"name": "", "converter": ""},
}
def convert_row(row):
return {key: row.get(key, None) for key in FIELD_CONVERTERS.keys()}
# TODO: implement
# TODO: check municipio (correct name)
# return {
# "uf": row["estadoSigla"].upper(),
# "municipio": row["municipio"],
# "codigo_cnes": row["cnes"],
# "nome": row["nomeCnes"],
# 'dataNotificacaoOcupacao': '2020-08-11T03:00:07.102Z',
# 'ofertaRespiradores': 0,
# 'ofertaHospCli': 86,
# 'ofertaHospUti': 0,
# 'ofertaSRAGCli': 12,
# 'ofertaSRAGUti': 0,
# 'ocupHospCli': 22,
# 'ocupHospUti': 3,
# 'ocupSRAGCli': 2,
# 'ocupSRAGUti': 3,
# 'altas': 0,
# 'obitos': 1,
# 'ocupacaoInformada': True,
# 'algumaOcupacaoInformada': True
# }
return row
def main():
dt = datetime.datetime.today().strftime("%Y-%m-%dT%H:%M:%S")
parser = argparse.ArgumentParser()
parser.add_argument("--username", default="user-api-leitos")
parser.add_argument("--password", default="aQbLL3ZStaTr38tj")
parser.add_argument("--api-url", default="https://elastic-leitos.saude.gov.br/")
parser.add_argument("--index", default="leito_ocupacao")
parser.add_argument("--ttl", default="10m")
parser.add_argument(
"--output-filename", default=DOWNLOAD_PATH / f"ocupacao-{dt}.csv"
)
args = parser.parse_args()
es = ElasticSearch(
args.api_url,
username=args.username,
password=args.password,
)
iterator = es.search(
index=args.index,
sort_by="dataNotificacaoOcupacao",
ttl=args.ttl,
)
writer = CsvLazyDictWriter(args.output_filename)
for row in tqdm(iterator, unit_scale=True):
writer.writerow(convert_row(row))
writer.close()
if __name__ == "__main__":
main()