This repository has been archived by the owner on Apr 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
228 lines (200 loc) · 7.26 KB
/
main.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
# coding=utf-8
import requests
import json
import os
import sys
import re
import datetime
import csv
slack_url = os.getenv('SLACK_URL')
if not slack_url:
print('Provide a hook to your Slack workspace in the `SLACK_URL` environment property.\n(e.g. https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX)')
sys.exit('Slack hook URL is necessary to post the overview into the workspace.')
approximate_citizen_count = 10_701_777
mzcr_url = 'https://onemocneni-aktualne.mzcr.cz/covid-19'
overview_url = 'https://onemocneni-aktualne.mzcr.cz/api/v2/covid-19/zakladni-prehled.csv'
overview_csv = next(csv.DictReader(requests.get(overview_url).content.decode('utf-8-sig').splitlines()))
tests_antigen = overview_csv['provedene_antigenni_testy_celkem']
tests_pcr = int(overview_csv['provedene_testy_celkem']) - int(overview_csv['provedene_antigenni_testy_celkem'])
total_vaccinations = int(overview_csv['vykazana_ockovani_celkem'])
total_vaccinations_percentage = '{:.1%}'.format(total_vaccinations / approximate_citizen_count)
# We're rewriting that file. If it's not ours, it has no business being here.
dirname = os.path.dirname(__file__)
previous_data_path = os.path.join(dirname, 'previous.json')
if not os.path.isfile(previous_data_path):
open(previous_data_path, 'w').close()
with open(previous_data_path, 'r+') as f:
previous_data = f.read()
current_data_points = [
('tests_pcr', tests_pcr),
('tests_antigen', tests_antigen),
('total_infections', overview_csv['potvrzene_pripady_celkem']),
('active_infections', overview_csv['aktivni_pripady']),
('recoveries', overview_csv['vyleceni']),
('hospitalized', overview_csv['aktualne_hospitalizovani']),
('deceased', overview_csv['umrti']),
('total_vaccinations', total_vaccinations),
]
current_data = {key: int(value) for key, value in current_data_points}
f.seek(0)
f.write(json.dumps(current_data, indent=2))
f.truncate()
if previous_data:
previous_data = json.loads(previous_data)
else:
previous_data = {}
def format_number(number):
return f'{number:,}'.replace(',', ' ').strip()
def format_comparison(old_value, new_value):
if old_value is not None:
comparison = new_value - old_value
if comparison == 0:
return ' (unchanged)'
return ' ({}{})'.format(
'+' if comparison > 0 else '',
format_number(comparison)
)
else:
return ''
previous_total_tests = None
previous_pcr_tests = previous_data.get('tests_pcr')
if previous_pcr_tests is not None:
previous_total_tests = (previous_total_tests or 0) + previous_pcr_tests
previous_antigen_tests = previous_data.get('tests_antigen')
if previous_antigen_tests is not None:
previous_total_tests = (previous_total_tests or 0) + previous_antigen_tests
class Slack:
def section(text):
return {
"type": "section",
"text": {
"type": "mrkdwn",
"text": text
}
}
def context(text):
return {
"type": "context",
"elements": [
{
"type": "mrkdwn",
"text": text
}
]
}
def header(text):
return {
"type": "header",
"text": {
"type": "plain_text",
"text": text,
"emoji": True
}
}
def spacer():
return {
"type": "section",
"text": {
"type": "plain_text",
"text": " ",
"emoji": False
}
}
def divider():
return { "type": "divider" }
def overflow(text, elements):
return {
"type": "section",
"text": {
"type": "mrkdwn",
"text": text
},
"accessory": {
"type": "overflow",
"options": [
map(overflow_item, elements)
]
}
}
def overflow_item(text):
return {
"text": {
"type": "plain_text",
"text": text,
"emoji": True
}
}
payload = {
"blocks": [
Slack.header("Situace – {}".format(datetime.datetime.now().strftime("%d/%m/%Y"))),
Slack.section(
"[Tests] :female-doctor: :male-doctor:" +
"\n\t PCR: *{}*{}".format(
format_number(current_data['tests_pcr']),
format_comparison(previous_data.get('tests_pcr'), current_data['tests_pcr']),
) +
"\n\t Antigen: *{}*{}".format(
format_number(current_data['tests_antigen']),
format_comparison(previous_data.get('tests_antigen'), current_data['tests_antigen']),
) +
"\n\t Total: *{}*{}".format(
format_number(current_data['tests_pcr'] + current_data['tests_antigen']),
format_comparison(previous_total_tests, current_data['tests_pcr'] + current_data['tests_antigen']),
)
),
Slack.divider(),
Slack.section(
"[Infections] :zombie: :female_zombie:" +
"\n\t Active: *{}*{}".format(
format_number(current_data['active_infections']),
format_comparison(previous_data.get('active_infections'), current_data['active_infections']),
) +
"\n\t Recovered: *{}*{}".format(
format_number(current_data['recoveries']),
format_comparison(previous_data.get('recoveries'), current_data['recoveries']),
) +
"\n\t Total: *{}*{}".format(
format_number(current_data['total_infections']),
format_comparison(previous_data.get('total_infections'), current_data['total_infections']),
)
),
Slack.divider(),
Slack.section(
"[Losses] :hospital: :f:" +
"\n\t Hospitalized: *{}*{}".format(
format_number(current_data['hospitalized']),
format_comparison(previous_data.get('hospitalized'), current_data['hospitalized']),
) +
"\n\t Deceased: *{}*{}".format(
format_number(current_data['deceased']),
format_comparison(previous_data.get('deceased'), current_data['deceased']),
)
),
Slack.divider(),
Slack.section(
"[Vaccinations] :syringe: :pill:" +
"\n\t Any dose: *{}*{}".format(
format_number(current_data['total_vaccinations']),
format_comparison(previous_data.get('total_vaccinations'), current_data['total_vaccinations']),
)
),
Slack.divider(),
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*For more info click this button:*"
},
"accessory": {
"type": "button",
"text": {
"type": "plain_text",
"text": "MZČR",
"emoji": True
},
"url": mzcr_url
}
}
]
}
requests.post(slack_url, json=payload)