moved file based functions into new file fileActions.go

This commit is contained in:
Dom 2019-08-11 22:24:14 +02:00
parent 3af92fa056
commit 2a776d5c5f
2 changed files with 147 additions and 139 deletions

147
fileActions.go Normal file
View File

@ -0,0 +1,147 @@
package paxan
import (
"encoding/csv"
"log"
"net/http"
"time"
"golang.org/x/text/encoding/charmap"
)
// SpricelistEntry - One entry in the spricelist
type SpricelistEntry struct {
Sortiment string
Warengruppe string
Untergruppe string
Artnr int
Itemname1 string
Itemname2 string
Hersteller string
Qualitaet string
Herkunft string
Einheit string
Aktion bool
Menge1 float64
Einzelpreis1 float64
Vpepreis1 float64
Menge2 string
Einzelpreis2 string
Vpepreis2 string
Menge3 string
Einzelpreis3 string
Vpepreis3 string
Menge4 string
Einzelpreis4 string
Vpepreis4 string
Empfvk float64
Mwst float64
Ean string
Glutenfrei bool
Laktosefrei bool
Vegan bool
Regional bool
Fairtrade bool
}
// DownloadPricelists - Download both price lists
func DownloadPricelists(client *http.Client) error {
// Download preisliste
r, err := client.Get("https://www.hakopaxan-shop.de/csv/preisliste.csv")
if err != nil {
return err
}
downloadFile(("preisliste-" + time.Now().Format("20060102150405") + ".csv"), r)
// Download spreisliste
r, err = client.Get("https://www.hakopaxan-shop.de/csv/spreisliste.csv")
if err != nil {
return err
}
downloadFile(("spreisliste-" + time.Now().Format("20060102150405") + ".csv"), r)
return nil
}
// DownloadBnnlists - Download both price lists. The lists are public so we do not necessarily login before downloading.
func DownloadBnnlists(client *http.Client) error {
// Download PL.BNN
r, err := client.Get("https://static.paxan.de/2016/PL.BNN")
if err != nil {
return err
}
downloadFile(("PL-" + time.Now().Format("20060102150405") + ".BNN"), r)
// Download PLF.BNN
r, err = client.Get("https://static.paxan.de/2016/PLF.BNN")
if err != nil {
return err
}
downloadFile(("PLF-" + time.Now().Format("20060102150405") + ".BNN"), r)
return nil
}
// RetrievePricelist - Downloads and converts pricelist into an array
func RetrievePricelist(client *http.Client) ([]SpricelistEntry, error) {
// Download spreisliste
resp, err := client.Get("https://www.hakopaxan-shop.de/csv/spreisliste.csv")
if err != nil {
return nil, err
}
dr := charmap.ISO8859_1.NewDecoder().Reader(resp.Body)
r := csv.NewReader(dr)
r.Comma = ';'
records, err := r.ReadAll()
if err != nil {
log.Fatal(err)
}
var priceList []SpricelistEntry
for i, line := range records {
if i == 0 {
continue
}
var priceListEntry SpricelistEntry
priceListEntry.Sortiment = line[0]
priceListEntry.Warengruppe = line[1]
priceListEntry.Untergruppe = line[2]
priceListEntry.Artnr = convertStringToInt(line[3])
priceListEntry.Itemname1 = line[4]
priceListEntry.Itemname2 = line[5]
priceListEntry.Hersteller = line[6]
priceListEntry.Qualitaet = line[7]
priceListEntry.Herkunft = line[8]
priceListEntry.Einheit = line[9]
priceListEntry.Aktion = convertStringToBool(line[10])
priceListEntry.Menge1 = convertStringToFloat(line[11])
priceListEntry.Einzelpreis1 = convertStringToFloat(line[12])
priceListEntry.Vpepreis1 = convertStringToFloat(line[13])
priceListEntry.Menge2 = line[14]
priceListEntry.Einzelpreis2 = line[15]
priceListEntry.Vpepreis2 = line[16]
priceListEntry.Menge3 = line[17]
priceListEntry.Einzelpreis3 = line[18]
priceListEntry.Vpepreis3 = line[19]
priceListEntry.Menge4 = line[20]
priceListEntry.Einzelpreis4 = line[21]
priceListEntry.Vpepreis4 = line[22]
priceListEntry.Empfvk = convertStringToFloat(line[23])
priceListEntry.Mwst = convertStringToFloat(line[24])
priceListEntry.Ean = line[25]
priceListEntry.Glutenfrei = convertStringToBool(line[26])
priceListEntry.Laktosefrei = convertStringToBool(line[27])
priceListEntry.Vegan = convertStringToBool(line[28])
priceListEntry.Regional = convertStringToBool(line[29])
priceListEntry.Fairtrade = convertStringToBool(line[30])
priceList = append(priceList, priceListEntry)
}
return priceList, nil
}

View File

@ -1,7 +1,6 @@
package paxan
import (
"encoding/csv"
"io"
"log"
"net/http"
@ -13,7 +12,6 @@ import (
"time"
"github.com/PuerkitoBio/goquery"
"golang.org/x/text/encoding/charmap"
)
type paxanWebOrder struct {
@ -35,41 +33,6 @@ type paxanWebOrderEntry struct {
Total2 string
}
// SpricelistEntry - One entry in the spricelist
type SpricelistEntry struct {
Sortiment string
Warengruppe string
Untergruppe string
Artnr int
Itemname1 string
Itemname2 string
Hersteller string
Qualitaet string
Herkunft string
Einheit string
Aktion bool
Menge1 float64
Einzelpreis1 float64
Vpepreis1 float64
Menge2 string
Einzelpreis2 string
Vpepreis2 string
Menge3 string
Einzelpreis3 string
Vpepreis3 string
Menge4 string
Einzelpreis4 string
Vpepreis4 string
Empfvk float64
Mwst float64
Ean string
Glutenfrei bool
Laktosefrei bool
Vegan bool
Regional bool
Fairtrade bool
}
func convertStringToBool(value string) bool {
if value == "X" {
return true
@ -149,108 +112,6 @@ func CloseClient(client *http.Client) error {
return nil
}
// DownloadPricelists - Download both price lists
func DownloadPricelists(client *http.Client) error {
// Download preisliste
r, err := client.Get("https://www.hakopaxan-shop.de/csv/preisliste.csv")
if err != nil {
return err
}
downloadFile(("preisliste-" + time.Now().Format("20060102150405") + ".csv"), r)
// Download spreisliste
r, err = client.Get("https://www.hakopaxan-shop.de/csv/spreisliste.csv")
if err != nil {
return err
}
downloadFile(("spreisliste-" + time.Now().Format("20060102150405") + ".csv"), r)
return nil
}
// DownloadBnnlists - Download both price lists. The lists are public so we do not necessarily login before downloading.
func DownloadBnnlists(client *http.Client) error {
// Download PL.BNN
r, err := client.Get("https://static.paxan.de/2016/PL.BNN")
if err != nil {
return err
}
downloadFile(("PL-" + time.Now().Format("20060102150405") + ".BNN"), r)
// Download PLF.BNN
r, err = client.Get("https://static.paxan.de/2016/PLF.BNN")
if err != nil {
return err
}
downloadFile(("PLF-" + time.Now().Format("20060102150405") + ".BNN"), r)
return nil
}
// RetrievePricelist - Downloads and converts pricelist into an array
func RetrievePricelist(client *http.Client) ([]SpricelistEntry, error) {
// Download spreisliste
resp, err := client.Get("https://www.hakopaxan-shop.de/csv/spreisliste.csv")
if err != nil {
return nil, err
}
dr := charmap.ISO8859_1.NewDecoder().Reader(resp.Body)
r := csv.NewReader(dr)
r.Comma = ';'
records, err := r.ReadAll()
if err != nil {
log.Fatal(err)
}
var priceList []SpricelistEntry
for i, line := range records {
if i == 0 {
continue
}
var priceListEntry SpricelistEntry
priceListEntry.Sortiment = line[0]
priceListEntry.Warengruppe = line[1]
priceListEntry.Untergruppe = line[2]
priceListEntry.Artnr = convertStringToInt(line[3])
priceListEntry.Itemname1 = line[4]
priceListEntry.Itemname2 = line[5]
priceListEntry.Hersteller = line[6]
priceListEntry.Qualitaet = line[7]
priceListEntry.Herkunft = line[8]
priceListEntry.Einheit = line[9]
priceListEntry.Aktion = convertStringToBool(line[10])
priceListEntry.Menge1 = convertStringToFloat(line[11])
priceListEntry.Einzelpreis1 = convertStringToFloat(line[12])
priceListEntry.Vpepreis1 = convertStringToFloat(line[13])
priceListEntry.Menge2 = line[14]
priceListEntry.Einzelpreis2 = line[15]
priceListEntry.Vpepreis2 = line[16]
priceListEntry.Menge3 = line[17]
priceListEntry.Einzelpreis3 = line[18]
priceListEntry.Vpepreis3 = line[19]
priceListEntry.Menge4 = line[20]
priceListEntry.Einzelpreis4 = line[21]
priceListEntry.Vpepreis4 = line[22]
priceListEntry.Empfvk = convertStringToFloat(line[23])
priceListEntry.Mwst = convertStringToFloat(line[24])
priceListEntry.Ean = line[25]
priceListEntry.Glutenfrei = convertStringToBool(line[26])
priceListEntry.Laktosefrei = convertStringToBool(line[27])
priceListEntry.Vegan = convertStringToBool(line[28])
priceListEntry.Regional = convertStringToBool(line[29])
priceListEntry.Fairtrade = convertStringToBool(line[30])
priceList = append(priceList, priceListEntry)
}
return priceList, nil
}
func paxanGetOrders(client *http.Client) ([]paxanWebOrder, error) {
var list []paxanWebOrder
kvs := make(map[string]string)