-
Notifications
You must be signed in to change notification settings - Fork 0
/
video.go
285 lines (243 loc) · 7.87 KB
/
video.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package sitemap
// sitemap formatting with syntactic sugar. © Arthur Mingard 2022
// See https://developers.google.com/search/docs/crawling-indexing/sitemaps/video-sitemaps
import (
"encoding/xml"
"strings"
"time"
)
const (
// TagLimit is the number of permitted video tags.
TagLimit = 32
// MaxDescriptionLength is the maximum length of the video:description.
MaxDescriptionLength = 2048
// DurationMin is the minimum video duration of 1 second.
DurationMin int = 1
// DurationMax is the maximum video duration length in seconds (8 hours).
DurationMax int = 28800
// RatingLow is the lowest posible rating.
RatingLow float32 = 0.0
// RatingHigh is the highest posible rating.
RatingHigh float32 = 5.0
)
// Restriction stores country restriction details
type Restriction struct {
XMLName xml.Name `xml:"video:restriction,omitempty"`
Relationship *xml.Attr `xml:",attr,omitempty"`
Value string `xml:",chardata"`
}
// Uploader stores the uploader of the video.
type Uploader struct {
XMLName xml.Name `xml:"video:uploader,omitempty"`
// Info (optional) specifies the URL of a webpage with additional information about this uploader.
Info string `xml:"info,attr,omitempty"`
// Value is the video uploader's name, a string with a maximum of 255 characters.
Value string `xml:",chardata"`
}
// Video stores video entry data.
type Video struct {
XMLName xml.Name `xml:"video:video"`
// Required
// Title HTML entities must be escaped or wrapped in a CDATA block.
Title string `xml:"video:title"`
// Description Maximum {{MaxDescriptionLength}} characters. All HTML entities must be escaped or wrapped in a CDATA block. It must match the description displayed on the web page (it doesn't need to be a word-for-word match).
Description string `xml:"video:description,omitempty"`
ThumbnailLoc string `xml:"video:thumbnail_loc,omitempty"`
// ContentLoc HTML and Flash aren't supported formats.
ContentLoc string `xml:"video:content_loc,omitempty"`
// Can be used instead of or alongside ContentLoc.
PlayerLoc string `xml:"video:player_loc,omitempty"`
// Recommended
// Duration value must be from {{DurationMin}} to {{DurationMax}} inclusive.
Duration int `xml:"video:duration,omitempty"`
// ExpirationDate format either YYYY-MM-DD or YYYY-MM-DDThh:mm:ss+TZD
ExpirationDate time.Time `xml:"video:expiration_date,omitempty"`
// Optional
// Rating values are float numbers in the range {{RatingLow}} (low) to {{RatingHigh}} (high), inclusive.
Rating float32 `xml:"video:rating,omitempty"`
ViewCount int `xml:"video:view_count,omitempty"`
PublicationDate time.Time `xml:"video:publication_date,omitempty"`
// FamilyFriendly whether the video is available with SafeSearch.
FamilyFriendly BoolStr `xml:"video:family_friendly,omitempty"`
Restrictions []*Restriction `xml:"video:restriction,omitempty"`
Platforms []*Platform `xml:"video:platform,omitempty"`
// RequiresSubscription indicates whether a subscription is required to view the video.
RequiresSubscription BoolStr `xml:"video:requires_subscription,omitempty"`
Uploader *Uploader `xml:"video:uploader,omitempty"`
// Live indicates whether the video is a live stream
Live BoolStr `xml:"video:live,omitempty"`
// Tags are limited to a max of {{TagLimit}}.
Tags []string `xml:"video:tag,omitempty"`
}
// SetTitle sets the video extensions title parameter.
func (v *Video) SetTitle(t string) *Video {
v.Title = t
return v
}
// SetDescription sets the video extensions description parameter.
func (v *Video) SetDescription(d string) *Video {
// Limit to the maximum length of a description.
v.Description = d
if len(v.Description) > MaxDescriptionLength {
v.Description = d[:MaxDescriptionLength]
}
return v
}
// SetThumbnailLocation sets the video thumbnail location parameter.
func (v *Video) SetThumbnailLocation(t string) *Video {
v.ThumbnailLoc = t
return v
}
// SetContentLocation sets the video content location parameter.
func (v *Video) SetContentLocation(c string) *Video {
v.ContentLoc = c
return v
}
// SetPlayerLocation sets the video player location parameter.
func (v *Video) SetPlayerLocation(p string) *Video {
v.PlayerLoc = p
return v
}
// SetDuration sets the video duration parameter.
func (v *Video) SetDuration(d int) *Video {
// Must be no less than min and no more than max.
if d >= DurationMin && d <= DurationMax {
v.Duration = d
}
return v
}
// SetExpirationDate sets the video ExpirationDate parameter.
func (v *Video) SetExpirationDate(t time.Time) *Video {
v.ExpirationDate = t.UTC()
return v
}
// SetRating sets the video rating.
func (v *Video) SetRating(r float32) *Video {
if r >= RatingLow && r <= RatingHigh {
v.Rating = r
}
return v
}
// SetViewCount sets the video view_count.
func (v *Video) SetViewCount(vc int) *Video {
v.ViewCount = vc
return v
}
// SetPublicationDate sets the video extensions PublicationDate parameter.
func (v *Video) SetPublicationDate(t time.Time) *Video {
v.PublicationDate = t.UTC()
return v
}
// IsFamilyFriendly sets the family friendly option to 'yes'
func (v *Video) IsFamilyFriendly() *Video {
v.FamilyFriendly = Yes
return v
}
// NotFamilyFriendly sets the family friendly option to 'no'
func (v *Video) NotFamilyFriendly() *Video {
v.FamilyFriendly = No
return v
}
// setRestrictions creates a country restriction block.
func (v *Video) setRestrictions(r *xml.Attr, c string) {
v.Restrictions = append(v.Restrictions, &Restriction{
Relationship: r,
Value: c,
})
}
// AllowCountries creates a list of allowed countries.
func (v *Video) AllowCountries(c string) *Video {
v.setRestrictions(Allow, c)
return v
}
// DenyCountries creates a list of denied countries.
func (v *Video) DenyCountries(c string) *Video {
v.setRestrictions(Deny, c)
return v
}
// addPlatform creates a platform restriction block.
func (v *Video) addPlatform(r *xml.Attr, p ...PlatformName) {
// Convert platforms to string
platforms := make([]string, 0)
for _, a := range p {
platforms = append(platforms, string(a))
}
v.Platforms = append(v.Platforms, &Platform{
Relationship: r,
Value: strings.Join(platforms, " "),
})
}
// AllowPlatforms creates a list of allowed platforms.
func (v *Video) AllowPlatforms(p ...PlatformName) *Video {
v.addPlatform(Allow, p...)
return v
}
// DenyPlatforms creates a list of denied platforms.
func (v *Video) DenyPlatforms(p ...PlatformName) *Video {
v.addPlatform(Deny, p...)
return v
}
// SubRequired sets the requires_subscription option to 'yes'
func (v *Video) SubRequired() *Video {
v.RequiresSubscription = Yes
return v
}
// SubNotRequired sets the requires_subscription option to 'no'
func (v *Video) SubNotRequired() *Video {
v.RequiresSubscription = No
return v
}
// setUploader sets the uploader
func (v *Video) setUploader(u *Uploader) *Video {
v.Uploader = u
return v
}
// SetUploaderInfo sets the uploader
func (v *Video) SetUploaderInfo(i string) *Video {
if v.Uploader == nil {
return v.setUploader(&Uploader{
Info: i,
})
}
v.Uploader.Info = i
return v
}
// SetUploaderVal sets the uploader value
func (v *Video) SetUploaderVal(val string) *Video {
if v.Uploader == nil {
return v.setUploader(&Uploader{
Value: val,
})
}
v.Uploader.Value = val
return v
}
// IsLive sets the live option to 'yes'
func (v *Video) IsLive() *Video {
v.Live = Yes
return v
}
// NotLive sets the live option to 'no'
func (v *Video) NotLive() *Video {
v.Live = No
return v
}
// SetTags sets tag values
func (v *Video) SetTags(t ...string) *Video {
// Only push tags if we're below the limit.
if len(v.Tags)+len(t) <= cap(v.Tags) {
v.Tags = append(v.Tags, t...)
}
return v
}
func defaultVideo() *Video {
return &Video{
Restrictions: make([]*Restriction, 0),
Platforms: make([]*Platform, 0),
Tags: make([]string, 0, TagLimit),
}
}
// NewVideo returns a new instance of the default Video extension.
func NewVideo() *Video {
return defaultVideo()
}