forked from danielfireman/remuneracao-justica-crawler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
71 lines (65 loc) · 1.48 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
package main
import (
"bufio"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
"github.com/PuerkitoBio/goquery"
)
var (
pairsFlag = flag.String("p", "", "")
out = flag.String("out", "", "")
)
const (
basePath = "http://www.cnj.jus.br"
remuneracaoPath = "http://www.cnj.jus.br/transparencia/remuneracao-dos-magistrados/remuneracao-"
)
func main() {
flag.Parse()
pairs := strings.Split(*pairsFlag, ",")
var wg sync.WaitGroup
for _, p := range pairs {
remuneracaoURL := fmt.Sprintf("%s%s", remuneracaoPath, p)
doc, err := goquery.NewDocument(remuneracaoURL)
if err != nil {
log.Fatal(err)
}
doc.Find("td").Each(func(index int, item *goquery.Selection) {
linkTag := item.Find("a")
link, _ := linkTag.Attr("href")
if strings.HasSuffix(link, "xls") || strings.HasSuffix(link, "xlsx") {
wg.Add(1)
go func() {
defer wg.Done()
dLink := fmt.Sprintf("%s%s", basePath, link)
resp, err := http.Get(dLink)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
outPath := filepath.Join(*out, fmt.Sprintf("%s-%s", p, filepath.Base(dLink)))
f, err := os.Create(outPath)
defer f.Close()
if err != nil {
log.Fatal(err)
}
w := bufio.NewWriter(f)
w.Write(b)
w.Flush()
fmt.Printf("%s downloaded to %s\n", dLink, outPath)
}()
}
})
}
wg.Wait()
}