-
Notifications
You must be signed in to change notification settings - Fork 0
/
jobs.py
462 lines (346 loc) · 15.6 KB
/
jobs.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
from datetime import datetime, date, timedelta
from telegram import ParseMode
from modules.utils import load_config, get_messagetext
from modules.utils import file_open_json, file_write_json
import modules.rss as rss
import modules.graphs as graphs
import modules.c19api as c19api
cfg = load_config()
bot = cfg["bot"]
jobs = bot["autopost"]["jobs"]
def stats(context):
yesterday = (date.today() - timedelta(days=1)).strftime("%d.%m.%Y")
# metadata
population = 5391369
tested = c19api.metadata("tested")
confirmed = c19api.metadata("confirmed")
dead = c19api.metadata("dead")
admissions = c19api.metadata("admissions")
icu = c19api.metadata("icu")
respiratory = c19api.metadata("respiratory")
vaccine_doses = c19api.timeseries("vaccine_doses")
# totals
tested_total = tested.get("total")
confirmed_total = confirmed.get("total")
dead_total = dead.get("total")
admissions_total = admissions.get("total")
icu_total = icu.get("total")
respiratory_total = respiratory.get("total")
# newYesterday
tested_newYesterday = tested.get("newYesterday", 0)
confirmed_newYesterday = confirmed.get("newYesterday", 0)
dead_newYesterday = dead.get("newYesterday", 0)
# newSince
confirmed_newSince_d7 = confirmed.get("newSince_d8", 0)
confirmed_newSince_d14 = confirmed.get("newSince_d15", 0)
# percentages
dead_pct = round(dead_total / confirmed_total * 100, 1)
respiratory_pct = round(respiratory_total / admissions_total * 100, 1)
icu_pct = round(icu_total / admissions_total * 100, 1)
# vaccine data
vaccine_data = list(
filter(lambda x: x["granularity_geo"] == "nation", vaccine_doses)
)[-1]
vacc_total_dose_1 = vaccine_data.get("total_dose_1")
vacc_total_dose_2 = vaccine_data.get("total_dose_2")
vacc_total_dose_3 = vaccine_data.get("total_dose_3")
vacc_total_dose_1_pct = vacc_total_dose_1 / population
vacc_total_dose_2_pct = vacc_total_dose_2 / population
vacc_total_dose_3_pct = vacc_total_dose_3 / population
ret_str = f"🔢 <b>Nøkkeltall - {yesterday}</b>"
ret_str += f"\n\n🦠 Smittetilfeller siste døgn: <b>{confirmed_newYesterday:,}</b>"
ret_str += f"\nSiste 7d: <b>{confirmed_newSince_d7:,}</b>"
ret_str += f"\nSiste 14d: <b>{confirmed_newSince_d14:,}</b>"
ret_str += f"\nTotalt: <b>{confirmed_total:,}</b>"
ret_str += f"\n\n❗ Dødsfall siste døgn: <b>{dead_newYesterday:,}</b>"
ret_str += f"\nTotalt: <b>{dead_total:,}</b> ({dead_pct}% av smittede)"
ret_str += f"\n\n🔬 Testede siste døgn: <b>{tested_newYesterday:,}</b>"
ret_str += f"\nTotalt: <b>{tested_total:,}</b>"
# https://www.helsedirektoratet.no/statistikk/antall-innlagte-pasienter-pa-sykehus-med-pavist-covid-19
# Fra 23. mars 2022 ble rapporteringen avsluttet og visningen blir derfor ikke lenger oppdatert etter denne datoen.
#
# ret_str += f"\n\n🏥 Innlagt på sykehus: <b>{admissions_total:,}</b>"
# ret_str += f"\n🤒 Innlagt på intensivavdeling: <b>{icu_total:,}</b> ({icu_pct}% av innlagte)"
# ret_str += f"\n😷 Tilkoblet respirator: <b>{respiratory_total:,}</b> ({respiratory_pct}% av innlagte)"
ret_str += "\n\n💉 Andel av befolkningen vaksinert"
ret_str += f"\nDose 1: <b>{vacc_total_dose_1_pct:,.02%}</b> (<b>{vacc_total_dose_1:,}</b> personer)"
ret_str += f"\nDose 2: <b>{vacc_total_dose_2_pct:,.02%}</b> (<b>{vacc_total_dose_2:,}</b> personer)"
ret_str += f"\nDose 3: <b>{vacc_total_dose_3_pct:,.02%}</b> (<b>{vacc_total_dose_3:,}</b> personer)"
ret_str = ret_str.replace(",", " ")
context.bot.send_message(
chat_id=bot["autopost"]["chatid"], text=ret_str, parse_mode=ParseMode.HTML
)
def tested(context):
source_name = jobs["tested"]["source"]["name"]
source_url = jobs["tested"]["source"]["url"]
curr_data = c19api.timeseries("tested")[-1]
curr_total = curr_data.get("total")
last_data = file_open_json("tested")
last_total = last_data.get("total")
diff_tested = curr_total - last_total
if diff_tested > 0:
messagetext = get_messagetext("tested", diff_tested)
curr_new_today = curr_data.get("new")
ret_str = f"🔬 <b>{diff_tested:,}</b> {messagetext}"
ret_str += (
f"\nTotalt: <b>{last_total:,}</b> (Nye i dag: <b>{curr_new_today:,}</b>)"
)
ret_str += f"\n\nKilde: <a href='{source_url}'>{source_name}</a>"
file_write_json("tested", curr_data)
ret_str = ret_str.replace(",", " ")
print(ret_str, "\n")
context.bot.send_message(
chat_id=bot["autopost"]["chatid"],
text=ret_str,
parse_mode=ParseMode.HTML,
disable_web_page_preview=True,
)
else:
return None
def tested_lab(context):
source_name = jobs["tested_lab"]["source"]["name"]
source_url = jobs["tested_lab"]["source"]["url"]
curr_data = c19api.timeseries("tested_lab")[-1]
curr_total = curr_data.get("total")
last_data = file_open_json("tested_lab")
last_total = last_data.get("total")
if curr_total - last_total > 0:
ret_str = "🔺 <b>Andel positive tester</b>"
ret_str += "\nProsentandelen positive tester i Norge siden epidemiens start."
ret_str += f"\n\nKilde: <a href='{source_url}'>{source_name}</a>"
file_write_json("tested_lab", curr_data)
print(ret_str, "\n")
context.bot.send_photo(
bot["autopost"]["chatid"],
graphs.tested(),
parse_mode=ParseMode.HTML,
caption=ret_str,
)
else:
return None
def confirmed(context):
source_name = jobs["confirmed"]["source"]["name"]
source_url = jobs["confirmed"]["source"]["url"]
curr_data = c19api.timeseries("confirmed")[-1]
curr_total = curr_data.get("total")
last_data = file_open_json("confirmed")
last_total = last_data.get("total")
diff_confirmed = curr_total - last_total
if diff_confirmed > 0:
messagetext = get_messagetext("confirmed", diff_confirmed)
ret_str = f"🦠 <b>{diff_confirmed}</b> {messagetext}"
if datetime.now().hour in range(0, 2):
yesterday_data = c19api.timeseries("confirmed")[-2]
yesterday_new = yesterday_data.get("new")
ret_str += f"\nTotalt: <b>{last_total:,}</b> (Nye siste døgn: <b>{yesterday_new:,}</b>)"
else:
newToday = curr_data.get("new")
ret_str += (
f"\nTotalt: <b>{curr_total:,}</b> (Nye i dag: <b>{newToday:,}</b>)"
)
ret_str += f"\n\nKilde: <a href='{source_url}'>{source_name}</a>"
file_write_json("confirmed", curr_data)
ret_str = ret_str.replace(",", " ")
print(ret_str, "\n")
context.bot.send_message(
chat_id=bot["autopost"]["chatid"],
text=ret_str,
parse_mode=ParseMode.HTML,
disable_web_page_preview=True,
)
else:
return None
def confirmed_by_testdate(context):
source_name = jobs["confirmed_by_testdate"]["source"]["name"]
source_url = jobs["confirmed_by_testdate"]["source"]["url"]
data = c19api.timeseries("confirmed")
curr_data = list(filter(lambda x: x["source"] == "fhi:git", data))[-1]
curr_total = curr_data.get("total")
last_data = file_open_json("confirmed_by_testdate")
last_total = last_data.get("total")
if curr_total - last_total > 0:
ret_str = "🦠 <b>Antall meldte smittetilfeller</b>"
ret_str += "\nAntall meldte COVID-19 tilfeller etter prøvetakingsdato."
ret_str += "\nDet er 1-2 dagers forsinkelse i tiden fra diagnose til registrering i MSIS."
ret_str += f"\n\nKilde: <a href='{source_url}'>{source_name}</a>"
file_write_json("confirmed_by_testdate", curr_data)
print(ret_str, "\n")
context.bot.send_photo(
bot["autopost"]["chatid"],
graphs.confirmed(),
parse_mode=ParseMode.HTML,
caption=ret_str,
)
else:
return None
def dead(context):
source_name = jobs["dead"]["source"]["name"]
source_url = jobs["dead"]["source"]["url"]
curr_data = c19api.timeseries("dead")[-1]
curr_total = curr_data.get("total")
last_data = file_open_json("dead")
last_total = last_data.get("total")
diff_dead = curr_total - last_total
if diff_dead > 0:
messagetext = get_messagetext("dead", diff_dead)
curr_new_today = curr_data.get("new")
ret_str = "❗ <b>COVID-19 assosierte dødsfall</b>"
ret_str += f"\n<b>{diff_dead}</b> {messagetext}"
ret_str += (
f"\nTotalt: <b>{curr_total:,}</b> (Nye i dag: <b>{curr_new_today:,}</b>)"
)
ret_str += f"\n\nKilde: <a href='{source_url}'>{source_name}</a>"
file_write_json("dead", curr_data)
ret_str = ret_str.replace(",", " ")
print(ret_str, "\n")
context.bot.send_photo(
bot["autopost"]["chatid"],
graphs.dead(),
parse_mode=ParseMode.HTML,
caption=ret_str,
)
else:
return None
def hospitalized(context):
source_name = jobs["hospitalized"]["source"]["name"]
source_url = jobs["hospitalized"]["source"]["url"]
curr_data = c19api.timeseries("hospitalized")[-1]
curr_respiratory = int(curr_data.get("respiratory"))
curr_admissions = int(curr_data.get("admissions"))
curr_icu = int(curr_data.get("icu"))
last_data = file_open_json("hospitalized")
last_respiratory = int(last_data.get("respiratory"))
last_admissions = int(last_data.get("admissions"))
last_icu = int(last_data.get("icu"))
if (
curr_admissions != last_admissions
or curr_respiratory != last_respiratory
or curr_icu != last_icu
):
diff_admissions = curr_admissions - last_admissions
diff_respiratory = curr_respiratory - last_respiratory
diff_icu = curr_icu - last_icu
respiratory_pct = curr_respiratory / curr_admissions
icu_pct = curr_icu / curr_admissions
ret_str = "🏥 <b>Innlagte pasienter på sykehus</b>"
if diff_admissions != 0:
ret_str += f"\nEndring i antall innlagte: <b>{diff_admissions:+,}</b>"
if diff_icu != 0:
ret_str += f"\nEndring i antall på intensivavdeling: <b>{diff_icu:+,}</b>"
if diff_respiratory != 0:
ret_str += f"\nEndring i antall på respirator: <b>{diff_respiratory:+,}</b>"
ret_str += f"\n\n<b>{curr_admissions:,}</b> personer er innlagt på sykehus"
ret_str += f"\n<b>{curr_icu:,}</b> personer er innlagt på intensivavdeling ({icu_pct:.01%})"
ret_str += f"\n<b>{curr_respiratory:,}</b> personer er på respirator ({respiratory_pct:.01%})"
ret_str += f"\n\nKilde: <a href='{source_url}'>{source_name}</a>"
ret_str = ret_str.replace(",", " ")
print(ret_str, "\n")
file_write_json("hospitalized", curr_data)
context.bot.send_photo(
bot["autopost"]["chatid"],
graphs.hospitalized(),
parse_mode=ParseMode.HTML,
caption=ret_str,
)
else:
return None
def vaccine(context):
population = 5391369
source_name = jobs["vaccine"]["source"]["name"]
source_url = jobs["vaccine"]["source"]["url"]
data = c19api.timeseries("vaccine_doses")
curr_data = list(filter(lambda x: x["granularity_geo"] == "nation", data))[-1]
curr_total_doses = curr_data.get("total_doses")
last_data = file_open_json("vaccine_doses")
last_total_doses = last_data.get("total_doses")
diff_total_doses = curr_total_doses - last_total_doses
if diff_total_doses > 0:
curr_total_dose_1 = curr_data.get("total_dose_1")
curr_total_dose_2 = curr_data.get("total_dose_2")
curr_total_dose_3 = curr_data.get("total_dose_3")
curr_total_dose_1_pct = curr_total_dose_1 / population
curr_total_dose_2_pct = curr_total_dose_2 / population
curr_total_dose_3_pct = curr_total_dose_3 / population
last_total_dose_1 = last_data.get("total_dose_1")
last_total_dose_2 = last_data.get("total_dose_2")
last_total_dose_3 = last_data.get("total_dose_3")
diff_total_dose_1 = curr_total_dose_1 - last_total_dose_1
diff_total_dose_2 = curr_total_dose_2 - last_total_dose_2
diff_total_dose_3 = curr_total_dose_3 - last_total_dose_3
ret_str = "💉 <b>Antall vaksinerte</b>"
if diff_total_dose_1 != 0:
ret_str += (
f"\n<b>{diff_total_dose_1:,}</b> nye personer vaksinert med 1. dose"
)
if diff_total_dose_2 != 0:
ret_str += f"\n<b>{diff_total_dose_2:,}</b> nye personer fullvaksinert"
if diff_total_dose_3 != 0:
ret_str += (
f"\n<b>{diff_total_dose_3:,}</b> nye personer vaksinert med 3. dose"
)
ret_str += "\n\n<b>Total andel av befolkningen</b>"
ret_str += f"\nDose 1: <b>{curr_total_dose_1_pct:,.02%}</b> ({curr_total_dose_1:,} personer)"
ret_str += f"\nDose 2: <b>{curr_total_dose_2_pct:,.02%}</b> ({curr_total_dose_2:,} personer)"
ret_str += f"\nDose 3: <b>{curr_total_dose_3_pct:,.02%}</b> ({curr_total_dose_3:,} personer)"
ret_str += f"\n\nKilde: <a href='{source_url}'>{source_name}</a>"
file_write_json("vaccine_doses", curr_data)
ret_str = ret_str.replace(",", " ")
print(ret_str, "\n")
context.bot.send_photo(
bot["autopost"]["chatid"],
graphs.vaccine_doses(),
parse_mode=ParseMode.HTML,
caption=ret_str,
)
else:
return None
def smittestopp(context):
source_name = jobs["smittestopp"]["source"]["name"]
source_url = jobs["smittestopp"]["source"]["url"]
curr_data = c19api.timeseries("smittestopp")[-1]
curr_total_downloads = int(curr_data.get("total_downloads"))
curr_total_reported = int(curr_data.get("total_reported"))
last_data = file_open_json("smittestopp")
last_total_downloads = int(last_data.get("total_downloads"))
last_total_reported = int(last_data.get("total_reported"))
if (
curr_total_downloads != last_total_downloads
or curr_total_reported != last_total_reported
):
new_downloads = int(curr_data.get("new_downloads"))
new_reported = int(curr_data.get("new_reported"))
if new_downloads == 1:
new_downloads_text = "ny nedlasting"
else:
new_downloads_text = "nye nedlastinger"
if new_reported == 1:
new_reported_text = "ny person meldt smittet i appen"
else:
new_reported_text = "nye personer meldt smittet i appen"
ret_str = "📱 <b>Smittestopp</b>"
if new_downloads != 0:
ret_str += f"\n<b>{new_downloads:,}</b> {new_downloads_text}"
if new_reported != 0:
ret_str += f"\n<b>{new_reported:,}</b> {new_reported_text}"
ret_str += f"\n\nTotalt antall nedlastinger: <b>{curr_total_downloads:,}</b>"
ret_str += (
f"\nTotalt meldt smittet i appen: <b>{curr_total_reported:,}</b> personer"
)
ret_str += f"\n\nKilde: <a href='{source_url}'>{source_name}</a>"
file_write_json("smittestopp", curr_data)
ret_str = ret_str.replace(",", " ")
print(ret_str, "\n")
context.bot.send_photo(
bot["autopost"]["chatid"],
graphs.smittestopp(),
parse_mode=ParseMode.HTML,
caption=ret_str,
)
else:
return None
def rss_feed(context):
res = rss.fetch_feed()
if res is not None:
context.bot.send_message(
chat_id=bot["autopost"]["chatid"], text=res, parse_mode=ParseMode.HTML
)