package main import ( "errors" "net/http" "strings" "golang.org/x/net/html" ) var ( notHTML = errors.New("not HTML content") tooBig = errors.New("cotent too big") ) const MB = 1024 * 1024 func findTitle(n *html.Node) (s string) { if n.Type == html.ElementNode && n.Data == "title" { for c := n.FirstChild; c != nil; c = c.NextSibling { s += c.Data } return strings.TrimSpace(s) } for c := n.FirstChild; c != nil; c = c.NextSibling { if t := findTitle(c); t != "" { return t } } return "" } func FetchTitle(url string) (string, error) { resp, err := http.Get(url) if err != nil { return "", err } defer resp.Body.Close() ct := resp.Header.Get("Content-Type") if !strings.HasPrefix(ct, "text/html") { return "", notHTML } if resp.ContentLength > 8*MB { return "", tooBig } doc, err := html.Parse(resp.Body) if err != nil { return "", err } title := findTitle(doc) if len(title) > 80 { title = title[:80] + " ..." } return title, nil }