-
Notifications
You must be signed in to change notification settings - Fork 0
/
GetTitle.go
62 lines (54 loc) · 1.78 KB
/
GetTitle.go
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
62
package katsuragi
import (
"fmt"
"golang.org/x/net/html"
)
func (f *Fetcher) GetTitle(url string) (string, error) {
html, err := retrieveHTML(url, f)
if err != nil {
return "", err
}
title, found := traverseAndExtractTitle(html)
if !found {
return "", fmt.Errorf("GetTitle failed to find title in HTML")
}
return title, nil
}
// Valid tags
var validTitleTags = map[string]bool{
"title": true,
}
// Valid meta tags
var validTitleMeta = map[string]bool{
"title": true,
"twitter:title": true,
"og:title": true,
}
// traverseAndExtractTitle traverses the HTML node tree and extracts the title of the webpage
func traverseAndExtractTitle(n *html.Node) (string, bool) {
if n.Type == html.ElementNode {
if validTitleTags[n.Data] && n.Parent != nil && n.Parent.Data == "head" {
if n.FirstChild != nil {
// If the <title> tag has a child node, return the data of the child node
return n.FirstChild.Data, true
}
} else if n.Data == "meta" {
attrMap := extractAttributes(n.Attr) // Extract attributes to map
if name, found := attrMap["name"]; found && validTitleMeta[name] {
if content, found := attrMap["content"]; found && content != "" {
return content, true
}
} else if property, found := attrMap["property"]; found && validTitleMeta[property] {
if content, found := attrMap["content"]; found && content != "" {
return content, true
}
}
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
if title, found := traverseAndExtractTitle(c); found {
return title, true
}
}
return "", false
}