-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser_og.go
126 lines (117 loc) · 2.77 KB
/
parser_og.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
package linkpreview
import "strings"
func (ctx *ParserContext) parseOGMeta() {
if !ctx.Config.ParseOGMeta {
return
}
ogMeta := &OGMeta{}
var imageTags []*MetaTag
var videoTags []*MetaTag
for _, tag := range ctx.MetaTags {
tagProp := tag.Property
if !strings.HasPrefix(tagProp, "og:") {
continue
}
tagContent := tag.Content
parsed := true
switch tagProp {
case "og:url":
ogMeta.URL = tagContent
case "og:title":
ogMeta.Title = tagContent
case "og:type":
ogMeta.Type = tagContent
case "og:description":
ogMeta.Description = tagContent
case "og:site_name":
ogMeta.SiteName = tagContent
case "og:locale":
ogMeta.Locale = tagContent
default:
parsed = false
}
if parsed {
continue
}
if strings.HasPrefix(tagProp, "og:image") {
imageTags = append(imageTags, tag)
} else if strings.HasPrefix(tagProp, "og:video") {
videoTags = append(videoTags, tag)
} else {
ogMeta.Others = append(ogMeta.Others, tag)
}
}
if len(imageTags) > 0 {
ctx.parseOGImages(ogMeta, imageTags)
}
if len(videoTags) > 0 {
ctx.parseOGVideos(ogMeta, videoTags)
}
ctx.Result.OGMeta = ogMeta
}
func (ctx *ParserContext) parseOGImages(ogMeta *OGMeta, tags []*MetaTag) {
var currImage *OGImage
for _, tag := range tags {
switch tag.Property {
case "og:image":
if currImage != nil {
ogMeta.Images = append(ogMeta.Images, currImage)
}
currImage = &OGImage{URL: parseURL(tag.Content, ctx.Link)}
case "og:image:url", "og:image:secure_url":
if currImage != nil {
currImage.URL = tag.Content
}
case "og:image:width":
if currImage != nil {
currImage.Width = parseInt(tag.Content)
}
case "og:image:height":
if currImage != nil {
currImage.Height = parseInt(tag.Content)
}
case "og:image:type":
if currImage != nil {
currImage.Type = tag.Content
}
case "og:image:alt":
if currImage != nil {
currImage.Alt = tag.Content
}
}
}
if currImage != nil {
ogMeta.Images = append(ogMeta.Images, currImage)
}
}
func (ctx *ParserContext) parseOGVideos(ogMeta *OGMeta, tags []*MetaTag) {
var currVideo *OGVideo
for _, tag := range tags {
switch tag.Property {
case "og:video":
if currVideo != nil {
ogMeta.Videos = append(ogMeta.Videos, currVideo)
}
currVideo = &OGVideo{URL: parseURL(tag.Content, ctx.Link)}
case "og:video:url", "og:video:secure_url":
if currVideo != nil {
currVideo.URL = tag.Content
}
case "og:video:width":
if currVideo != nil {
currVideo.Width = parseInt(tag.Content)
}
case "og:video:height":
if currVideo != nil {
currVideo.Height = parseInt(tag.Content)
}
case "og:video:type":
if currVideo != nil {
currVideo.Type = tag.Content
}
}
}
if currVideo != nil {
ogMeta.Videos = append(ogMeta.Videos, currVideo)
}
}