/
cli.go
412 lines (372 loc) · 10.9 KB
/
cli.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
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
package main
import (
"bufio"
"encoding/csv"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"regexp"
"strconv"
"strings"
"github.com/PuerkitoBio/goquery"
)
func Init(args []string) {
fmt.Println("[WARNING] Please note that media statements prior to (16-03-2020) may not be parseable due to inconsistent html formatting")
if len(args) == 0 {
log.Fatal("[ERROR] Please pass an argument to the tool {nicd, cov}")
}
parse(args[0])
}
func invalidArgs() {
log.Fatal("[ERROR] Invalid argument\noptions:\n\tnicd - " + NEWSROOM + "\n\tcov - " + ALTERNATE)
}
func parse(opt string) {
fmt.Println("[INFO] Selected " + opt)
prevID := establishContext()
if strings.EqualFold(opt, "nicd") {
results := Crawl(Request(NEWSROOM), "1")
displayOpts(results)
selection := read("Please select an option to crawl 1 - " + strconv.Itoa(len(results)) + "\n")
id, _ := strconv.Atoi(prevID)
selectedId, _ := strconv.Atoi(selection)
out(ParseNICD(results[selectedId-1], id))
} else if strings.EqualFold(opt, "cov") {
results := Crawl(Request(ALTERNATE), "2")
displayOpts(results)
selection := read("Please select an option to crawl 1 - " + strconv.Itoa(len(results)) + "\n")
id, _ := strconv.Atoi(prevID)
selectedId, _ := strconv.Atoi(selection)
out(ParseSACOVID(results[selectedId-1], id))
} else {
invalidArgs()
}
}
func displayOpts(results []Result) {
fmt.Println("[INFO] OPTIONS (select one)\n--------------------------------------------")
for i, v := range results {
fmt.Println(i+1, v.link)
}
}
func out(inst []Instance) {
fmt.Println("[INFO] OUTPUT\n----------------------------------")
for _, i := range inst {
fmt.Println(i.ToCsvRepr())
}
}
func read(prompt string) string {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := scanner.Text()
if line == "exit" {
os.Exit(0)
}
return line
}
if err := scanner.Err(); err != nil {
log.Fatal("fatal error encountered reading std::in")
}
return ""
}
func establishContext() string {
GetCurrent()
instances := ParseCsv("./current.csv")
prevID := instances[len(instances)-1].case_id
return prevID
}
func (i Instance) ToCsvRepr() string {
return i.case_id + "," +
i.date + "," +
i.YYMMDD + "," +
i.country + "," +
i.province + "," +
i.geo_subdivision + "," +
i.age + "," +
i.gender + "," +
i.transmission_type
}
func ParseInstance(dateString string, context string, province string, id int) Instance {
bio := BIOGRAPHICAL.FindAllString(context, -1)
ng := NO_GENDER_FOUND.FindAllString(context, -1)
date := ParseDate(dateString)
if len(bio) > 0 {
age := AGE.FindAllString(bio[0], -1)
idVal := strconv.Itoa(id)
gen := GENDER.FindAllString(bio[0], -1)
return Instance{
case_id: idVal,
date: RemoveNonAlphaNumberic(removeLBR(date.nativeRepr)),
YYMMDD: RemoveNonAlphaNumberic(removeLBR(date.YYYYMMDD)),
country: COUNTRY,
province: province,
geo_subdivision: GEOSUBDIVISION + province,
age: age[0],
gender: gen[0],
transmission_type: TrimTransmissionType(context, true),
}
} else {
age := AGE.FindAllString(ng[0], -1)
idVal := strconv.Itoa(id)
return Instance{
case_id: idVal,
date: RemoveNonAlphaNumberic(removeLBR(date.nativeRepr)),
YYMMDD: RemoveNonAlphaNumberic(removeLBR(date.YYYYMMDD)),
country: COUNTRY,
province: province,
geo_subdivision: GEOSUBDIVISION + province,
age: age[0],
gender: "not specified",
// being extra safe
transmission_type: TrimTransmissionType(context, false),
}
}
}
func ParseDate(date string) Date {
new := strings.Split(strings.Replace(date, " ,", "", -1), " ")
native := new[0] + "-" + MONTHS[new[1]] + "-" + new[2]
yyyymmdd := new[2] + MONTHS[new[1]] + new[0]
return Date{nativeRepr: native, YYYYMMDD: yyyymmdd}
}
func Prompt(prompt string) string {
reader := bufio.NewReader(os.Stdin)
fmt.Print(prompt)
text, _ := reader.ReadString('\n')
return text
}
func TrimTransmissionType(context string, bio bool) string {
if bio {
return strings.Replace(strings.Replace(strings.TrimSpace(strings.Replace(strings.Replace(BIOGRAPHICAL.Split(context, -1)[1], "travelled", "Travelled", -1), "who", "", -1)), ",", "", -1), "had ", "", -1)
}
return strings.Replace(strings.Replace(strings.TrimSpace(strings.Replace(strings.Replace(NO_GENDER_FOUND.Split(context, -1)[1], "travelled", "Travelled", -1), "who", "", -1)), ",", "", -1), "had ", "", -1)
}
/**
Hit the /newsroom page and return a parsable document using {@link goquery}
**/
func Request(url string) *goquery.Document {
doc, err := goquery.NewDocument(url)
if err != nil {
log.Fatal(err)
}
return doc
}
func ParseNICD(r Result, id int) []Instance {
var instances []Instance
internalID := id
doc := Request(r.link)
dateString := ""
doc.Find("li").Each(func(i int, s *goquery.Selection) {
v, e := s.Attr("itemprop")
if e && v == "datePublished" {
dateString = s.Text()
}
})
doc.Find("strong").Each(func(i int, s *goquery.Selection) {
fmt.Println(s.Text())
selections := s.Parent().Next().Find("li")
if selections != nil {
selections.Each(func(i int, is *goquery.Selection) {
cleaned := strings.Replace(RemoveNonAlpha(s.Text()), "Province", "", -1)
if val, ok := PROVINCES[strings.ToUpper(cleaned)]; ok {
internalID++
instances = append(instances, ParseInstance(dateString, is.Text(), val, internalID))
}
})
}
})
return instances
}
func ParseSACOVID(r Result, id int) []Instance {
var instances []Instance
doc := Request(r.link)
internalID := id
dateString := ""
doc.Find("span").Each(func(i int, s *goquery.Selection) {
matches := DATE.FindAllString(s.Text(), -1)
if len(matches) > 0 {
dateString = matches[0]
}
})
doc.Find("p").Each(func(i int, s *goquery.Selection) {
selected := s.Find("strong")
selected.Each(func(i int, str *goquery.Selection) {
cleaned := RemoveNonAlpha(str.Text())
if val, ok := PROVINCES[cleaned]; ok {
lines := strings.Split(strings.TrimSpace(s.Text()), "\n")
for i := 1; i < len(lines); i++ {
// Gotcha; weird hypen/dash usage
formatted := strings.TrimSpace(strings.Replace(strings.Replace(lines[i], "-", "", -1), "–", "", -1))
internalID++
instances = append(instances, ParseInstance(dateString, formatted, val, internalID))
}
}
})
})
return instances
}
/**
Crawl a given document, find <a> tags with 'href' fields, as per https://www.gov.za/newsroom DOM
Pass the found tags through a regular expression check for corona virus 'meta-data'
If the checks are passed, pass through 1 more filter to see if we can determine the number of cases,
if this check fails, the 'cases' is set to UNDEF at this point.
We calculate them later on.
Return a slice of Results
**/
func Crawl(doc *goquery.Document, opt string) []Result {
var results = make([]Result, 0)
hrefRegex := ""
if opt == "1" {
hrefRegex = HREF_REGEX
} else {
hrefRegex = ALT_HREF
}
re := regexp.MustCompile("[0-9]+")
doc.Find("a[href]").Each(func(index int, item *goquery.Selection) {
href, _ := item.Attr("href")
if strings.Contains(href, hrefRegex) {
values := re.FindAllString(item.Text(), -1)
if !UNIQUE_LINKS[href] {
if len(values) > 1 {
results = append(results, Result{link: href, title: item.Text(), count: values[0]})
} else {
results = append(results, Result{link: href, title: item.Text(), count: "UNDEF"})
}
UNIQUE_LINKS[href] = true
}
}
})
return results
}
func ParseCsv(filename string) []Instance {
csvFile, _ := os.Open(filename)
reader := csv.NewReader(bufio.NewReader(csvFile))
var instances []Instance
for {
line, error := reader.Read()
if error == io.EOF {
break
} else if error != nil {
log.Fatal(error)
}
instances = append(instances, Instance{
case_id: line[0],
date: line[1],
YYMMDD: line[2],
country: line[3],
province: line[4],
geo_subdivision: line[5],
age: line[6],
gender: line[7],
transmission_type: line[8],
},
)
}
return instances
}
func RemoveNonAlphaNumberic(str string) string {
reg, err := regexp.Compile("[^a-zA-Z0-9-]+")
if err != nil {
log.Fatal(err)
}
processedString := reg.ReplaceAllString(str, "")
return processedString
}
func RemoveNonAlpha(str string) string {
reg, err := regexp.Compile("[^a-zA-Z]+")
if err != nil {
log.Fatal(err)
}
processedString := reg.ReplaceAllString(str, "")
return processedString
}
func RemoveLeadingDash(str string) string {
reg, err := regexp.Compile("[^a-zA-Z0-9]+")
if err != nil {
log.Fatal(err)
}
processedString := reg.ReplaceAllString(str, "")
return processedString
}
/**
TODO - When GOV.ZA is back up, this should be done with real data
**/
func AmendCurrent() {
newEntry := "52,15-03-2020,20200315,South Africa,KZN,ZA-KZN,34,male,Travelled to UK"
// add a line to current.csv
f, err := os.OpenFile("current.csv", os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
panic(err)
}
defer f.Close()
if _, err = f.WriteString(newEntry); err != nil {
panic(err)
}
}
func GetUpdatedFile(w http.ResponseWriter, r *http.Request) {
GetCurrent()
AmendCurrent()
Openfile, err := os.Open("current.csv")
defer Openfile.Close() //Close after function return
if err != nil {
//File not found, send 404
http.Error(w, "File not found.", 404)
return
}
FileHeader := make([]byte, 512)
Openfile.Read(FileHeader)
FileContentType := http.DetectContentType(FileHeader)
FileStat, _ := Openfile.Stat()
FileSize := strconv.FormatInt(FileStat.Size(), 10)
//Send the headers
w.Header().Set("Content-Disposition", "attachment; filename=covid19za_timeline_confirmed.csv")
w.Header().Set("Content-Type", FileContentType)
w.Header().Set("Content-Length", FileSize)
//Send the file
//We read 512 bytes from the file already, so we reset the offset back to 0
Openfile.Seek(0, 0)
io.Copy(w, Openfile) //'Copy' the file to the client
Cleanup()
return
}
func Index(w http.ResponseWriter, r *http.Request) {
path := "." + r.URL.Path
if path == "./" {
path = "./static/index.html"
} else {
path = "./static" + path
}
http.ServeFile(w, r, path)
}
/**
FIXME: This should be done with go-git when I have time...
**/
func GetCurrent() {
//get the response
resp, err := http.Get("https://raw.githubusercontent.com/dsfsi/covid19za/master/data/covid19za_timeline_confirmed.csv")
//body
body, err := ioutil.ReadAll(resp.Body)
//header
var header string
for h, v := range resp.Header {
for _, v := range v {
header += fmt.Sprintf("%s %s \n", h, v)
}
}
//append all to one slice
var write []byte
write = append(write, body...)
//write it to a file
err = ioutil.WriteFile("current.csv", write, 0644)
if err != nil {
panic(err)
}
}
func Cleanup() {
os.Remove("current.csv")
}
// persky line carriage...
func removeLBR(text string) string {
re := regexp.MustCompile(`\x{000D}\x{000A}|[\x{000A}\x{000B}\x{000C}\x{000D}\x{0085}\x{2028}\x{2029}]`)
return re.ReplaceAllString(text, ``)
}