-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
executable file
·57 lines (50 loc) · 969 Bytes
/
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
package main
import (
"fmt"
"io"
"net/http"
"os"
"strings"
"github.com/PuerkitoBio/goquery"
)
func main() {
if len(os.Args) < 2 {
process("https://yle.fi/selkouutiset")
} else {
for _, target := range os.Args[1:] {
process(target)
}
}
}
func process(target string) {
fmt.Println(target)
var in io.ReadCloser
var err error
if strings.HasPrefix(target, "https://") {
resp, err := http.Get(target)
if err != nil {
panic(err)
}
in = resp.Body
} else {
in, err = os.Open(target)
if err != nil {
panic(err)
}
}
doc, err := goquery.NewDocumentFromReader(in)
if err != nil {
panic(err)
}
in.Close()
article := article{}
article.fillDefaults()
doc.Find("article.yle__article").Find("h1, h2, h3, p").Each(func(i int, s *goquery.Selection) {
if goquery.NodeName(s) == "p" && goquery.NodeName(s.Parent()) == "figure" {
// ignore
} else {
article.handle(goquery.NodeName(s), s.Text())
}
})
article.close()
}