-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathvideo.go
138 lines (115 loc) · 4.11 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
package pexels
import (
"context"
"fmt"
"github.com/google/go-querystring/query"
)
type VideoResponse struct {
Id int `json:"id"`
Width int `json:"width"`
Height int `json:"height"`
Url string `json:"url"`
Image string `json:"image"`
Tags []string `json:"tags"`
Duration int `json:"duration"`
User struct {
Id int `json:"id"`
Name string `json:"name"`
Url string `json:"url"`
} `json:"user"`
VideoFiles []struct {
Id int `json:"id"`
Quality string `json:"quality"`
FileType string `json:"file_type"`
Width int `json:"width"`
Height int `json:"height"`
Fps float64 `json:"fps"`
Link string `json:"link"`
} `json:"video_files"`
VideoPictures []struct {
Id int `json:"id"`
Picture string `json:"picture"`
Nr int `json:"nr"`
} `json:"video_pictures"`
}
type VideoSearchResponse struct {
Page int `json:"page"`
PerPage int `json:"per_page"`
TotalResults int `json:"total_results"`
Url string `json:"url"`
NextPage string `json:"next_page"`
PrevPage string `json:"prev_page"`
Photos []*VideoResponse `json:"videos"`
}
type VideoQueryParams struct {
// Default: 1
Page int `query:"page" url:"page,omitempty"`
// Default: 15, Max: 80
PerPage int `query:"per_page" url:"per_page,omitempty"`
Query string `query:"query" url:"query,omitempty"`
// "landscape", "portrait", "square"
Orientation string `query:"orientation" url:"per_page,omitempty"`
// "large" (4K), "medium" (Full HD) or "small" (HD)
Size string `query:"size" url:"size,omitempty"`
// "en-US", "pt-BR", "es-ES", "ca-ES", "de-DE", "it-IT", "fr-FR", "sv-SE", "id-ID", "pl-PL", "ja-JP", "zh-TW", "zh-CN", "ko-KR"
// "th-TH", "nl-NL", "hu-HU", "vi-VN", "cs-CZ", "da-DK", "fi-FI", "uk-UA", "el-GR", "ro-RO", "nb-NO", "sk-SK", "tr-TR", "ru-RU"
Locale string `query:"locale" url:"locale,omitempty"`
}
// SearchVideos search Pexels for videos.
// Query could be something broad like "Nature", "Tigers", "People". Or it could be something specific like "Group of people working".
// https://www.pexels.com/api/documentation/#videos-search
//
//encore:api public method=GET path=/videos/search
func SearchVideos(ctx context.Context, queryParams *VideoQueryParams) (*VideoSearchResponse, error) {
if queryParams.Query == "" {
return nil, fmt.Errorf("query string needs to contain param 'query'")
}
v, err := query.Values(queryParams)
if err != nil {
return nil, err
}
url := "https://api.pexels.com/videos/search?" + v.Encode()
var response *VideoSearchResponse
if err = makeGetRequest(url, &response); err != nil {
return nil, err
}
return response, nil
}
type VideoPopularParams struct {
// Default: 1
Page int `query:"page" url:"page,omitempty"`
// Default: 15, Max: 80
PerPage int `query:"per_page" url:"per_page,omitempty"`
MinWidth int `query:"min_width" url:"min_width,omitempty"`
MinHeight int `query:"min_height" url:"min_height,omitempty"`
MinDuration int `query:"min_duration" url:"min_duration,omitempty"`
MaxDuration int `query:"max_duration" url:"max_duration,omitempty"`
}
// PopularVideos receives the current popular Pexels videos.
// https://www.pexels.com/api/documentation/#videos-popular
//
//encore:api public method=GET path=/videos/popular
func PopularVideos(ctx context.Context, queryParams *VideoPopularParams) (*VideoSearchResponse, error) {
v, err := query.Values(queryParams)
if err != nil {
return nil, err
}
url := "https://api.pexels.com/videos/popular?" + v.Encode()
var response *VideoSearchResponse
if err = makeGetRequest(url, &response); err != nil {
return nil, err
}
return response, nil
}
// GetVideo retrieves a specific video.
// https://www.pexels.com/api/documentation/#videos-show
//
//encore:api public method=GET path=/video/:id
func GetVideo(ctx context.Context, id string) (*VideoResponse, error) {
url := "https://api.pexels.com/videos/videos/" + id
var response *VideoResponse
if err := makeGetRequest(url, &response); err != nil {
return nil, err
}
return response, nil
}