-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcovid19AT.php
156 lines (134 loc) · 7.35 KB
/
covid19AT.php
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
<?php
/**
* covid19AT
*
* Gets the COVID-19 stats from the austrian goverment and formats it machine readable.
*
* @copyright Copyright (c) 2020 Matthias Schaffer (https://matthiasschaffer.com)
* @license https://spdx.org/licenses/MIT.html MIT License
* @link https://github.com/fellwell5/covid19AT
*/
$states_array = [
"b" => ["name" => "Burgenland", "name_en" => "Burgenland", "population" => 294466],
"k" => ["name" => "Kärnten", "name_en" => "Carinthia", "population" => 561390],
"n" => ["name" => "Niederösterreich", "name_en" => "Lower Austria", "population" => 1684623],
"o" => ["name" => "Oberösterreich", "name_en" => "Upper Austria", "population" => 1490392],
"s" => ["name" => "Salzburg", "name_en" => "Salzburg", "population" => 558479],
"st" => ["name" => "Steiermark", "name_en" => "Styria", "population" => 1246576],
"t" => ["name" => "Tirol", "name_en" => "Tyrol", "population" => 757852],
"v" => ["name" => "Vorarlberg", "name_en" => "Vorarlberg", "population" => 397094],
"w" => ["name" => "Wien", "name_en" => "Vienna", "population" => 1911728]
];
$state_mapping = ["Burgenland" => "b", "Kärnten" => "k", "Niederösterreich" => "n", "Oberösterreich" => "o", "Salzburg" => "s",
"Steiermark" => "st", "Tirol" => "t", "Vorarlberg" => "v", "Wien" => "w"];
$total_population = 8904511;
function parse_csv_string($string){
$csv = [];
$lines = explode("\n", $string);
$head = str_getcsv(array_shift($lines), ";");
$columns = count($head);
foreach($lines as $line){
$line = str_getcsv($line, ";");
if($columns > count($line)) break;
$newline = [];
foreach($line as $id => $col){
$newline[$head[$id]] = $col;
}
$csv[] = $newline;
}
return $csv;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, "https://info.gesundheitsministerium.at/data/AllgemeinDaten.csv");
$csv = curl_exec($ch);
$allgemeine_daten = parse_csv_string($csv)[0];
$array["timestamp"] = strtotime($allgemeine_daten["Timestamp"]);
$array["date"] = strtotime($allgemeine_daten["Timestamp"]);
$array["time"] = strtotime($allgemeine_daten["Timestamp"]);
$array["total"]["tested_persons"] = intval($allgemeine_daten["GesTestungen"]);
$array["total"]["infected"] = intval($allgemeine_daten["PositivGetestet"]);
$array["total"]["recovered"] = intval($allgemeine_daten["Genesen"]);
$array["total"]["dead"] = intval($allgemeine_daten["TotBestaetigt"]);
$array["total"]["currently_sick"] = intval($allgemeine_daten["AktuelleErkrankungen"]);
$array["total"]["population"] = $total_population;
$array["total"]["tested_persons_percent"] = ($array["total"]["tested_persons"] / $array["total"]["population"]) * 100;
$array["total"]["infected_percent"] = ($array["total"]["infected"] / $array["total"]["population"]) * 100;
$array["total"]["recovered_percent"] = ($array["total"]["recovered"] / $array["total"]["population"]) * 100;
$array["total"]["dead_percent"] = ($array["total"]["dead"] / $array["total"]["population"]) * 100;
$array["total"]["currently_sick_percent"] = ($array["total"]["currently_sick"] / $array["total"]["population"]) * 100;
/* GET Information: currently_sick */
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, "https://info.gesundheitsministerium.at/data/Bundesland.csv");
$csv = curl_exec($ch);
$bundeslaender = parse_csv_string($csv);
$array["details"]["currently_sick"]["information"] = [
"name" => "Zurzeit Erkrankt", "name_en" => "Currently Sick",
"updated_timestamp" => strtotime($bundeslaender[0]["Timestamp"]),
"updated_datetime" => $bundeslaender[0]["Timestamp"]
];
foreach($bundeslaender as $bl){
$bl_short = $state_mapping[ucfirst($bl["Bundesland"])];
$array["details"]["currently_sick"]["states"][$bl_short] = intval($bl["Anzahl"]);
}
/* GET Information: recovered & dead */
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, "https://info.gesundheitsministerium.at/data/GenesenTodesFaelleBL.csv");
$csv = curl_exec($ch);
$bundeslaender = parse_csv_string($csv);
$array["details"]["recovered"]["information"] = [
"name" => "Genesen", "name_en" => "Recovered",
"updated_timestamp" => strtotime($bundeslaender[0]["Timestamp"]),
"updated_datetime" => $bundeslaender[0]["Timestamp"]
];
$array["details"]["dead"]["information"] = [
"name" => "Verstorben", "name_en" => "Dead",
"updated_timestamp" => strtotime($bundeslaender[0]["Timestamp"]),
"updated_datetime" => $bundeslaender[0]["Timestamp"]
];
foreach($bundeslaender as $bl){
$bl_short = $state_mapping[ucfirst($bl["Bundesland"])];
$array["details"]["recovered"]["states"][$bl_short] = intval($bl["Genesen"]);
$array["details"]["dead"]["states"][$bl_short] = intval($bl["Todesfälle"]);
}
/* GET Information: district_infected */
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, "https://info.gesundheitsministerium.at/data/Bezirke.csv");
$csv = curl_exec($ch);
$bezirke = parse_csv_string($csv);
$array["details"]["district_infected"]["information"] = [
"name" => "Infiziert (nach Bezirk)", "name_en" => "Infected (by district)",
"updated_timestamp" => strtotime($bezirke[0]["Timestamp"]),
"updated_datetime" => $bezirke[0]["Timestamp"]
];
foreach($bezirke as $be){
$array["details"]["district_infected"]["districts"][intval($be["GKZ"])] = intval($be["Anzahl"]);
}
foreach($bezirke as $be){
$array["details"]["district_infected"]["districts_name"][intval($be["GKZ"])] = $be["Bezirk"];
}
/* SET State Informations */
$array["details"]["infected"]["information"] = [
"name" => "Infizierte", "name_en" => "Infected",
"updated_timestamp" => strtotime($bundeslaender[0]["Timestamp"]),
"updated_datetime" => $bundeslaender[0]["Timestamp"]
];
foreach($states_array as $key => $state){
$array["states"][$key] = $state;
$array["states"][$key]["infected"] = (isset($array["details"]["recovered"]["states"][$key]) ? $array["details"]["recovered"]["states"][$key] : 0)+
(isset($array["details"]["dead"]["states"][$key]) ? $array["details"]["dead"]["states"][$key] : 0)+
(isset($array["details"]["currently_sick"]["states"][$key]) ? $array["details"]["currently_sick"]["states"][$key] : 0);
$array["details"]["infected"]["states"][$key] = $array["states"][$key]["infected"];
$array["states"][$key]["recovered"] = (isset($array["details"]["recovered"]["states"][$key]) ? $array["details"]["recovered"]["states"][$key] : 0);
$array["states"][$key]["dead"] = (isset($array["details"]["dead"]["states"][$key]) ? $array["details"]["dead"]["states"][$key] : 0);
$array["states"][$key]["currently_sick"] = (isset($array["details"]["currently_sick"]["states"][$key]) ? $array["details"]["currently_sick"]["states"][$key] : 0);
$array["states"][$key]["infected_percent"] = ($array["states"][$key]["infected"] / $array["states"][$key]["population"]) * 100;
$array["states"][$key]["recovered_percent"] = ($array["states"][$key]["recovered"] / $array["states"][$key]["population"]) * 100;
$array["states"][$key]["dead_percent"] = ($array["states"][$key]["dead"] / $array["states"][$key]["population"]) * 100;
$array["states"][$key]["currently_sick_percent"] = ($array["states"][$key]["currently_sick"] / $array["states"][$key]["population"]) * 100;
}
header("Content-Type: application/json");
echo json_encode($array);