forked from nytimes/amara
-
Notifications
You must be signed in to change notification settings - Fork 0
/
video.go
264 lines (242 loc) · 7.29 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
package amara
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/url"
"strconv"
"strings"
"time"
)
type Language struct {
Created time.Time `json:"created"`
LanguageCode string `json:"language_code"`
OriginalLanguageCode interface{} `json:"original_language_code"`
Name string `json:"name"`
Title string `json:"title"`
Description string `json:"description"`
Metadata struct {
SpeakerName string `json:"speaker-name"`
Location string `json:"location"`
} `json:"metadata"`
Versions []struct {
Author struct {
Username string `json:"username"`
ID string `json:"id"`
URI string `json:"uri"`
} `json:"author"`
Published bool `json:"published"`
VersionNo int `json:"version_no"`
} `json:"versions"`
SubtitlesURI string `json:"subtitles_uri"`
ResourceURI string `json:"resource_uri"`
SubtitleCount int `json:"subtitle_count"`
SubtitlesComplete bool `json:"subtitles_complete"`
IsPrimaryAudioLanguage bool `json:"is_primary_audio_language"`
IsRtl bool `json:"is_rtl"`
Published bool `json:"published"`
}
type Video struct {
ID string `json:"id"`
VideoType string `json:"video_type"`
PrimaryAudioLanguageCode string `json:"primary_audio_language_code"`
Title string `json:"title"`
Description string `json:"description"`
Duration int `json:"duration"`
Thumbnail string `json:"thumbnail"`
Created time.Time `json:"created"`
Team interface{} `json:"team"`
TeamType interface{} `json:"team_type"`
Project interface{} `json:"project"`
AllUrls []string `json:"all_urls"`
Metadata struct {
SpeakerName string `json:"speaker-name"`
Location string `json:"location"`
} `json:"metadata"`
Languages []struct {
Code string `json:"code"`
Name string `json:"name"`
Published bool `json:"published"`
Dir string `json:"dir"`
SubtitlesURI string `json:"subtitles_uri"`
ResourceURI string `json:"resource_uri"`
} `json:"languages"`
ActivityURI string `json:"activity_uri"`
UrlsURI string `json:"urls_uri"`
SubtitleLanguagesURI string `json:"subtitle_languages_uri"`
ResourceURI string `json:"resource_uri"`
}
type SubtitleInfo struct {
VersionNumber int `json:"version_number"`
SubFormat string `json:"sub_format"`
Subtitles string `json:"subtitles"`
Author struct {
Username string `json:"username"`
ID string `json:"id"`
URI string `json:"uri"`
} `json:"author"`
Language struct {
Code string `json:"code"`
Name string `json:"name"`
Dir string `json:"dir"`
} `json:"language"`
Title string `json:"title"`
Description string `json:"description"`
Metadata struct {
SpeakerName string `json:"speaker-name"`
Location string `json:"location"`
} `json:"metadata"`
VideoTitle string `json:"video_title"`
VideoDescription string `json:"video_description"`
ActionsURI string `json:"actions_uri"`
NotesURI string `json:"notes_uri"`
ResourceURI string `json:"resource_uri"`
SiteURI string `json:"site_uri"`
Video string `json:"video"`
VersionNo int `json:"version_no"`
}
type EditorLoginSession struct {
URL string `json:"url"`
}
func (c *Client) GetVideo(id string) (*Video, error) {
data, err := c.doRequest("GET", fmt.Sprintf("%s/videos/%s/", c.endpoint, id), nil)
if err != nil {
return nil, err
}
video := Video{}
if err = json.Unmarshal(data, &video); err != nil {
return nil, err
}
return &video, nil
}
func (c *Client) CreateVideo(params url.Values) (*Video, error) {
data, err := c.doRequest(
"POST",
fmt.Sprintf("%s/videos/", c.endpoint),
bytes.NewBufferString(params.Encode()),
)
if err != nil {
return nil, err
}
video := Video{}
if err = json.Unmarshal(data, &video); err != nil {
return nil, err
}
return &video, nil
}
func (c *Client) GetLanguage(videoID, langCode string) (*Language, error) {
data, err := c.doRequest(
"GET",
fmt.Sprintf("%s/videos/%s/languages/%s/", c.endpoint, videoID, langCode),
nil,
)
if err != nil {
return nil, err
}
language := Language{}
if err = json.Unmarshal(data, &language); err != nil {
return nil, err
}
return &language, nil
}
func (c *Client) CreateLanguage(videoID, langCode string) (*Language, error) {
params := url.Values{}
params.Add("language_code", langCode)
params.Add("subtitles_complete", "false")
params.Add("is_primary_audio_language", "true")
data, err := c.doRequest(
"POST",
fmt.Sprintf("%s/videos/%s/languages/", c.endpoint, videoID),
strings.NewReader(params.Encode()),
)
if err != nil {
return nil, err
}
lang := Language{}
if err = json.Unmarshal(data, &lang); err != nil {
return nil, err
}
return &lang, nil
}
func (c *Client) UpdateLanguage(videoID, langCode string, complete bool) (*Language, error) {
params := url.Values{}
params.Add("subtitles_complete", strconv.FormatBool(complete))
data, err := c.doRequest(
"PUT",
fmt.Sprintf("%s/videos/%s/languages/%s/", c.endpoint, videoID, langCode),
strings.NewReader(params.Encode()),
)
if err != nil {
return nil, err
}
lang := Language{}
if err = json.Unmarshal(data, &lang); err != nil {
return nil, err
}
return &lang, nil
}
func (c *Client) CreateSubtitles(videoID, langCode, format string, params url.Values) (*SubtitleInfo, error) {
if params == nil {
return nil, errors.New("please provide the request body parameters")
}
params.Set("sub_format", format)
data, err := c.doRequest(
"POST",
fmt.Sprintf("%s/videos/%s/languages/%s/subtitles/", c.endpoint, videoID, langCode),
bytes.NewBufferString(params.Encode()),
)
if err != nil {
return nil, err
}
subtitle := SubtitleInfo{}
if err = json.Unmarshal(data, &subtitle); err != nil {
return nil, err
}
return &subtitle, nil
}
func (c *Client) GetSubtitleInfo(videoID, langCode string) (*SubtitleInfo, error) {
data, err := c.doRequest(
"GET",
fmt.Sprintf("%s/videos/%s/languages/%s/subtitles/?sub_format=vtt", c.endpoint, videoID, langCode),
nil,
)
if err != nil {
return nil, err
}
subtitle := SubtitleInfo{}
if err = json.Unmarshal(data, &subtitle); err != nil {
return nil, err
}
return &subtitle, nil
}
func (c *Client) GetRawSubtitles(videoID, langCode string, captionFormat string) ([]byte, error) {
data, err := c.doRequest(
"GET",
fmt.Sprintf("%s/videos/%s/languages/%s/subtitles/?format=%s", c.endpoint, videoID, langCode, captionFormat),
nil,
)
if err != nil {
return nil, err
}
return data, nil
}
func (c *Client) EditorLogin(videoID, langCode, userName string) (*EditorLoginSession, error) {
params := url.Values{}
params.Set("video_id", videoID)
params.Set("user", userName)
params.Set("language_code", langCode)
data, err := c.doRequest(
"POST",
fmt.Sprintf("%s/editor-login/", c.endpoint),
bytes.NewBufferString(params.Encode()),
)
if err != nil {
return nil, err
}
var editorSession EditorLoginSession
if err := json.Unmarshal(data, &editorSession); err != nil {
return nil, err
}
return &editorSession, nil
}