This repository has been archived by the owner on Jul 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cases.go
271 lines (213 loc) · 6.21 KB
/
cases.go
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
package nzcovid19cases
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"github.com/anaskhan96/soup"
)
type RawCase struct {
ReportedDate string
Case int
Location string
Age string
Gender string
TravelRelated string
LastCityBeforeNZ string
FlightNumber string
DepartureDate string
ArrivalDate string
CaseType string
}
type CaseStatsResponse struct {
ConfirmedCasesTotal int
ConfirmedCasesNew24h int
ProbableCasesTotal int
ProbableCasesNew24h int
RecoveredCasesTotal int
RecoveredCasesNew24h int
//HospitalisedCasesTotal int
//HospitalisedCasesNew24h int
DeathCasesTotal int
DeathCasesNew24h int
}
func parseRow(cols []soup.Root) RawCase {
var c RawCase
c.ReportedDate = cols[0].Text()
c.Gender = cols[1].Text()
c.Age = cols[2].Text()
c.Location = cols[3].Text()
// Handle case with erroneous colspan
_, sigh := cols[3].Attrs()["colspan"]
if sigh {
return c
}
c.TravelRelated = cols[4].Text()
c.LastCityBeforeNZ = cols[5].Text()
c.FlightNumber = cols[6].Text()
c.ArrivalDate = cols[7].Text()
c.DepartureDate = cols[8].Text()
return c
}
const (
CSVRenderType = "csv"
JSONRenderType = "json"
)
func parseStat(stat soup.Root) (int, int, error) {
tds := stat.FindAll("td")
if len(tds) != 2 { //nolint:gomnd
return 0, 0, fmt.Errorf("expected two columns")
}
numString := strings.ReplaceAll(tds[0].Text(), ",", "")
num, err := strconv.Atoi(numString)
if err != nil {
return 0, 0, fmt.Errorf("failed to convert %v to number: %w", tds[1].Text(), err)
}
if strings.TrimSpace(tds[1].Text()) == "" {
return num, 0, nil
}
num24hString := strings.ReplaceAll(tds[1].Text(), ",", "")
num24h, err := strconv.Atoi(num24hString)
if err != nil {
return 0, 0, fmt.Errorf("failed to convert %v to number: %w", tds[1].Text(), err)
}
return num, num24h, nil
}
func ScrapeCases() ([]*RawCase, error) {
resp, err := soup.Get("https://www.health.govt.nz/our-work/diseases-and-conditions/" +
"covid-19-novel-coronavirus/covid-19-current-cases/covid-19-current-cases-details")
if err != nil {
return nil, err
}
doc := soup.HTMLParse(resp)
tables := doc.FindAll("table", "class", "table-style-two")
var offset = 0
if len(tables) > 2 { //nolint:gomnd
offset = 1
}
rows := tables[offset].FindAll("tr")
var cases []*RawCase //nolint:prealloc
// Note: slice starting at 1, skipping the header
for i, row := range rows[1:] {
cols := row.FindAll("td")
if len(cols) < 8 { //nolint:gomnd
return nil, fmt.Errorf("table 1 row has %v columns, not at least 8", len(cols))
}
c := parseRow(cols)
c.CaseType = "confirmed"
c.Case = len(rows) - i - 1
cases = append(cases, &c)
}
rows = tables[offset+1].FindAll("tr")
// Note: slice starting at 1, skipping the header
for i, row := range rows[1:] {
cols := row.FindAll("td")
if len(cols) != 9 { //nolint:gomnd
return nil, fmt.Errorf("table 1 row has %v columns, not 9", len(cols))
}
c := parseRow(cols)
c.CaseType = "probable"
c.Case = len(rows) - i - 1
cases = append(cases, &c)
}
return cases, nil
}
func RenderCases(normCases []*NormalisedCase, viewType string) (string, error) {
validViewTypes := map[string]bool{
CSVRenderType: true,
JSONRenderType: true,
}
if !validViewTypes[viewType] {
return "", InvalidUsageError{fmt.Sprintf("unknown view type: %v", viewType)}
}
var sb strings.Builder
switch viewType {
case CSVRenderType:
sb.WriteString(
`"CaseNumber",` +
`"ReportedDate",` +
`"LocationName",` +
`"AgeValid",` +
`"OlderOrEqualToAge",` +
`"YoungerThanAge",` +
`"Gender",` +
`"IsTravelRelated",` +
`"DepartureDateValid",` +
`"DepartureDate",` +
`"ArrivalDateValid",` +
`"ArrivalDate",` +
`"LastCityBeforeNZ",` +
`"FlightNumber",` +
`"CaseType"`,
)
sb.WriteRune('\n')
for _, c := range normCases {
sb.WriteString(fmt.Sprintf(
`%v, "%v", "%v", "%v", "%v", "%v", %v, %v, "%v", "%v", "%v", "%v", "%v", "%v", "%v", "%v"`,
c.CaseNumber, c.ReportedDate, c.LocationName, c.Age.Valid, c.Age.OlderOrEqualToAge,
c.Age.YoungerThanAge, c.Gender, c.IsTravelRelated.Valid, c.IsTravelRelated.Value,
c.DepartureDate.Valid, c.DepartureDate.Value, c.ArrivalDate.Valid, c.ArrivalDate.Value,
c.LastCityBeforeNZ, c.FlightNumber, c.CaseType),
)
sb.WriteRune('\n')
}
case JSONRenderType:
b, err := json.MarshalIndent(normCases, "", " ")
if err != nil {
return "", err
}
sb.Write(b)
}
return sb.String(), nil
}
func ScrapeCaseStats() (CaseStatsResponse, error) {
var cS CaseStatsResponse
resp, err := soup.Get("https://www.health.govt.nz/our-work/diseases-and-conditions/" +
"covid-19-novel-coronavirus/covid-19-current-cases")
if err != nil {
return cS, err
}
doc := soup.HTMLParse(resp)
tables := doc.FindAll("table")
stats := tables[0].FindAll("tr")
if len(stats) != 8 { //nolint:gomnd
return cS, fmt.Errorf("stats table has %v TR, not 8", len(stats))
}
//for i, s := range stats {
// a, b ,_ := parseStat(s)
// fmt.Println(i,a,b)
//}
cS.ConfirmedCasesTotal, cS.ConfirmedCasesNew24h, err = parseStat(stats[1])
if err != nil {
return cS, fmt.Errorf("problem parsing confirmed cases %w", err)
}
cS.ProbableCasesTotal, cS.ProbableCasesNew24h, err = parseStat(stats[2])
if err != nil {
return cS, fmt.Errorf("problem parsing probable cases %w", err)
}
//cS.HospitalisedCasesTotal, cS.HospitalisedCasesNew24h, err = parseStat(stats[4]) //parseStatHosp(stats[4])
//if err != nil {
// return cS, fmt.Errorf("problem parsing hospitalised cases %w", err)
//}
cS.RecoveredCasesTotal, cS.RecoveredCasesNew24h, err = parseStat(stats[4])
if err != nil {
return cS, fmt.Errorf("problem parsing recovered cases %w", err)
}
cS.DeathCasesTotal, cS.DeathCasesNew24h, err = parseStat(stats[6])
if err != nil {
return cS, fmt.Errorf("problem parsing dead cases %w", err)
}
return cS, nil
}
func RenderCaseStats(cS CaseStatsResponse, viewType string) (string, error) {
var sb strings.Builder
if viewType != JSONRenderType {
return "", InvalidUsageError{fmt.Sprintf("unknown view type: %v", viewType)}
}
b, err := json.MarshalIndent(cS, "", " ")
if err != nil {
return "", err
}
sb.Write(b)
return sb.String(), nil
}