fattureDownloader: added validation check on downloaded pdfs. Resolved downloadedFiles slice problems when errors on files.

Signed-off-by: Ettore Dreucci <ettore.dreucci@gmail.com>
This commit is contained in:
2020-11-18 18:39:06 +01:00
parent 7bf59e4234
commit d986e240af

View File

@@ -10,6 +10,7 @@ import (
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"runtime" "runtime"
"sort"
"strings" "strings"
"sync" "sync"
@@ -40,9 +41,9 @@ func getInvoiceIDs(fileName string) []string {
var invoiceIDs []string var invoiceIDs []string
for i := 4; i <= int(sheet.MaxRow); i++ { for i := 0; i <= int(sheet.MaxRow); i++ {
row := sheet.Row(i) row := sheet.Row(i)
if row.Col(8) != "" { if strings.Contains(row.Col(8), "CCSR") {
id := strings.ReplaceAll(row.Col(8), "/", "-") id := strings.ReplaceAll(row.Col(8), "/", "-")
invoiceIDs = append(invoiceIDs, id) invoiceIDs = append(invoiceIDs, id)
} }
@@ -51,7 +52,7 @@ func getInvoiceIDs(fileName string) []string {
} }
func convertXLStoFODS(fileName string) string { func convertXLStoFODS(fileName string) string {
var sofficePath string = "libreoffice" var sofficePath = "libreoffice"
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
sofficePath = filepath.FromSlash("C:/Program Files/LibreOffice/program/soffice.exe") sofficePath = filepath.FromSlash("C:/Program Files/LibreOffice/program/soffice.exe")
} }
@@ -157,15 +158,15 @@ func createTmpDir() string {
func downloadInvoices(ids []string, urls []string) []string { func downloadInvoices(ids []string, urls []string) []string {
if len(ids) != len(urls) { if len(ids) != len(urls) {
exitWithError = true exitWithError = true
log.Panicf("Il numero di fatture da scaricare non corrisponde al numero di URL individuati nel file") log.Printf("Il numero di fatture da scaricare non corrisponde al numero di URL individuati nel file. IDs: %d/URLs: %d", len(ids), len(urls))
} }
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
sem := make(chan bool, 30) sem := make(chan bool, 30)
mu := sync.Mutex{} mu := sync.Mutex{}
invoiceNum := len(ids) invoiceNum := minOf(len(ids), len(urls))
downloadCount := 0 downloadCount := 0
downloadedFiles := make([]string, invoiceNum) downloadedFiles := make([]string, 0)
log.Printf("Inizio il download di %d fatture\n", invoiceNum) log.Printf("Inizio il download di %d fatture\n", invoiceNum)
for i := 0; i < invoiceNum; i++ { for i := 0; i < invoiceNum; i++ {
@@ -180,9 +181,11 @@ func downloadInvoices(ids []string, urls []string) []string {
err := downloadFile(out, url) err := downloadFile(out, url)
if err != nil { if err != nil {
log.Printf("Impossibile scaricare il file %v: %v\n", url, err) log.Printf("Impossibile scaricare il file %v: %v\n", url, err)
} else if api.ValidateFile(out, nil) != nil {
log.Printf("Errore nella validazione del file %v\n", out)
} else { } else {
downloadedFiles[i] = out
mu.Lock() mu.Lock()
downloadedFiles = append(downloadedFiles, out)
downloadCount++ downloadCount++
mu.Unlock() mu.Unlock()
} }
@@ -192,7 +195,8 @@ func downloadInvoices(ids []string, urls []string) []string {
} }
wg.Wait() wg.Wait()
log.Printf("Scaricate %d/%d fatture\n", downloadCount, invoiceNum) log.Printf("Scaricate %d/%d fatture\n", downloadCount, invoiceNum)
return downloadedFiles[:downloadCount] sort.Strings(downloadedFiles)
return downloadedFiles
} }
func mergeInvoices(files []string) string { func mergeInvoices(files []string) string {
@@ -247,6 +251,18 @@ func cleanTmpDir() {
} }
} }
func minOf(vars ...int) int {
min := vars[0]
for _, i := range vars {
if min > i {
min = i
}
}
return min
}
func main() { func main() {
exitWithError = false exitWithError = false
args := os.Args args := os.Args