forked from silentsokolov/go-vimeo
/
videos.go
668 lines (561 loc) · 20.1 KB
/
videos.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
package vimeo
import (
"errors"
"fmt"
"net"
"net/http"
"os"
"strconv"
"strings"
"time"
)
// VideosService handles communication with the videos related
// methods of the Vimeo API.
//
// Vimeo API docs: https://developer.vimeo.com/api/endpoints/videos
type VideosService service
type dataListVideo struct {
Data []*Video `json:"data,omitempty"`
pagination
}
// Embed internal object provides access to HTML embed code.
type Embed struct {
HTML string `json:"html,omitempty"`
}
// Stats internal object provides access to video statistic.
type Stats struct {
Plays int `json:"plays,omitempty"`
}
// App internal object provides access to specific app.
type App struct {
URI string `json:"uri,omitempty"`
Name string `json:"name,omitempty"`
}
// Buttons internal object embed settings.
type Buttons struct {
Like bool `json:"like"`
WatchLater bool `json:"watchlater"`
Share bool `json:"share"`
Embed bool `json:"embed"`
Vote bool `json:"vote"`
HD bool `json:"HD"`
}
// Logos internal object embed settings.
type Logos struct {
Vimeo bool `json:"vimeo"`
Custom bool `json:"custom"`
StickyCustom bool `json:"sticky_custom"`
}
// EmbedSettings internal object provides access to embed settings.
type EmbedSettings struct {
Buttons *Buttons `json:"buttons,omitempty"`
Logos *Logos `json:"logos,omitempty"`
Outro string `json:"outro,omitempty"`
Portrait string `json:"portrait,omitempty"`
Title string `json:"title,omitempty"`
ByLine string `json:"byline,omitempty"`
Badge bool `json:"badge"`
ByLineBadge bool `json:"byline_badge"`
CollectionsButton bool `json:"collections_button"`
PlayBar bool `json:"playbar"`
Volume bool `json:"volume"`
FullscreenButton bool `json:"fullscreen_button"`
ScalingButton bool `json:"scaling_button"`
Autoplay bool `json:"autoplay"`
Autopause bool `json:"autopause"`
Loop bool `json:"loop"`
Color string `json:"color,omitempty"`
Link bool `json:"link"`
OverlayEmailCapture int `json:"overlay_email_capture,omitempty"`
OverlayEmailCaptureText string `json:"overlay_email_capture_text,omitempty"`
OverlayEmailCaptureConfirmation string `json:"overlay_email_capture_confirmation,omitempty"`
}
// EmbedPresets internal object present settings.
type EmbedPresets struct {
URI string `json:"uri,omitempty"`
Name string `json:"name,omitempty"`
Settings *EmbedSettings `json:"settings,omitempty"`
User *User `json:"user,omitempty"`
}
// Video represents a video.
type Video struct {
URI string `json:"uri,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Link string `json:"link,omitempty"`
Duration int `json:"duration,omitempty"`
Width int `json:"width,omitempty"`
Height int `json:"height,omitempty"`
Language string `json:"language,omitempty"`
Embed *Embed `json:"embed,omitempty"`
CreatedTime time.Time `json:"created_time,omitempty"`
ModifiedTime time.Time `json:"modified_time,omitempty"`
ReleaseTime time.Time `json:"release_time,omitempty"`
ContentRating []string `json:"content_rating,omitempty"`
License string `json:"license,omitempty"`
Privacy *Privacy `json:"privacy,omitempty"`
Pictures *Pictures `json:"pictures,omitempty"`
Tags []*Tag `json:"tags,omitempty"`
Stats *Stats `json:"stats,omitempty"`
User *User `json:"user,omitempty"`
App *App `json:"app,omitempty"`
Status string `json:"status,omitempty"`
ResourceKey string `json:"resource_key,omitempty"`
EmbedPresets *EmbedPresets `json:"embed_presets,omitempty"`
}
// UploadVideo represents a video.
type UploadVideo struct {
URI string `json:"uri,omitempty"`
TicketID string `json:"ticket_id,omitempty"`
User *User `json:"user,omitempty"`
UploadLink string `json:"upload_link,omitempty"`
UploadLinkSecure string `json:"upload_link_secure,omitempty"`
CompleteURI string `json:"complete_uri,omitempty"`
}
// TitleRequest a request to edit an embed settings.
type TitleRequest struct {
Owner string `json:"owner,omitempty"`
Portrait string `json:"portrait,omitempty"`
Name string `json:"name,omitempty"`
}
// RatingTVRequest a request to edit video.
type RatingTVRequest struct {
Rating string `json:"rating,omitempty"`
Reason string `json:"reason,omitempty"`
}
// RatingMPAARequest a request to edit video.
type RatingMPAARequest struct {
Rating string `json:"rating,omitempty"`
Reason string `json:"reason,omitempty"`
}
// RatingsRequest a request to edit an embed settings.
type RatingsRequest struct {
RatingTVRequest string `json:"tv,omitempty"`
RatingMPAARequest string `json:"mpaa,omitempty"`
}
// ExtraLinksRequest a request to edit video.
type ExtraLinksRequest struct {
IMDB string `json:"imdb,omitempty"`
RottenTomatoes string `json:"rotten_tomatoes,omitempty"`
}
// EmbedRequest a request to edit an embed settings.
type EmbedRequest struct {
Buttons *Buttons `json:"buttons,omitempty"`
Logos *Logos `json:"logos,omitempty"`
Outro string `json:"outro,omitempty"`
Portrait string `json:"portrait,omitempty"`
Title *TitleRequest `json:"title,omitempty"`
ByLine string `json:"byline,omitempty"`
Badge bool `json:"badge"`
ByLineBadge bool `json:"byline_badge"`
CollectionsButton bool `json:"collections_button"`
PlayBar bool `json:"playbar"`
Volume bool `json:"volume"`
FullscreenButton bool `json:"fullscreen_button"`
ScalingButton bool `json:"scaling_button"`
Autoplay bool `json:"autoplay"`
Autopause bool `json:"autopause"`
Loop bool `json:"loop"`
Color string `json:"color,omitempty"`
Link bool `json:"link"`
RatingsRequest *RatingsRequest `json:"ratings,omitempty"`
ExtraLinks *ExtraLinksRequest `json:"external_links,omitempty"`
OverlayEmailCapture int `json:"overlay_email_capture,omitempty"`
OverlayEmailCaptureText string `json:"overlay_email_capture_text,omitempty"`
OverlayEmailCaptureConfirmation string `json:"overlay_email_capture_confirmation,omitempty"`
}
// VideoRequest represents a request to edit an video.
type VideoRequest struct {
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
License string `json:"license,omitempty"`
Privacy *Privacy `json:"privacy,omitempty"`
Password string `json:"password,omitempty"`
ReviewLink bool `json:"review_link"`
Locale string `json:"locale,omitempty"`
ContentRating []string `json:"content_rating,omitempty"`
Embed *EmbedRequest `json:"embed,omitempty"`
}
// GetID returns the numeric identifier (ID) of the video.
func (v Video) GetID() int {
l := strings.SplitN(v.URI, "/", -1)
ID, _ := strconv.Atoi(l[len(l)-1])
return ID
}
// ListVideoOptions specifies the optional parameters to the
// CategoriesService.ListVideo method.
type ListVideoOptions struct {
Query string `url:"query,omitempty"`
Filter string `url:"filter,omitempty"`
FilterEmbeddable string `url:"filter_embeddable,omitempty"`
Sort string `url:"sort,omitempty"`
Direction string `url:"direction,omitempty"`
FilterPlayable string `url:"filter_playable,omitempty"`
ListOptions
}
// UploadVideoOptions specifies the optional parameters to the
// uploadVideo method.
type UploadVideoOptions struct {
Type string `json:"type,omitempty"`
}
func listVideo(c *Client, url string, opt *ListVideoOptions) ([]*Video, *Response, error) {
u, err := addOptions(url, opt)
if err != nil {
return nil, nil, err
}
req, err := c.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
videos := &dataListVideo{}
resp, err := c.Do(req, videos)
if err != nil {
return nil, resp, err
}
resp.setPaging(videos)
return videos.Data, resp, err
}
func getVideo(c *Client, url string) (*Video, *Response, error) {
req, err := c.NewRequest("GET", url, nil)
if err != nil {
return nil, nil, err
}
video := &Video{}
resp, err := c.Do(req, video)
if err != nil {
return nil, resp, err
}
return video, resp, err
}
func getUploadVideo(c *Client, uri string, opt *UploadVideoOptions) (*UploadVideo, *Response, error) {
req, err := c.NewRequest("POST", uri, opt)
if err != nil {
return nil, nil, err
}
uploadVideo := &UploadVideo{}
resp, err := c.Do(req, uploadVideo)
if err != nil {
return nil, resp, err
}
return uploadVideo, resp, err
}
func completeUploadVideo(c *Client, completeURI string) (*Video, *Response, error) {
req, err := c.NewRequest("DELETE", completeURI, nil)
if err != nil {
return nil, nil, err
}
// Get uri form header location
resp, err := c.Do(req, nil)
if err != nil {
return nil, nil, err
}
url, err := resp.Location()
if err != nil {
return nil, nil, err
}
video, resp, err := getVideo(c, url.String())
return video, resp, err
}
func processUploadVideo(c *Client, uploadURL string) (int64, error) {
req, err := http.NewRequest("PUT", uploadURL, nil)
if err != nil {
return int64(0), err
}
req.Header.Set("Content-Length", "0")
req.Header.Set("Content-Range", "bytes */*")
resp, err := c.Do(req, nil)
if err != nil {
return int64(0), err
}
rangeHeader := resp.Header.Get("Range")
last := strings.SplitN(rangeHeader, "-", 2)[1]
lastByte, err := strconv.Atoi(last)
if err != nil {
return int64(0), err
}
return int64(lastByte), nil
}
func uploadVideo(c *Client, url string, file *os.File) (*Video, *Response, error) {
opt := &UploadVideoOptions{Type: "streaming"}
stat, err := file.Stat()
if err != nil {
return nil, nil, err
}
if stat.IsDir() {
return nil, nil, errors.New("the video file can't be a directory")
}
uploadVideo, _, err := getUploadVideo(c, url, opt)
if err != nil {
return nil, nil, err
}
lastByte := int64(0)
for lastByte < stat.Size() {
req, err := c.NewUploadRequest(uploadVideo.UploadLinkSecure, file, stat.Size(), lastByte)
if err != nil {
return nil, nil, err
}
_, err = c.Do(req, nil)
if err != nil {
switch nerr := err.(type) {
case net.Error:
if nerr.Timeout() {
lastByte, err = processUploadVideo(c, uploadVideo.UploadLinkSecure)
if err != nil {
return nil, nil, err
}
continue
}
default:
return nil, nil, err
}
}
lastByte, err = processUploadVideo(c, uploadVideo.UploadLinkSecure)
if err != nil {
return nil, nil, err
}
}
video, resp, err := completeUploadVideo(c, uploadVideo.CompleteURI)
return video, resp, err
}
func deleteVideo(c *Client, url string) (*Response, error) {
req, err := c.NewRequest("DELETE", url, nil)
if err != nil {
return nil, err
}
return c.Do(req, nil)
}
func addVideo(c *Client, url string) (*Response, error) {
req, err := c.NewRequest("PUT", url, nil)
if err != nil {
return nil, err
}
return c.Do(req, nil)
}
// List lists the videos.
//
// Vimeo API docs: https://developer.vimeo.com/api/playground/videos
func (s *VideosService) List(opt *ListVideoOptions) ([]*Video, *Response, error) {
videos, resp, err := listVideo(s.client, "videos", opt)
return videos, resp, err
}
// Get specific video by ID.
//
// Vimeo API docs: https://developer.vimeo.com/api/playground/videos/%7Bvideo_id%7D
func (s *VideosService) Get(vid int) (*Video, *Response, error) {
u := fmt.Sprintf("videos/%d", vid)
video, resp, err := getVideo(s.client, u)
return video, resp, err
}
// Edit specific video by ID.
//
// Vimeo API docs: https://developer.vimeo.com/api/playground/videos/%7Bvideo_id%7D
func (s *VideosService) Edit(vid int, r *VideoRequest) (*Video, *Response, error) {
u := fmt.Sprintf("videos/%d", vid)
req, err := s.client.NewRequest("PATCH", u, r)
if err != nil {
return nil, nil, err
}
video := &Video{}
resp, err := s.client.Do(req, video)
if err != nil {
return nil, resp, err
}
return video, resp, nil
}
// Delete specific video by ID.
//
// Vimeo API docs: https://developer.vimeo.com/api/playground/videos/%7Bvideo_id%7D
func (s *VideosService) Delete(vid int) (*Response, error) {
u := fmt.Sprintf("videos/%d", vid)
resp, err := deleteVideo(s.client, u)
return resp, err
}
// ListCategory lists the video category.
//
// Vimeo API docs: https://developer.vimeo.com/api/playground/videos/%7Bvideo_id%7D/categories
func (s *VideosService) ListCategory(vid int, opt *ListCategoryOptions) ([]*Category, *Response, error) {
u := fmt.Sprintf("videos/%d/categories", vid)
catogories, resp, err := listCategory(s.client, u, opt)
return catogories, resp, err
}
// LikeList lists users who liked this video.
//
// Vimeo API docs: https://developer.vimeo.com/api/playground/videos/%7Bvideo_id%7D/likes
func (s *VideosService) LikeList(vid int, opt *ListUserOptions) ([]*User, *Response, error) {
u := fmt.Sprintf("videos/%d/likes", vid)
users, resp, err := listUser(s.client, u, opt)
return users, resp, err
}
// GetPreset get preset by name.
//
// Vimeo API docs: https://developer.vimeo.com/api/playground/videos/%7Bvideo_id%7D/presets/%7Bpreset_id%7D
func (s *VideosService) GetPreset(vid int, p int) (*Preset, *Response, error) {
u := fmt.Sprintf("videos/%d/presets/%d", vid, p)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
portf := &Preset{}
resp, err := s.client.Do(req, portf)
if err != nil {
return nil, resp, err
}
return portf, resp, err
}
// AssignPreset embed preset by name.
//
// Vimeo API docs: https://developer.vimeo.com/api/playground/videos/%7Bvideo_id%7D/presets/%7Bpreset_id%7D
func (s *VideosService) AssignPreset(vid int, p int) (*Response, error) {
u := fmt.Sprintf("videos/%d/presets/%d", vid, p)
req, err := s.client.NewRequest("PUT", u, nil)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
}
// UnassignPreset embed preset by name.
//
// Vimeo API docs: https://developer.vimeo.com/api/playground/videos/%7Bvideo_id%7D/presets/%7Bpreset_id%7D
func (s *VideosService) UnassignPreset(vid int, p int) (*Response, error) {
u := fmt.Sprintf("videos/%d/presets/%d", vid, p)
req, err := s.client.NewRequest("DELETE", u, nil)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
}
type dataListDomain struct {
Data []*Domain `json:"data,omitempty"`
pagination
}
// Domain represents a domain.
type Domain struct {
URI string `json:"uri,omitempty"`
Name string `json:"name,omitempty"`
}
// ListDomain lists the domains.
//
// Vimeo API docs: https://developer.vimeo.com/api/playground/videos/%7Bvideo_id%7D/privacy/domains
func (s *VideosService) ListDomain(vid int) ([]*Domain, *Response, error) {
u := fmt.Sprintf("videos/%d/privacy/domains", vid)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
domains := &dataListDomain{}
resp, err := s.client.Do(req, domains)
if err != nil {
return nil, resp, err
}
resp.setPaging(domains)
return domains.Data, resp, err
}
// AllowDomain embedding on a domain.
//
// Vimeo API docs: https://developer.vimeo.com/api/playground/videos/%7Bvideo_id%7D/privacy/domains/%7Bdomain%7D
func (s *VideosService) AllowDomain(vid int, d string) (*Response, error) {
u := fmt.Sprintf("videos/%d/privacy/domains/%s", vid, d)
req, err := s.client.NewRequest("PUT", u, nil)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
}
// DisallowDomain embedding on a domain.
//
// Vimeo API docs: https://developer.vimeo.com/api/playground/videos/%7Bvideo_id%7D/privacy/domains/%7Bdomain%7D
func (s *VideosService) DisallowDomain(vid int, d string) (*Response, error) {
u := fmt.Sprintf("videos/%d/privacy/domains/%s", vid, d)
req, err := s.client.NewRequest("DELETE", u, nil)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
}
// ListUser list the all allowed users
//
// Vimeo API docs: https://developer.vimeo.com/api/playground/videos/%7Bvideo_id%7D/privacy/users
func (s *VideosService) ListUser(vid int) ([]*User, *Response, error) {
u := fmt.Sprintf("videos/%d/privacy/users", vid)
users, resp, err := listUser(s.client, u, nil)
return users, resp, err
}
// AllowUsers allow users to view this video.
//
// Vimeo API docs: https://developer.vimeo.com/api/playground/videos/%7Bvideo_id%7D/privacy/users
func (s *VideosService) AllowUsers(vid int) (*Response, error) {
u := fmt.Sprintf("videos/%d/privacy/users", vid)
req, err := s.client.NewRequest("PUT", u, nil)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
}
// AllowUser allow users to view this video.
//
// Vimeo API docs: https://developer.vimeo.com/api/playground/videos/%7Bvideo_id%7D/privacy/users/%7Buser_id%7D
func (s *VideosService) AllowUser(vid int, uid string) (*Response, error) {
u := fmt.Sprintf("videos/%d/privacy/users/%s", vid, uid)
req, err := s.client.NewRequest("PUT", u, nil)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
}
// DisallowUser disallow user from viewing this video.
//
// Vimeo API docs: https://developer.vimeo.com/api/playground/videos/%7Bvideo_id%7D/privacy/users/%7Buser_id%7D
func (s *VideosService) DisallowUser(vid int, uid string) (*Response, error) {
u := fmt.Sprintf("videos/%d/privacy/users/%s", vid, uid)
req, err := s.client.NewRequest("DELETE", u, nil)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
}
// ListTag list a video's tags
//
// Vimeo API docs: https://developer.vimeo.com/api/playground/videos/%7Bvideo_id%7D/tags
func (s *VideosService) ListTag(vid int) ([]*Tag, *Response, error) {
u := fmt.Sprintf("videos/%d/tags", vid)
tags, resp, err := listTag(s.client, u)
return tags, resp, err
}
// GetTag specific tag by name.
//
// Vimeo API docs: https://developer.vimeo.com/api/playground/videos/%7Bvideo_id%7D/tags/%7Bword%7D
func (s *VideosService) GetTag(vid int, t string) (*Tag, *Response, error) {
u := fmt.Sprintf("videos/%d/tags/%s", vid, t)
tag, resp, err := getTag(s.client, u)
return tag, resp, err
}
// AssignTag specific tag by name.
//
// Vimeo API docs: https://developer.vimeo.com/api/playground/videos/%7Bvideo_id%7D/tags/%7Bword%7D
func (s *VideosService) AssignTag(vid int, t string) (*Response, error) {
u := fmt.Sprintf("videos/%d/tags/%s", vid, t)
req, err := s.client.NewRequest("PUT", u, nil)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
}
// UnassignTag specific tag by name.
//
// Vimeo API docs: https://developer.vimeo.com/api/playground/videos/%7Bvideo_id%7D/tags/%7Bword%7D
func (s *VideosService) UnassignTag(vid int, t string) (*Response, error) {
u := fmt.Sprintf("videos/%d/tags/%s", vid, t)
req, err := s.client.NewRequest("DELETE", u, nil)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
}
// ListRelatedVideo lists the related video.
//
// Vimeo API docs: https://developer.vimeo.com/api/playground/videos/%7Bvideo_id%7D/videos
func (s *VideosService) ListRelatedVideo(vid int, opt *ListVideoOptions) ([]*Video, *Response, error) {
u := fmt.Sprintf("videos/%d/videos", vid)
videos, resp, err := listVideo(s.client, u, opt)
return videos, resp, err
}