Skip to content

Commit

Permalink
Title parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
tedwardd committed Sep 14, 2017
0 parents commit da2fe99
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
webby
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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 changes: 50 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -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.