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

View File

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

View File

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