about summary refs log tree commit diff stats
path: root/html/027call_ingredient.cc.html
Commit message (Expand)AuthorAgeFilesLines
* 3395Kartik K. Agaram2016-09-171-14/+14
* 3315Kartik K. Agaram2016-09-101-6/+10
* 3219Kartik K. Agaram2016-08-171-0/+1
* 3158Kartik K. Agaram2016-07-271-2/+2
* 3102Kartik K. Agaram2016-07-051-1/+2
* 2996Kartik K. Agaram2016-05-211-7/+7
* 2866Kartik K. Agaram2016-04-251-0/+221
07'>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
package main

import (
	"encoding/csv"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"strconv"
	"strings"
	"time"

	"github.com/olekukonko/tablewriter"
	"myCOVIDcli/renderfloat"
)

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.Fprintln(os.Stderr, err)
				os.Exit(2)
			}
			deaths, err := strconv.Atoi(line[8]) //convert deaths to int
			if err != nil {
				// handle error
				fmt.Fprintln(os.Stderr, err)
				os.Exit(2)
			}

			recovered, err := strconv.Atoi(line[9]) //convert recovered to int
			if err != nil {
				// handle error
				fmt.Fprintln(os.Stderr, 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
}