about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorCodeCarefully <CodeCarefully@users.noreply.github.com>2020-05-28 00:45:11 +0300
committerGitHub <noreply@github.com>2020-05-28 00:45:11 +0300
commit07b0b06efc84eb1874b9e5a54061424233afd09f (patch)
tree04008926813ee78be266be5b1eb3a2379037b4f6
parent9918422f1dc7a7b276bdf7b4020aa335bf688850 (diff)
downloadmyCovidCLI-07b0b06efc84eb1874b9e5a54061424233afd09f.tar.gz
Update and rename parse.go to myCovidCLI.go
Added a lot more resilience (Find the latest csv, instead of guessing)
Added "Recovered"
Added "Still sick"
Removed Italy
Removed unused code
-rw-r--r--myCovidCLI.go138
-rw-r--r--parse.go137
2 files changed, 138 insertions, 137 deletions
diff --git a/myCovidCLI.go b/myCovidCLI.go
new file mode 100644
index 0000000..0e9b790
--- /dev/null
+++ b/myCovidCLI.go
@@ -0,0 +1,138 @@
+package main
+import (
+   "myCOVIDcli/renderfloat"
+   "encoding/csv"
+   "fmt"
+   "github.com/olekukonko/tablewriter"
+   "io"
+   "io/ioutil"
+   "log"
+   "net/http"
+   "os"
+   "strconv"
+   "strings"
+   "time"
+)
+
+
+func main() {
+
+   maxloops :=3
+   currentTime := time.Now() //get current time/date
+   bodydata := ""
+   for i := 0; i < maxloops; i++ {
+
+      strcurrentdate := currentTime.Format("01-02-2006") //reformat for URL format
+      COVIDurl := "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/"+strcurrentdate+".csv"
+      //fmt.Println(COVIDurl)
+      resp, err := http.Get(COVIDurl)
+      if err != nil {
+         panic(err)
+      }
+      defer resp.Body.Close()
+      //fmt.Println(resp.StatusCode)
+      if resp.StatusCode == 200 {
+         body, err := ioutil.ReadAll(resp.Body)
+         if err != nil {
+            log.Fatalln(err)
+         }
+         bodydata = string(body)
+         fmt.Println("Last Updated: " + strcurrentdate)
+         break
+      }
+
+      currentTime = currentTime.AddDate(0, 0, -1) //go to yesterday, this source updates only daily
+   }
+
+
+   //fmt.Println(bodydata)
+   reader := csv.NewReader(strings.NewReader(bodydata))
+
+   //zero out all variables
+   usconfirmed := 0
+   usdeaths := 0
+   usrecovered := 0
+   region := ""
+   state := ""
+   confirmed := ""
+   deaths:=""
+   recovered:=""
+   data := [][]string{}
+
+   for {
+      line, error := reader.Read() //read in a line
+      if error == io.EOF {
+         break
+      } else if error != nil {
+         log.Fatal(error)
+      }
+
+      region = line[3]
+      state = line[1]
+      confirmed = line[7]
+      deaths = line[8]
+      recovered = line[9]
+
+      if region == "US" {
+
+         confirmed, err := strconv.Atoi(line[7]) //convert confirmed to int
+         if err != nil {
+            // handle error
+            fmt.Println(err)
+            os.Exit(2)
+         }
+         deaths, err := strconv.Atoi(line[8]) //convert deaths to int
+         if err != nil {
+            // handle error
+            fmt.Println(err)
+            os.Exit(2)
+         }
+
+         recovered, err := strconv.Atoi(line[9]) //convert recovered to int
+         if err != nil {
+            // handle error
+            fmt.Println(err)
+            os.Exit(2)
+         }
+
+         usconfirmed = usconfirmed + confirmed
+         usdeaths = usdeaths + deaths
+         usrecovered = usrecovered + recovered
+      }
+
+      //create array for table output
+      if state == "New York City" || region == "Israel" || region == "Estonia" {
+         f_confirmed, _ := strconv.ParseFloat(confirmed, 8)
+         f_deaths, _ := strconv.ParseFloat(deaths, 8)
+         f_recovered, _ := strconv.ParseFloat(recovered, 8)
+
+
+
+         data = append(data, []string{region, state,
+            renderfloat.RenderFloat("#,###.", f_confirmed),
+            renderfloat.RenderFloat("#,###.", f_deaths),
+            renderfloat.RenderFloat("#,###.", f_recovered),
+            renderfloat.RenderFloat("#,###.", f_confirmed - f_recovered-f_deaths)})
+         //fmt.Println(state + "   Deaths: " + deaths + " Confirmed: " + confirmed )
+      }
+
+   }
+
+
+      f_confirmed, _ := strconv.ParseFloat(strconv.Itoa(usconfirmed), 8)
+      f_deaths, _ := strconv.ParseFloat(strconv.Itoa(usdeaths), 8)
+      f_recovered, _ := strconv.ParseFloat(strconv.Itoa(usdeaths), 8)
+      data = append(data, []string{"US","Total",
+         renderfloat.RenderFloat("#,###.", f_confirmed),
+         renderfloat.RenderFloat("#,###.", f_deaths),
+         renderfloat.RenderFloat("#,###.", f_recovered),
+         renderfloat.RenderFloat("#,###.", f_confirmed - f_recovered-f_deaths)})
+
+   table := tablewriter.NewWriter(os.Stdout)
+   table.SetHeader([]string{"Region","Area", "Confirmed", "Deaths", "Recovered","Still sick"})
+
+   for _, v := range data {
+      table.Append(v)
+   }
+   table.Render() // Send output
+   }
diff --git a/parse.go b/parse.go
deleted file mode 100644
index 4b22082..0000000
--- a/parse.go
+++ /dev/null
@@ -1,137 +0,0 @@
-package main
-
-import (
-	"bufio"
-	"coronaVirus/renderfloat"
-	"encoding/csv"
-	"fmt"
-	"github.com/olekukonko/tablewriter"
-	"io"
-	"log"
-	"net/http"
-	"os"
-	"strconv"
-	"time"
-)
-
-// DownloadFile will download a url to a local file. It's efficient because it will
-// write as it downloads and not load the whole file into memory.
-func DownloadFile(filepath string, url string) error {
-
-	// Get the data
-	resp, err := http.Get(url)
-	if err != nil {
-		return err
-	}
-	defer resp.Body.Close()
-
-	// Create the file
-	out, err := os.Create(filepath)
-	if err != nil {
-		return err
-	}
-	defer out.Close()
-
-	// Write the body to file
-	_, err = io.Copy(out, resp.Body)
-	return err
-}
-
-func main() {
-	currentTime := time.Now() //get current time/date
-	currentTime = currentTime.AddDate(0, 0, -1) //go to yesterday, this source updates only daily
-	strcurrentdate := currentTime.Format("01-02-2006") //reformat for URL format
-	COVIDurl := "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/"+strcurrentdate+".csv"
-
-	//fmt.Println(COVIDurl)
-
-	filepath := "covid.csv" // path to save the CSV to.
-	if err := DownloadFile(filepath, COVIDurl); err != nil {
-		panic(err)
-	}
-
-	csvFile, _ := os.Open(filepath)
-	reader := csv.NewReader(bufio.NewReader(csvFile))
-
-	//zero out all variables
-	usconfirmed := 0
-	usdeaths := 0
-	region := ""
-	state := ""
-	confirmed := ""
-	deaths:=""
-
-	data := [][]string{}
-
-	for {
-		line, error := reader.Read() //read in a line
-		if error == io.EOF {
-			break
-		} else if error != nil {
-			log.Fatal(error)
-		}
-
-		region = line[3]
-		state = line[1]
-		confirmed = line[7]
-		deaths = line[8]
-
-		if region == "US" {
-
-			confirmed, err := strconv.Atoi(line[7]) //convert confirmed to int
-			if err != nil {
-				// handle error
-				fmt.Println(err)
-				os.Exit(2)
-			}
-			deaths, err := strconv.Atoi(line[8]) //convert deaths to int
-			if err != nil {
-				// handle error
-				fmt.Println(err)
-				os.Exit(2)
-			}
-			usconfirmed = usconfirmed + confirmed
-			usdeaths = usdeaths + deaths
-		}
-
-		//create array for table output
-		if state == "New York City" {
-			f, _ := strconv.ParseFloat(confirmed, 8)
-			data = append(data, []string{state, renderfloat.RenderFloat("#,###.", f), deaths})
-			//fmt.Println(state + "   Deaths: " + deaths + " Confirmed: " + confirmed )
-		}
-
-		if region == "Israel" {
-			f, _ := strconv.ParseFloat(confirmed, 8)
-			data = append(data, []string{region, renderfloat.RenderFloat("#,###.", f), deaths})
-			//fmt.Println(region + "   Deaths: " + deaths + " Confirmed: " + confirmed )
-		}
-
-		if region == "Italy" {
-			f, _ := strconv.ParseFloat(confirmed, 8)
-			d, _ := strconv.ParseFloat(deaths, 8)
-			data = append(data, []string{region, renderfloat.RenderFloat("#,###.", f), renderfloat.RenderFloat("#,###.", d)})
-			//fmt.Println(region + "   Deaths: " + deaths + " Confirmed: " + confirmed )
-		}
-
-		if region == "Estonia" {
-			f, _ := strconv.ParseFloat(confirmed, 8)
-			data = append(data, []string{region, renderfloat.RenderFloat("#,###.", f), deaths})
-			//fmt.Println(region + "   Deaths: " + deaths + " Confirmed: " + confirmed )
-		}
-	}
-
-		usconfirmed1, _ := strconv.ParseFloat(strconv.Itoa(usconfirmed), 8)
-		usdeaths1, _ := strconv.ParseFloat(strconv.Itoa(usdeaths), 8)
-		data = append(data, []string{"USA", renderfloat.RenderFloat("#,###.", usconfirmed1), renderfloat.RenderFloat("#,###.", usdeaths1) })
-
-	table := tablewriter.NewWriter(os.Stdout)
-	table.SetHeader([]string{"Area", "Confirmed", "Deaths"})
-
-	for _, v := range data {
-		table.Append(v)
-	}
-	table.Render() // Send output
-
-	}
-