-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgetpipo.go
42 lines (36 loc) · 1.02 KB
/
getpipo.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
package main
import (
"net/http"
"golang.org/x/net/html"
"strings"
)
func GetPipo() (string) {
// Get the pipo a la con (theme it) page into resp
resp, err := http.Get("http://pipo.alacon.org/?pipo=flanc&theme=it")
if err != nil {
return "Could not fetch HTML page"
}
// Parse Page body and place into doc
doc, err := html.Parse(resp.Body)
if err != nil {
return "Could not parse HTML page"
}
slice := []string{}
var f func(*html.Node, bool)
// Look for for type ElementNode get Text if data is h2 and append it in slice
f = func(n *html.Node, IsText bool) {
if IsText && n.Type == html.TextNode {
slice = append(slice, string(n.Data))
}
IsText = IsText || (n.Type == html.ElementNode && n.Data == "h2")
for c := n.FirstChild; c != nil; c = c.NextSibling {
f(c, IsText)
}
}
f(doc, false)
var pipo string
// do some dirty manipulations to just have the sentence
pipo = strings.Join(slice[1:], " ")
pipo = strings.Replace(pipo, "\n", "", -1)
return pipo
}