From ad15e3e4dacf88de97891c86746fa0d5eeeda361 Mon Sep 17 00:00:00 2001 From: Elis Hirwing Date: Tue, 17 Apr 2018 23:14:50 +0200 Subject: [PATCH] Pollen: Add pollen module --- main.go | 7 +++ status.sample.toml | 5 ++ statusscreen-config.go | 5 ++ statusscreen-pollen.go | 135 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 152 insertions(+) create mode 100644 statusscreen-pollen.go diff --git a/main.go b/main.go index dd95d85..29925bd 100644 --- a/main.go +++ b/main.go @@ -15,6 +15,7 @@ func main() { sl := SL{Config: &config} clock := Clock{Config: &config} weather := Weather{Config: &config} + pollen := Pollen{Config: &config} tm.Clear() tm.Print("\033[?25l") @@ -34,6 +35,12 @@ func main() { tm.Print("\033[K\n") + for _, row := range pollen.GetOutput() { + tm.Print(row + "\033[K\n") + } + + tm.Print("\033[K\n") + for _, row := range sl.GetOutput() { tm.Print(row + "\033[K\n") } diff --git a/status.sample.toml b/status.sample.toml index 3c6cb66..6c3164d 100644 --- a/status.sample.toml +++ b/status.sample.toml @@ -10,3 +10,8 @@ TimeFormat = "15:04:05" [Weather] URL = "http://wttr.in/" RefreshDelay = 1800 + +[Pollen] +URL = "https://pollenkoll.se/wp-content/themes/pollenkoll/api/get_cities.php" +RefreshDelay = 1800 +CityName = "Stockholm" diff --git a/statusscreen-config.go b/statusscreen-config.go index 5ccf6b9..108f4e0 100644 --- a/statusscreen-config.go +++ b/statusscreen-config.go @@ -15,4 +15,9 @@ type Config struct { URL string RefreshDelay int64 } + Pollen struct { + URL string + RefreshDelay int64 + CityName string + } } diff --git a/statusscreen-pollen.go b/statusscreen-pollen.go new file mode 100644 index 0000000..795e429 --- /dev/null +++ b/statusscreen-pollen.go @@ -0,0 +1,135 @@ +package main + +import ( + "bytes" + "encoding/json" + "github.com/olekukonko/tablewriter" + "io/ioutil" + "net/http" + "strings" + "time" +) + +type Pollen struct { + Config *Config + lastResponse PollenResponse + nextRefresh int64 +} + +type PollenResponse []struct { + CityId int64 `json:"cityid"` + DateMod string `json:"date_mod"` + Name string `json:"name"` + Latitude string `json:"latitude"` + Longitude string `json:"longitude"` + RegionId string `json:"region_id"` + RegionUrl string `json:"region_url"` + + Pollen []struct { + Type string `json:"type"` + Day0Value string `json:"day0_value"` + Day0Description string `json:"day0_desc"` + Day0Name string `json:"day0_name"` + Day0Date string `json:"day0_date"` + Day0RelativeDate string `json:"day0_relative_date"` + Day1Value string `json:"day1_value"` + Day1Description string `json:"day1_desc"` + Day1Name string `json:"day1_name"` + Day1Date string `json:"day1_date"` + Day1RelativeDate string `json:"day1_relative_date"` + Day2Value string `json:"dayX_value"` + Day2Description string `json:"day2_desc"` + Day2Name string `json:"day2_name"` + Day2Date string `json:"day2_date"` + Day2RelativeDate string `json:"day2_relative_date"` + Day3Value string `json:"day3_value"` + Day3Description string `json:"day3_desc"` + Day3Name string `json:"day3_name"` + Day3Date string `json:"day3_date"` + Day3RelativeDate string `json:"day3_relative_date"` + } `json:"pollen"` +} + +func (pollen *Pollen) GetOutput() []string { + var b bytes.Buffer + + // Prepare table + table := tablewriter.NewWriter(&b) + table.SetHeader([]string{ + "Typ", + "Idag", + "Imorgon", + "Övermorgon", + "Överövermorgon", + }) + + // Check if it's time to refresh yet + if pollen.nextRefresh <= time.Now().Unix() { + pollen.nextRefresh += 10 + + go pollen.getData() + } + + for _, v := range pollen.lastResponse { + if v.Name != pollen.Config.Pollen.CityName { + continue + } + + for _, v2 := range v.Pollen { + table.Append([]string{ + v2.Type, + v2.Day0Description, + v2.Day1Description, + v2.Day2Description, + v2.Day3Description, + }) + } + } + + table.Render() + + return strings.Split(b.String(), "\n") +} + +func (pollen *Pollen) getData() { + pollenData, err := pollen.getPollenData() + + if err == nil { + pollen.lastResponse = pollenData + pollen.nextRefresh = time.Now().Unix() + pollen.Config.Pollen.RefreshDelay + } else { + // log.Fatal(err) + } +} + +func (pollen *Pollen) getPollenData() (PollenResponse, error) { + parsedResponse := PollenResponse{} + + // Prepare URL to fetch data from + url := pollen.Config.Pollen.URL + + // Set up http client to fetch pollen data + client := http.Client{ + Timeout: time.Second * 5, + } + + // Do request + resp, err := client.Get(url) + if err != nil { + return parsedResponse, err + } + + // Read response + body, readErr := ioutil.ReadAll(resp.Body) + if readErr != nil { + return parsedResponse, err + } + + // Parse response + parseErr := json.Unmarshal(body, &parsedResponse) + if parseErr != nil { + return parsedResponse, parseErr + } + + return parsedResponse, nil +}