-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
82 lines (66 loc) · 1.37 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
package main
import (
"bufio"
"os"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/geziyor/geziyor"
"github.com/geziyor/geziyor/client"
"github.com/geziyor/geziyor/export"
)
func main() {
geziyor.NewGeziyor(&geziyor.Options{
StartURLs: loadSources(),
ParseFunc: extractArticles,
Exporters: []export.Exporter{&export.JSONLine{FileName: "out.jsonl"}},
}).Start()
}
func loadSources() []string {
var sources []string
f, err := os.OpenFile("sources.txt", os.O_RDONLY, os.ModePerm)
if err != nil {
panic(err)
}
defer f.Close()
sc := bufio.NewScanner(f)
for sc.Scan() {
s := sc.Text()
if len(s) == 0 {
continue
}
sources = append(sources, s)
}
if err := sc.Err(); err != nil {
panic(err)
}
return sources
}
func extractArticles(g *geziyor.Geziyor, r *client.Response) {
r.HTMLDoc.Find("tr").Each(func(i int, s *goquery.Selection) {
s.Find("td").Each(func(i int, s *goquery.Selection) {
article := s.Find("a")
url, exists := article.Attr("href")
if !exists {
return
}
if !strings.Contains(url, "/handle/") {
return
}
name := article.Text()
if len(name) == 0 {
return
}
var fullURL string
u, err := r.Request.URL.Parse(url)
if err != nil {
fullURL = url
} else {
fullURL = u.String()
}
g.Exports <- map[string]interface{}{
"text": name,
"url": fullURL,
}
})
})
}