updates and code cleaned

This commit is contained in:
Paul 2019-07-23 21:49:46 +02:00
parent 25cc6b8313
commit d9d668b5e0
3 changed files with 38 additions and 42 deletions

View File

@ -1,42 +1,37 @@
package main
import (
"flag"
"log"
)
func main() {
var configpath string
var fpc FuelPricesConfig
var zipfile ZipFile
var xmlfile XMLFile
var prices []Price
var err error
var szip Srcfile
var output []byte
flag.StringVar(&configpath, "configfile", "common.ini", "config file to use with fuelprices section")
flag.Parse()
err = GetConfig(configpath, &fpc)
err = fpc.GetConfig()
if err != nil {
log.Fatal(err)
}
err = DownloadFile(&fpc, &szip)
err = DownloadFile(&fpc, &zipfile)
if err != nil {
log.Fatal(err)
}
err = ExtractZip(&fpc, &szip, &output)
err = ExtractZip(&fpc, &zipfile, &xmlfile)
if err != nil {
log.Fatal(err)
}
var prices *[]Price
err = GetPrices(&prices, &fpc, &output)
err = GetPrices(&fpc, &prices, &xmlfile)
if err != nil {
log.Fatal(err)
}
err = SendToInflux(&fpc, prices)
err = SendToInflux(&fpc, &prices)
if err != nil {
log.Fatal(err)
}

View File

@ -20,16 +20,17 @@ import (
)
// GetConfig fetch config from ini file
func GetConfig(configfile string, fuelpricesconfig *FuelPricesConfig) error {
func (fpc *FuelPricesConfig) GetConfig() error {
flag.Usage = Usage
config, err := ini.Load(configfile)
flag.StringVar(&fpc.ConfigPath, "configfile", "common.ini", "config file to use with fuelprices section")
flag.Parse()
config, err := ini.Load(fpc.ConfigPath)
if err != nil {
return err
}
var fpc FuelPricesConfig
fuelpricesSection := config.Section("fuelprices")
fpc.RemoteURL = fuelpricesSection.Key("remote_url").MustString("https://donnees.roulez-eco.fr/opendata/instantane")
fpc.RemoteFilename = fuelpricesSection.Key("remote_filename").MustString("PrixCarburants_instantane.xml")
@ -53,8 +54,6 @@ func GetConfig(configfile string, fuelpricesconfig *FuelPricesConfig) error {
fpc.InfluxPass = influxdbSection.Key("password").MustString("password")
fpc.InfluxDB = influxdbSection.Key("database").MustString("me")
*fuelpricesconfig = fpc
if err != nil {
return err
}
@ -63,21 +62,22 @@ func GetConfig(configfile string, fuelpricesconfig *FuelPricesConfig) error {
}
// DownloadFile fetch file from webserver
func DownloadFile(fpc *FuelPricesConfig, szip *Srcfile) error {
func DownloadFile(fpc *FuelPricesConfig, zipfile *ZipFile) error {
pollTo := 30 * time.Millisecond
c := &http.Client{Timeout: pollTo * time.Second, Transport: &http.Transport{
client := &http.Client{Timeout: pollTo * time.Second, Transport: &http.Transport{
IdleConnTimeout: pollTo,
DisableCompression: false,
}}
resp, err := c.Get(fpc.RemoteURL)
resp, err := client.Get(fpc.RemoteURL)
if err != nil {
return err
}
defer resp.Body.Close()
szip.Content, err = ioutil.ReadAll(resp.Body)
zipfile.Content, err = ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
@ -88,20 +88,19 @@ func DownloadFile(fpc *FuelPricesConfig, szip *Srcfile) error {
}
// ExtractZip get the XML file to be processed
func ExtractZip(fpc *FuelPricesConfig, szip *Srcfile, output *[]byte) error {
zipfile, err := zip.NewReader(bytes.NewReader(szip.Content), int64(len(szip.Content)))
func ExtractZip(fpc *FuelPricesConfig, zipfile *ZipFile, xmlfile *XMLFile) error {
unzipped, err := zip.NewReader(bytes.NewReader(zipfile.Content), int64(len(zipfile.Content)))
if err != nil {
return err
}
for _, f := range zipfile.File {
if f.Name == fpc.RemoteFilename {
rc, err := f.Open()
for _, file := range unzipped.File {
if file.Name == fpc.RemoteFilename {
rc, err := file.Open()
if err != nil {
return err
}
*output, err = ioutil.ReadAll(rc)
xmlfile.Content, err = ioutil.ReadAll(rc)
rc.Close()
} else {
log.Fatal("File not found")
@ -111,13 +110,12 @@ func ExtractZip(fpc *FuelPricesConfig, szip *Srcfile, output *[]byte) error {
}
// GetPrices parses the XML file and get values of prices
func GetPrices(prices **[]Price, fpc *FuelPricesConfig, output *[]byte) error {
var pr []Price
func GetPrices(fpc *FuelPricesConfig, prices *[]Price, xmlfile *XMLFile) error {
var xml *xmlquery.Node
var valueattr = "valeur"
f := bytes.NewReader(*output)
xml, err := xmlquery.Parse(f)
file := bytes.NewReader(xmlfile.Content)
xml, err := xmlquery.Parse(file)
if err != nil {
return err
}
@ -130,12 +128,10 @@ func GetPrices(prices **[]Price, fpc *FuelPricesConfig, output *[]byte) error {
if list != nil {
for _, i := range list.Attr {
if i.Name.Local == "valeur" {
var val float64
if i.Name.Local == valueattr {
if s, err := strconv.ParseFloat(i.Value, 64); err == nil {
val = s
*prices = append(*prices, Price{ID: station, Fuel: fuel, Amount: s})
}
pr = append(pr, Price{ID: station, Fuel: fuel, Amount: val})
}
}
} else {
@ -143,7 +139,6 @@ func GetPrices(prices **[]Price, fpc *FuelPricesConfig, output *[]byte) error {
}
}
}
*prices = &pr
return err
}

View File

@ -1,14 +1,20 @@
package main
// Srcfile source file
type Srcfile struct {
// ZipFile source zipped file
type ZipFile struct {
Filename string
Filepath string
Content []byte
}
// XMLFile contains prices
type XMLFile struct {
Content []byte
}
// FuelPricesConfig is the main configuration
type FuelPricesConfig struct {
ConfigPath string
RemoteURL string
RemoteFilename string
XPathBase string