Code Monkey home page Code Monkey logo

Comments (8)

natea123 avatar natea123 commented on June 7, 2024

New to open-source contribution but would like to take a crack at this.

Since this is pulling from an API just showing cases in Brazil [1] would you want to just implement states/cities filter for Brazil?

[1] https://covid19-brazil-api-docs.now.sh/

from alertcovid19.

natea123 avatar natea123 commented on June 7, 2024

Additionally, implementing the NovelCOVID API [1] may make filtering by country easier

[1] https://github.com/NovelCOVID/API

from alertcovid19.

renanbastos93 avatar renanbastos93 commented on June 7, 2024

New to open-source contribution but would like to take a crack at this.

Since this is pulling from an API just showing cases in Brazil [1] would you want to just implement states/cities filter for Brazil?

[1] https://covid19-brazil-api-docs.now.sh/

Hello, thanks for this interesting contribution here.

Well, I believe that we should create a filter for any country, state, and city. I wish this system be used by anyone.

from alertcovid19.

renanbastos93 avatar renanbastos93 commented on June 7, 2024

Additionally, implementing the NovelCOVID API [1] may make filtering by country easier

[1] https://github.com/NovelCOVID/API

How nice, project it is interesting I will see.
we can use this API too.

from alertcovid19.

natea123 avatar natea123 commented on June 7, 2024

Working on adding the new API to the application with minimal change but running into error

json: cannot unmarshal array into Go value of type struct { Data main.LastValues "json:"data"" }

Below is my current implementation, running into issues unmarshaling the data, any insight would be appreciated:

package main

import (
	"context"
	"encoding/json"
	"flag"
	"fmt"
	"log"
	"net/http"
	"os"
	"time"

	"github.com/gen2brain/beeep"
)

// Exported ...
const (
	IMG string = "https://static.poder360.com.br/2020/03/23312-868x644.png"
	URL        = "https://corona.lmao.ninja/countries?sort=country"
)

// LastValues ...
type LastValues struct {
	Confirmed int `json:"cases"`
	Deaths    int `json:"deaths"`
	Recovered int `json:"recovered"`
}

func (l LastValues) String() string {
	return fmt.Sprintf("Confirmed: %d, Deaths: %d, Recovered: %d", l.Confirmed, l.Deaths, l.Recovered)
}

// fetchCOVID19Data ...
func fetchCOVID19Data(ctx context.Context, req *http.Request) <-chan LastValues {
	ch := make(chan LastValues)
	go func() {
		var r struct {
			Data LastValues `json:"data"`
		}
		body, err := http.DefaultClient.Do(req)
		if err != nil {
			log.Printf("fetchCOVID19Data: %v", err)
			return
		}
		defer body.Body.Close()
		err = json.NewDecoder(body.Body).Decode(&r)
		if err != nil {
			log.Printf("fetchCOVID19Data: %v", err)
			return
		}

		select {
		case ch <- LastValues{r.Data.Confirmed, r.Data.Deaths, r.Data.Recovered}:
		case <-ctx.Done():
		}
	}()
	return ch
}

func routine(sleep time.Duration) {
	cachedVal := LastValues{}
	const timeout = time.Second * 2
	for {
		ctx, cancel := context.WithTimeout(context.Background(), timeout)
		req, err := http.NewRequestWithContext(ctx, "GET", URL, nil)
		if err != nil {
			panic("internal error - misuse of NewRequestWithContext")
		}
		select {
		case newVal := <-fetchCOVID19Data(ctx, req):
			if cachedVal != newVal {
				err := beeep.Alert("COVID-19 Brazil", newVal.String(), IMG)
				if err != nil {
					log.Printf("rountine: %v", err)
				}
				cachedVal = newVal
			}
		case <-ctx.Done():
			log.Printf("rountine: %v", ctx.Err())
		}
		cancel()
		log.Printf("sleeping for %s", sleep)
		time.Sleep(sleep)
	}
}

func main() {
	log.SetPrefix(os.Args[0] + ": ")
	log.SetFlags(0)
	var timer time.Duration
	flag.DurationVar(&timer, "t", time.Hour, "interval between each api request")
	flag.Parse()
	routine(timer)
}

from alertcovid19.

natea123 avatar natea123 commented on June 7, 2024

Below is how im unmarshaling in my current working implementation of the API:

package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"io/ioutil"
	"net/http"
	"os"
)

func main() {

	var country string

	flag.StringVar(&country, "country", "", "specify country for COVID case stats")
	flag.Parse()

	type Resp struct {
		Country   string
		Cases     int
		Deaths    int
		Recovered int
	}

	url := "https://corona.lmao.ninja/countries?sort=country"
	method := "GET"

	client := &http.Client{}
	req, err := http.NewRequest(method, url, nil)

	if err != nil {
		fmt.Println(err)
	}
	res, err := client.Do(req)
	defer res.Body.Close()
	body, err := ioutil.ReadAll(res.Body)

	var resp []Resp
	json.Unmarshal([]byte(body), &resp)
}

from alertcovid19.

renanbastos93 avatar renanbastos93 commented on June 7, 2024

Working on adding the new API to the application with minimal change but running into error

json: cannot unmarshal array into Go value of type struct { Data main.LastValues "json:"data"" }

Below is my current implementation, running into issues unmarshaling the data, any insight would be appreciated:

package main

import (
	"context"
	"encoding/json"
	"flag"
	"fmt"
	"log"
	"net/http"
	"os"
	"time"

	"github.com/gen2brain/beeep"
)

// Exported ...
const (
	IMG string = "https://static.poder360.com.br/2020/03/23312-868x644.png"
	URL        = "https://corona.lmao.ninja/countries?sort=country"
)

// LastValues ...
type LastValues struct {
	Confirmed int `json:"cases"`
	Deaths    int `json:"deaths"`
	Recovered int `json:"recovered"`
}

func (l LastValues) String() string {
	return fmt.Sprintf("Confirmed: %d, Deaths: %d, Recovered: %d", l.Confirmed, l.Deaths, l.Recovered)
}

// fetchCOVID19Data ...
func fetchCOVID19Data(ctx context.Context, req *http.Request) <-chan LastValues {
	ch := make(chan LastValues)
	go func() {
		var r struct {
			Data LastValues `json:"data"`
		}
		body, err := http.DefaultClient.Do(req)
		if err != nil {
			log.Printf("fetchCOVID19Data: %v", err)
			return
		}
		defer body.Body.Close()
		err = json.NewDecoder(body.Body).Decode(&r)
		if err != nil {
			log.Printf("fetchCOVID19Data: %v", err)
			return
		}

		select {
		case ch <- LastValues{r.Data.Confirmed, r.Data.Deaths, r.Data.Recovered}:
		case <-ctx.Done():
		}
	}()
	return ch
}

func routine(sleep time.Duration) {
	cachedVal := LastValues{}
	const timeout = time.Second * 2
	for {
		ctx, cancel := context.WithTimeout(context.Background(), timeout)
		req, err := http.NewRequestWithContext(ctx, "GET", URL, nil)
		if err != nil {
			panic("internal error - misuse of NewRequestWithContext")
		}
		select {
		case newVal := <-fetchCOVID19Data(ctx, req):
			if cachedVal != newVal {
				err := beeep.Alert("COVID-19 Brazil", newVal.String(), IMG)
				if err != nil {
					log.Printf("rountine: %v", err)
				}
				cachedVal = newVal
			}
		case <-ctx.Done():
			log.Printf("rountine: %v", ctx.Err())
		}
		cancel()
		log.Printf("sleeping for %s", sleep)
		time.Sleep(sleep)
	}
}

func main() {
	log.SetPrefix(os.Args[0] + ": ")
	log.SetFlags(0)
	var timer time.Duration
	flag.DurationVar(&timer, "t", time.Hour, "interval between each api request")
	flag.Parse()
	routine(timer)
}

Hello, I am sorry for you to wait,

Well, this error occurrent because the JSON in the body is not matched with struct LastValues then we need to change struct to another approach. According to your another example in comment #3 (comment)

Like this:

type LastValue struct {
	Country   string `json:"country"`
	Cases     int 	 `json:"cases"`
	Deaths    int    `json:"deaths"`
	Recovered int    `json:"recovered"`
}

from alertcovid19.

renanbastos93 avatar renanbastos93 commented on June 7, 2024

Both examples sound good. So I suggest we create the filter that compares country case is different to Brazil use Novel API else use currently API when to use Novel we can get geolocation from client to get country based this.

@natea123 What do you think?

from alertcovid19.

Related Issues (7)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.