Browse Source

Pollen: Add pollen module

master
Elis Hirwing 3 years ago
parent
commit
ad15e3e4da
Signed by: etu GPG Key ID: D57EFA625C9A925F
  1. 7
      main.go
  2. 5
      status.sample.toml
  3. 5
      statusscreen-config.go
  4. 135
      statusscreen-pollen.go

7
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")
}

5
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"

5
statusscreen-config.go

@ -15,4 +15,9 @@ type Config struct {
URL string
RefreshDelay int64
}
Pollen struct {
URL string
RefreshDelay int64
CityName string
}
}

135
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
}
Loading…
Cancel
Save