-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
113 lines (89 loc) · 1.98 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/gocolly/colly"
)
type Record struct {
Title string `json:"title"`
Link string `json:"link"`
}
var MONTH_NAME_TO_EN = map[string]string{
"Janeiro": "January",
"Fevereiro": "February",
"Março": "March",
"Abril": "April",
"Maio": "May",
"Junho": "June",
"Julho": "July",
"Agosto": "August",
"Setembro": "September",
"Outubro": "October",
"Novembro": "November",
"Dezembro": "December",
}
func isNewPost(date string, now time.Time) bool {
parts := strings.Split(date, " / ")
year, err := strconv.Atoi(parts[2])
if err != nil {
panic("failed to parse year")
}
if year < now.Year() {
return false
}
if MONTH_NAME_TO_EN[parts[1]] != now.Month().String() {
return false
}
day, err := strconv.Atoi(parts[0])
if err != nil {
panic("failed to parse day")
}
return day == now.Day()
}
func crawl(results chan Record) {
collector := colly.NewCollector(colly.AllowedDomains("www.cargadetrabalhos.net", "cargadetrabalhos.net"))
now := time.Now()
abort := false
collector.OnHTML("div.entrycontent", func(e *colly.HTMLElement) {
if abort {
return
}
e.ForEach("span.date", func(i int, h *colly.HTMLElement) {
if !isNewPost(h.Text, now) {
abort = true
}
})
if abort {
fmt.Println("aborting")
return
}
e.ForEach("h2", func(i int, h *colly.HTMLElement) {
if abort {
fmt.Println("aborting")
return
}
fmt.Println("got title", h.Text)
results <- Record{
Title: h.Text,
Link: h.ChildAttr("a", "href"),
}
})
})
collector.OnScraped(func(r *colly.Response) {
fmt.Println("on scraped")
close(results)
})
collector.OnRequest(func(r *colly.Request) {
fmt.Println("Visiting", r.URL)
})
collector.Visit("https://www.cargadetrabalhos.net/category/web-design-programacao/")
}
func main() {
results := make(chan Record)
go crawl(results)
for record := range results {
fmt.Printf("Received record: %+v\n", record)
}
}