From 7b96813780291a8d49b86469c4bd1290b253366d Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Tue, 7 Jul 2015 16:36:33 +0200 Subject: Switch to goirc --- href.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 href.go (limited to 'href.go') diff --git a/href.go b/href.go new file mode 100644 index 0000000..9ba955f --- /dev/null +++ b/href.go @@ -0,0 +1,36 @@ +package main + +import ( + "net/http" + + "golang.org/x/net/html" +) + +func findTitle(n *html.Node) string { + if n.Type == html.ElementNode && n.Data == "title" { + if c := n.FirstChild; n != nil { + return c.Data + } + } + for c := n.FirstChild; c != nil; c = c.NextSibling { + if s := findTitle(c); s != "" { + return s + } + } + return "" +} + +func FetchTitle(url string) (string, error) { + resp, err := http.Get(url) + if err != nil { + return "", err + } + defer resp.Body.Close() + + doc, err := html.Parse(resp.Body) + if err != nil { + return "", err + } + + return findTitle(doc), nil +} -- cgit v1.2.3