You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
148 lines
3.4 KiB
148 lines
3.4 KiB
package pollen |
|
|
|
import ( |
|
"../config" |
|
"bytes" |
|
"encoding/json" |
|
"github.com/olekukonko/tablewriter" |
|
"io/ioutil" |
|
"net/http" |
|
"strings" |
|
"time" |
|
) |
|
|
|
type Pollen struct { |
|
Config *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 { |
|
// Drop rows that have the status: "inga halter" or "ingen rapport" |
|
if v2.Day0Description[:3] == "ing" && |
|
v2.Day1Description[:3] == "ing" && |
|
v2.Day2Description[:3] == "ing" && |
|
v2.Day3Description[:3] == "ing" { |
|
continue |
|
} |
|
|
|
table.Append([]string{ |
|
v2.Type, |
|
v2.Day0Description, |
|
v2.Day1Description, |
|
v2.Day2Description, |
|
v2.Day3Description, |
|
}) |
|
} |
|
} |
|
|
|
if table.NumLines() == 0 { |
|
return []string{} |
|
} |
|
|
|
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 |
|
}
|
|
|