fuelprices/functions.go

143 lines
3.0 KiB
Go
Raw Normal View History

2019-06-05 21:48:21 +02:00
package main
import (
"archive/zip"
"bytes"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"github.com/antchfx/xmlquery"
"gopkg.in/ini.v1"
)
// ParseArgs is not yet implemented
2019-06-05 21:48:21 +02:00
func ParseArgs() error {
return nil
}
// GetConfig fetch config from ini file
2019-06-05 21:48:21 +02:00
func GetConfig(configfile string, fuelpricesconfig *FuelPricesConfig) error {
flag.Usage = Usage
config, err := ini.Load(configfile)
HandleFatalError(err)
var fpc FuelPricesConfig
fuelpricesSection := config.Section("fuelprices")
fpc.RemoteURL = fuelpricesSection.Key("remote_url").String()
fpc.RemoteFilename = fuelpricesSection.Key("remote_filename").String()
fpc.XPathBase = fuelpricesSection.Key("xpath_base").String()
2019-06-15 13:55:48 +02:00
fpc.Table = fuelpricesSection.Key("table").String()
influxdbSection := config.Section("influxdb")
fpc.InfluxHost = influxdbSection.Key("hostname").String()
fpc.InfluxPort, err = influxdbSection.Key("port").Int()
2019-06-05 21:48:21 +02:00
HandleError(err)
2019-06-15 13:55:48 +02:00
fpc.InfluxUser = influxdbSection.Key("username").String()
fpc.InfluxPass = influxdbSection.Key("password").String()
2019-06-05 21:48:21 +02:00
HandleError(err)
2019-06-15 13:55:48 +02:00
fpc.InfluxDB = influxdbSection.Key("database").String()
2019-06-05 21:48:21 +02:00
2019-06-09 13:40:19 +02:00
fpc.Pos = fuelpricesSection.Key("pos").Strings(",")
fpc.Types = fuelpricesSection.Key("types").Strings(",")
2019-06-05 21:48:21 +02:00
*fuelpricesconfig = fpc
return err
}
// DownloadFile fetch file from webserver
2019-06-05 21:48:21 +02:00
func DownloadFile(url string) error {
resp, err := http.Get(url)
2019-06-09 13:40:19 +02:00
HandleError(err)
2019-06-05 21:48:21 +02:00
szip.Content, err = ioutil.ReadAll(resp.Body)
2019-06-09 13:40:19 +02:00
HandleError(err)
2019-06-05 21:48:21 +02:00
err = resp.Body.Close()
2019-06-09 13:40:19 +02:00
HandleError(err)
2019-06-05 21:48:21 +02:00
return err
}
// ExtractZip get the XML file to be processed
2019-06-05 21:48:21 +02:00
func ExtractZip() error {
zipfile, err = zip.NewReader(bytes.NewReader(szip.Content), int64(len(szip.Content)))
if err != nil {
log.Fatal("Unable to open zipfile")
}
for _, f := range zipfile.File {
2019-06-06 00:04:51 +02:00
if f.Name == fpc.RemoteFilename {
2019-06-05 21:48:21 +02:00
rc, err := f.Open()
if err != nil {
return err
}
ofile.Content, err = ioutil.ReadAll(rc)
rc.Close()
} else {
log.Fatal("File not found")
}
}
return err
}
// GetPrices parses the XML file and get values of prices
2019-06-05 21:48:21 +02:00
func GetPrices(prices **[]Price, spos []string, stypes []string, sxpathbase string) error {
var pr []Price
var xml *xmlquery.Node
f := bytes.NewReader(ofile.Content)
xml, err = xmlquery.Parse(f)
HandleError(err)
for _, station := range spos {
for _, fuel := range stypes {
query := fmt.Sprintf(sxpathbase, station, fuel)
list := xmlquery.FindOne(xml, query)
for _, i := range list.Attr {
if i.Name.Local == "valeur" {
var val float64
if s, err := strconv.ParseFloat(i.Value, 64); err == nil {
val = s
}
pr = append(pr, Price{ID: station, Fuel: fuel, Amount: val})
2019-06-05 21:48:21 +02:00
}
}
}
}
*prices = &pr
return err
}
// HandleError handles errors to return err
2019-06-05 21:48:21 +02:00
func HandleError(err error) error {
if err != nil {
return err
}
return nil
}
// HandleFatalError fatal errors
func HandleFatalError(err error) {
if err != nil {
log.Fatal(err)
os.Exit(2)
}
}
// Usage displays possible arguments
func Usage() {
flag.PrintDefaults()
os.Exit(1)
}