-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
89 lines (72 loc) · 1.71 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
package main
import (
"fmt"
"log"
"net/http"
"regexp"
"sync"
)
const dayLimit = 1
var matchers = make(map[string]Matcher)
var sites = make([]Site, 0)
var emailRe = regexp.MustCompile(`[\w._]+@\w+\.\w+`)
func init() {
matchers["default"] = DefaultMatcher{}
matchers["cnode/json"] = CNodeJSON{}
matchers["studygolang/html"] = StudyGolangHTML{}
sites = append(sites, Site{url: "https://cnodejs.org/api/v1/topics?tab=job", resType: "cnode/json"})
sites = append(sites, Site{url: "https://studygolang.com/go/jobs", resType: "studygolang/html"})
}
func main() {
results := make(chan *Result)
var wg sync.WaitGroup
wg.Add(len(sites))
for _, site := range sites {
matcher, ok := matchers[site.resType]
if !ok {
matcher = matchers["default"]
}
go func(matcher Matcher, url string) {
err := doMatch(matcher, url, results)
if err != nil {
log.Println(err)
}
wg.Done()
}(matcher, site.url)
}
go func() {
wg.Wait()
close(results)
}()
display(results)
}
func doMatch(matcher Matcher, url string, results chan<- *Result) error {
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return fmt.Errorf("HTTP response error code: %d", resp.StatusCode)
}
searchResults, err := matcher.match(resp.Body)
if err != nil {
return err
}
for _, result := range searchResults {
results <- result
}
return nil
}
func display(results chan *Result) {
for result := range results {
var shorten string
if len(result.content) > 140 {
s := []rune(result.content)
shorten = string(s[0:140])
} else {
shorten = result.content
}
log.Printf("标题:%s\n邮箱:%s\n正文:%s\n\n", result.title, result.email, shorten)
}
}