Permalink
Browse files

Title parsing

  • Loading branch information...
k4k committed Sep 14, 2017
0 parents commit da2fe99cccdac5dc62c350aecbfbfbe4c31aa967
Showing with 54 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +3 −0 README.md
  3. +50 −0 main.go
@@ -0,0 +1 @@
webby
@@ -0,0 +1,3 @@
# Webby Bot
Webby is an IRC bot written in Go. The initial goal is to provide helpers for
parsing URLs to provide the URL title and a shortend URL.
50 main.go
@@ -0,0 +1,50 @@
package main
import (
"io"
"net/http"
"golang.org/x/net/html"
)
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() {
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")
}
}

0 comments on commit da2fe99

Please sign in to comment.