From e5f87a099a27d8beaccc85cec463e04ad8858609 Mon Sep 17 00:00:00 2001 From: Steven Polley Date: Fri, 24 Apr 2020 20:36:05 -0600 Subject: [PATCH] initial commit --- README.md | 1 + main.go | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 main.go diff --git a/README.md b/README.md index 6d58b9e..42a7b92 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ # covid19-edmonton +I was bored and want to keep tabs on this in my area. Will download latest stats from alberta government website for COVID-19 and output the number of total and active cases in my area. diff --git a/main.go b/main.go new file mode 100644 index 0000000..c3fe9fb --- /dev/null +++ b/main.go @@ -0,0 +1,83 @@ +package main + +import ( + "bufio" + "encoding/json" + "fmt" + "io/ioutil" + "log" + "net/http" + "os" + "strings" +) + +const ( + statsURL = "https://covid19stats.alberta.ca" +) + +type Data [][]string + +// They screwed this up so much, the first index in the column, not the row. WTF alberta +// [0] = case number alberta +// [1] = date +// [2] = geo zone +// [3] = gender +// [4] = age range +// [5] = case status +// [6] = confirmed/suspected + +func main() { + + // Download the latest stats + resp, err := http.Get(statsURL) + if err != nil { + log.Fatalf("failed to download stats page '%s': %v", statsURL, err) + } + defer resp.Body.Close() + + bodyB, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Fatalf("failed to read stats page response body: %v", err) + } + + // Parse the HTML for the data + + // Find the beginning, trim off everything before + split := strings.Split(string(bodyB), "\"data\":[[\"1\",") + body := fmt.Sprintf("%s%s", "[[\"1\",", split[1]) + + // Find the end, trim off everything after + split = strings.Split(body, "]]") + body = fmt.Sprintf("%s%s", split[0], "]]") + + // Parse the json string into a struct + data := Data{} + err = json.Unmarshal([]byte(body), &data) + if err != nil { + log.Fatalf("failed to parse data as JSON: %v", err) + } + + // count the cases + var totalCasesEdmonton, activeCasesEdmonton, totalCasesAlberta, activeCasesAlberta int + for i := range data[2] { + if data[2][i] == "Edmonton Zone" { + totalCasesEdmonton++ + if data[5][i] == "Active" { + activeCasesEdmonton++ + } + } + if data[5][i] == "Active" { + activeCasesAlberta++ + } + totalCasesAlberta++ + } + + fmt.Println("Edmonton Active: ", activeCasesEdmonton) + fmt.Println("Edmonton Total: ", totalCasesEdmonton) + fmt.Println("Alberta Active: ", activeCasesAlberta) + fmt.Println("Alberta Total: ", totalCasesAlberta) + + fmt.Print("\n\nPress 'Enter' to continue...") + bufio.NewReader(os.Stdin).ReadBytes('\n') + +}