-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
73 lines (63 loc) · 2 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"fmt"
"log"
"os"
"strconv"
"strings"
"time"
)
type confSpec struct {
Month string
}
const (
defaultGeneralTimeout = 6 * time.Minute // Duração máxima total da coleta de todos os arquivos. Valor padrão calculado a partir de uma média de execuções ~4.5min
defaulTimeBetweenSteps = 15 * time.Second //Tempo de espera entre passos do coletor."
)
func main() {
if _, err := strconv.Atoi(os.Getenv("MONTH")); err != nil {
log.Fatalf("Invalid month (\"%s\"): %q", os.Getenv("MONTH"), err)
}
month := os.Getenv("MONTH")
if _, err := strconv.Atoi(os.Getenv("YEAR")); err != nil {
log.Fatalf("Invalid year (\"%s\"): %q", os.Getenv("YEAR"), err)
}
year := os.Getenv("YEAR")
outputFolder := os.Getenv("OUTPUT_FOLDER")
if outputFolder == "" {
outputFolder = "/output"
}
if err := os.Mkdir(outputFolder, os.ModePerm); err != nil && !os.IsExist(err) {
log.Fatalf("Error creating output folder(%s): %q", outputFolder, err)
}
generalTimeout := defaultGeneralTimeout
if os.Getenv("GENERAL_TIMEOUT") != "" {
var err error
generalTimeout, err = time.ParseDuration(os.Getenv("GENERAL_TIMEOUT"))
if err != nil {
log.Fatalf("Invalid GENERAL_TIMEOUT (\"%s\"): %q", os.Getenv("GENERAL_TIMEOUT"), err)
}
}
timeBetweenSteps := defaulTimeBetweenSteps
if os.Getenv("TIME_BETWEEN_STEPS") != "" {
var err error
timeBetweenSteps, err = time.ParseDuration(os.Getenv("TIME_BETWEEN_STEPS"))
if err != nil {
log.Fatalf("Invalid TIME_BETWEEN_STEPS (\"%s\"): %q", os.Getenv("TIME_BETWEEN_STEPS"), err)
}
}
c := crawler{
collectionTimeout: generalTimeout,
timeBetweenSteps: timeBetweenSteps,
year: year,
month: month,
output: outputFolder,
}
downloads, err := c.crawl()
if err != nil {
log.Fatalf("Error crawling (%s, %s, %s): %v", year, month, outputFolder, err)
}
// O parser do MPSC espera os arquivos separados por \n. Mudanças aqui tem que
// refletir as expectativas lá.
fmt.Println(strings.Join(downloads, "\n"))
}