-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
73 lines (58 loc) · 1.34 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
package main
import (
"fmt"
"io"
"net/http"
"time"
"github.com/clarkf/libirc"
"golang.org/x/net/html"
)
// Define you server, port and channel here
const server = "irc.freenode.net" // IRC Server
const port = 6667 // IRC Server Port
const channel = "#somechannelhere" // IRC Channel
func ircConnect(svr string, port int, cnl string) {
c := libirc.NewClient("webby", "webby-bot", "Webby Bot")
connErr := c.ConnectAndListen(fmt.Sprintf("%s:%d", svr, port))
if connErr != nil {
panic(connErr)
}
c.Join(cnl)
}
func isTitleElement(n *html.Node) bool {
return n.Type == html.ElementNode && n.Data == "title"
}
func traverse(n *html.Node) (string, bool) {
if isTitleElement(n) {
return n.FirstChild.Data, true
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
result, ok := traverse(c)
if ok {
return result, ok
}
}
return "", false
}
func GetHtmlTitle(r io.Reader) (string, bool) {
doc, docErr := html.Parse(r)
if docErr != nil {
panic("Failed to parse url")
}
return traverse(doc)
}
func main() {
ircConnect(server, port, channel)
time.Sleep(time.Duration(5) * time.Second)
url := "http://google.com"
resp, getErr := http.Get(url)
if getErr != nil {
panic(getErr)
}
defer resp.Body.Close()
if title, ok := GetHtmlTitle(resp.Body); ok {
println(title)
} else {
println("No title found")
}
}