summaryrefslogtreecommitdiff
path: root/href.go
blob: 6dc72d9ffa6c2fa159ddf8556706431da0870c30 (plain)
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
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
}