/
website.go
166 lines (150 loc) · 3.84 KB
/
website.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package website
import (
"bytes"
"github.com/PuerkitoBio/goquery"
"github.com/endeveit/guesslanguage"
"github.com/golibri/fetch"
"regexp"
"strings"
)
// Website contains all relevant metadata from a HTML web page
type Website struct {
URL string
Body string
Language string
Title string
Description string
Image string
Favicon string
Feeds []string
Tags []string
}
// Parse executes a given HTML string and transforms it into a Website{} struct
func Parse(s string) (Website, error) {
doc := docFromString(s)
w := Website{Body: s}
w.Title = titleFromDoc(&doc)
w.Description = descriptionFromDoc(&doc)
w.Image = imageFromDoc(&doc)
w.Favicon = faviconFromDoc(&doc)
w.Feeds = feedsFromDoc(&doc, s)
w.Tags = tagsFromDoc(&doc)
w.URL = canonicalFromDoc(&doc)
w.Language = detectLanguage(doc.Find("body").Text())
return w, nil
}
// FromURL parses a Website directly from a given URL
func FromURL(URL string) (Website, error) {
page, err := fetch.Get(URL)
if err != nil {
return Website{}, err
}
w, err := Parse(page.Body)
if err != nil {
return Website{}, err
}
return w, nil
}
func detectLanguage(str string) string {
lang, err := guesslanguage.Guess(str)
if err != nil {
lang = "en"
}
return lang
}
func docFromString(str string) goquery.Document {
buf := bytes.NewBuffer(nil)
buf.WriteString(str)
doc, err := goquery.NewDocumentFromReader(buf)
if err != nil {
return goquery.Document{}
}
return *doc
}
func titleFromDoc(doc *goquery.Document) string {
title := doc.Find("title,h1,h2,h3,h4,p,div,body").First().Text()
rx := regexp.MustCompile(`\s[|-].{1}.+$`)
if rx.MatchString(title) {
i := rx.FindStringIndex(title)[0]
title = title[:i]
}
return title
}
func descriptionFromDoc(doc *goquery.Document) string {
sel := "meta[property='og:description']"
sel += ", meta[name='twitter:description']"
sel += ", meta[name='description']"
desc, _ := doc.Find(sel).First().Attr("content")
return desc
}
func imageFromDoc(doc *goquery.Document) string {
sel := "meta[property='og:image']"
sel += ", meta[name='twitter:image']"
img, _ := doc.Find(sel).First().Attr("content")
return img
}
func faviconFromDoc(doc *goquery.Document) string {
sel := "link[rel='apple-touch-icon']"
sel += ", link[rel='apple-touch-icon-precomposed']"
sel += ", link[rel='shortcut icon']"
sel += ", link[rel='icon']"
fav, ok := doc.Find(sel).First().Attr("href")
if !ok {
sel = "meta[name='msapplication-TileImage']"
ms, _ := doc.Find(sel).First().Attr("content")
fav = ms
}
return fav
}
func feedsFromDoc(doc *goquery.Document, text string) []string {
sel := "link[type='application/rss+xml']"
sel += ", link[type='application/atom+xml']"
matches := doc.Find(sel)
if matches.Length() > 0 {
feeds := make([]string, matches.Length())
matches.Each(func(i int, s *goquery.Selection) {
url, _ := s.Attr("href")
feeds[i] = url
})
return feeds
}
rx := regexp.MustCompile(`href=['"]([^'"]*(rss|atom|feed|xml)[^'"]*)['"]`)
if rx.FindString(text) != "" {
matches := rx.FindAllStringSubmatch(text, -1)
feeds := make([]string, len(matches))
for i, e := range matches {
feeds[i] = e[1]
}
return feeds
}
return make([]string, 0)
}
func tagsFromDoc(doc *goquery.Document) []string {
sel := "meta[name=keywords]"
str, ok := doc.Find(sel).First().Attr("content")
if !ok {
return []string{}
}
str = strings.Replace(str, "|", ";", -1)
str = strings.Replace(str, ",", ";", -1)
if strings.Contains(str, ";") {
list := strings.Split(str, ";")
tags := make([]string, len(list))
for i, e := range list {
tags[i] = strings.Trim(e, " ")
}
return tags
}
tags := []string{str}
return tags
}
func canonicalFromDoc(doc *goquery.Document) string {
str, ok := doc.Find("link[rel=canonical]").First().Attr("href")
if !ok {
return ""
}
if len(str) > 3 {
return str
}
return ""
}