initial commit
This commit is contained in:
parent
05ba8ec8fd
commit
e5f87a099a
@ -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.
|
||||
|
83
main.go
Normal file
83
main.go
Normal file
@ -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')
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user