covid19-edmonton/main.go
2020-04-24 20:36:05 -06:00

84 lines
1.8 KiB
Go

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')
}