Pollen: Add pollen module
parent
756919d533
commit
ad15e3e4da
@ -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
|
||||
}
|
Loading…
Reference in New Issue