This repository was archived by the owner on Jan 12, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdatehtml.py
122 lines (111 loc) · 4.33 KB
/
updatehtml.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
import datetime
import requests
import tempfile
import urllib.parse
import openpyxl
filtered = True
filter = [9777,9190,9762, 9763,9780]
def getrkixlsx(url, path):
file = '%s/inzidenzen.xlsx' %(path)
r = requests.get(url, allow_redirects=True)
open(file, 'wb').write(r.content)
return file
def getdata(file):
data = {}
xlsx = openpyxl.load_workbook(file, read_only=True)
if 'LK_7-Tage-Fallzahlen (fixiert)' in xlsx:
sheet = xlsx['LK_7-Tage-Inzidenz (fixiert)']
elif 'LK_7-Tage-Inzidenz' in xlsx:
sheet = xlsx['LK_7-Tage-Inzidenz']
if sheet[5][-1].value == None:
negCol = -2
while sheet[5][negCol].value == None:
negCol -= 1
negCol += 1
else:
negCol = None
date = sheet[5][((0 if negCol == None else negCol)-14):negCol]
data['date'] = [x.value for x in date][::-1]
for row in sheet.iter_rows(min_row=6, values_only=True):
if not row[1] == None and (not filtered or row[2] in filter + ['LKNR']):
index = row[1]
data[index] = (row[((0 if negCol == None else negCol)-14):negCol])[::-1]
xlsx.close()
return data
def generatehtml(data, path):
html = '%s/index.html' %(path)
with open(html, 'a') as f:
f.write('\n'.join([
'<!DOCTYPE html>',
'<html lang="de">',
' <head>',
' <title>RKI Inzidenz Historie</title>',
' <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5">',
' <style>',
' html,body {',
' background: #1D1F21;',
' color: #C5C8C6;',
' font-family: sans-serif;',
' text-align: center;',
' }',
' a {',
' text-decoration: none;',
' color: #81A2BE;',
' }',
' table {',
' margin: 1em auto;',
' }',
' .purple {',
' color: #B294BB;',
' }',
' .pink {',
' color: #D75F95;',
' }',
' .red {',
' color: #CC6666;',
' }',
' .orange {',
' color: #F0C674;',
' }',
' .green {',
' color: #b5db68;',
' }',
' </style>',
' </head>',
' <body>',
' <h1>RKI Inzidenz Historie</h1>',
' <p>RKI Stand: %s' %(data['date'][0].strftime("%d.%m.%Y")), '<br>'
' Letztes Update: %s</p>' %(datetime.datetime.now()), ''
]))
for lk in data:
if lk == 'date': continue
id = urllib.parse.quote_plus(' '.join(lk.split(' ')[1:]))
f.write(' <h2 id="%s"><a href="#%s">%s</a></h2>\n' %(id, id, ' '.join(lk.split(' ')[1:])))
f.write(' <table>\n')
for i in range(len(data[lk])):
if data[lk][i] <= 50:
color = "green"
elif data[lk][i] <= 100:
color = "orange"
elif data[lk][i] <= 500:
color = "red"
elif data[lk][i] <= 1000:
color = "pink"
else:
color = "purple"
f.write(' <tr>\n')
f.write(' <th>%s</th>\n' %(data['date'][i].strftime("%d.%m.")))
f.write(' <th class="%s">%1.2f</th>\n' %(color, data[lk][i]))
f.write(' </tr>\n')
f.write(' </table>\n')
f.write('\n'.join([
' </body>',
'</html>'
]))
return html
if __name__ == "__main__":
with tempfile.TemporaryDirectory() as tempdir:
file = getrkixlsx("https://www.rki.de/DE/Content/InfAZ/N/Neuartiges_Coronavirus/Daten/Fallzahlen_Kum_Tab.xlsx?__blob=publicationFile", tempdir)
data = getdata(file)
html = generatehtml(data, tempdir)
open('index.html', 'w').write((open(html).read()))