This repository was archived by the owner on Aug 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.go
More file actions
147 lines (117 loc) · 3.25 KB
/
parser.go
File metadata and controls
147 lines (117 loc) · 3.25 KB
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
package parser
import (
"fmt"
"github.com/PuerkitoBio/goquery"
)
// Update the default metadata based on the type of tag.
func (m *MetaData) setValueByType(tag, value, domain string) {
switch tag {
case "og:site_name":
m.SiteName = setValue(value, domain)
case "og:type":
m.SiteType = setValue(value, "website")
}
}
// Set default values based on the link provided.
func (m *MetaData) setDefaultValue(linkData *Link, title string) {
m.Title = title
m.URL = linkData.URL
m.Domain = linkData.Domain
m.SiteName = linkData.Domain
m.SiteType = "website"
}
// Generate metadata from the page.
func (m *MetaData) generateMetaData(doc *goquery.Document, linkData *Link) {
title, description := getTitleAndDescription(doc)
// Set the default values.
m.setDefaultValue(linkData, title)
// Set the description.
m.Description = description
// Find the favicons.
m.Favicons = getFavicons(doc, linkData.URL)
// Set the images.
m.Images = getImages(doc, linkData.URL)
// Rest of the meta tags.
tagsRemaining := []string{
"og:type",
"og:site_name",
}
for _, tag := range tagsRemaining {
value := getMetaContent(doc, tag)
m.setValueByType(tag, value, linkData.Domain)
}
}
// Get the favicons from the page.
func getFavicons(doc *goquery.Document, link string) []string {
var favicons []string
baseURL := getBaseURL(link)
iconSelectors := []string{
"icon",
"mask-icon",
"shortcut icon",
"apple-touch-icon",
}
// Parse the doc for the icons.
for _, iconSelector := range iconSelectors {
doc.Find(fmt.Sprintf("link[rel='%s']", iconSelector)).
Each(func(i int, s *goquery.Selection) {
icon := s.AttrOr("href", "")
if icon != "" {
favicons = append(favicons, resolveURL(baseURL, icon))
}
})
}
return removeDuplicates(favicons)
}
// Get a meta tag content by property name.
// Example:
//
// image := getMetaContent(doc, "og:image").
func getMetaContent(doc *goquery.Document, data string) string {
// Find tag content by property.
property := doc.Find(fmt.Sprintf("meta[property='%s']", data)).AttrOr("content", "")
if property != "" {
return property
}
// Find tag content by name.
return doc.Find(fmt.Sprintf("meta[name='%s']", data)).AttrOr("content", "")
}
// Get title and description from the page.
func getTitleAndDescription(doc *goquery.Document) (t, d string) {
// Default.
title := getMetaContent(doc, "og:title")
desc := getMetaContent(doc, "og:description")
// Fallbacks.
if title == "" {
title = doc.Find("title").Text()
}
if desc == "" {
desc = getMetaContent(doc, "description")
}
return title, desc
}
// Get images from the page.
func getImages(doc *goquery.Document, link string) []string {
var images []string
baseURL := getBaseURL(link)
options := []string{
"og:image",
"twitter:image",
"msapplication-TileImage",
}
defaultImage := doc.Find(fmt.Sprintf("link[rel='%s']", "image_src")).AttrOr("href", "")
resolvedImages := []string{
resolveURL(baseURL, defaultImage),
}
for _, option := range options {
image := getMetaContent(doc, option)
resolvedImages = append(resolvedImages, resolveURL(baseURL, image))
}
// Only add non-empty images.
for _, image := range resolvedImages {
if image != "" {
images = append(images, image)
}
}
return removeDuplicates(images)
}