7
7
"github.com/eat-pray-ai/yutu/pkg/utils"
8
8
"google.golang.org/api/youtube/v3"
9
9
"io"
10
- "log"
11
10
"os"
12
11
)
13
12
@@ -40,12 +39,12 @@ type caption struct {
40
39
}
41
40
42
41
type Caption interface {
43
- get (parts []string ) []* youtube.Caption // todo: return error
44
- List (parts []string , output string )
45
- Insert (output string )
46
- Update (output string )
47
- Delete ()
48
- Download ()
42
+ get (parts []string ) ( []* youtube.Caption , error ) // todo: return error
43
+ List (parts []string , output string , writer io. Writer ) error
44
+ Insert (output string , writer io. Writer ) error
45
+ Update (output string , writer io. Writer ) error
46
+ Delete (writer io. Writer ) error
47
+ Download (writer io. Writer ) error
49
48
}
50
49
51
50
type Option func (* caption )
@@ -58,7 +57,7 @@ func NewCation(opts ...Option) Caption {
58
57
return c
59
58
}
60
59
61
- func (c * caption ) get (parts []string ) []* youtube.Caption {
60
+ func (c * caption ) get (parts []string ) ( []* youtube.Caption , error ) {
62
61
call := service .Captions .List (parts , c .VideoId )
63
62
if len (c .IDs ) > 0 {
64
63
call = call .Id (c .IDs ... )
@@ -72,33 +71,36 @@ func (c *caption) get(parts []string) []*youtube.Caption {
72
71
73
72
res , err := call .Do ()
74
73
if err != nil {
75
- utils .PrintJSON (c , nil )
76
- log .Fatalln (errors .Join (errGetCaption , err ))
74
+ return nil , errors .Join (errGetCaption , err )
77
75
}
78
76
79
- return res .Items
77
+ return res .Items , nil
80
78
}
81
79
82
- func (c * caption ) List (parts []string , output string ) {
83
- captions := c .get (parts )
80
+ func (c * caption ) List (parts []string , output string , writer io.Writer ) error {
81
+ captions , err := c .get (parts )
82
+ if err != nil {
83
+ return err
84
+ }
85
+
84
86
switch output {
85
87
case "json" :
86
- utils .PrintJSON (captions , nil )
88
+ utils .PrintJSON (captions , writer )
87
89
case "yaml" :
88
- utils .PrintYAML (captions , nil )
90
+ utils .PrintYAML (captions , writer )
89
91
default :
90
- fmt .Println ( "ID\t Name" )
92
+ _ , _ = fmt .Fprintln ( writer , "ID\t Name" )
91
93
for _ , caption := range captions {
92
- fmt .Printf ( "%s\t %s\n " , caption .Id , caption .Snippet .Name )
94
+ _ , _ = fmt .Fprintf ( writer , "%s\t %s\n " , caption .Id , caption .Snippet .Name )
93
95
}
94
96
}
97
+ return nil
95
98
}
96
99
97
- func (c * caption ) Insert (output string ) {
100
+ func (c * caption ) Insert (output string , writer io. Writer ) error {
98
101
file , err := os .Open (c .File )
99
102
if err != nil {
100
- utils .PrintJSON (c , nil )
101
- log .Fatalln (errors .Join (errInsertCaption , err ))
103
+ return errors .Join (errInsertCaption , err )
102
104
}
103
105
defer file .Close ()
104
106
@@ -127,23 +129,31 @@ func (c *caption) Insert(output string) {
127
129
128
130
res , err := call .Do ()
129
131
if err != nil {
130
- utils .PrintJSON (c , nil )
131
- log .Fatalln (errors .Join (errInsertCaption , err ))
132
+ return errors .Join (errInsertCaption , err )
132
133
}
133
134
134
135
switch output {
135
136
case "json" :
136
- utils .PrintJSON (res , nil )
137
+ utils .PrintJSON (res , writer )
137
138
case "yaml" :
138
- utils .PrintYAML (res , nil )
139
+ utils .PrintYAML (res , writer )
139
140
case "silent" :
140
141
default :
141
- fmt .Printf ( "Caption inserted: %s\n " , res .Id )
142
+ _ , _ = fmt .Fprintf ( writer , "Caption inserted: %s\n " , res .Id )
142
143
}
144
+ return nil
143
145
}
144
146
145
- func (c * caption ) Update (output string ) {
146
- caption := c .get ([]string {"snippet" })[0 ]
147
+ func (c * caption ) Update (output string , writer io.Writer ) error {
148
+ captions , err := c .get ([]string {"snippet" })
149
+ if err != nil {
150
+ return errors .Join (errUpdateCaption , err )
151
+ }
152
+ if len (captions ) == 0 {
153
+ return errGetCaption
154
+ }
155
+
156
+ caption := captions [0 ]
147
157
if c .AudioTrackType != "" {
148
158
caption .Snippet .AudioTrackType = c .AudioTrackType
149
159
}
@@ -179,8 +189,7 @@ func (c *caption) Update(output string) {
179
189
if c .File != "" {
180
190
file , err := os .Open (c .File )
181
191
if err != nil {
182
- utils .PrintJSON (c , nil )
183
- log .Fatalln (errors .Join (errUpdateCaption , err ))
192
+ return errors .Join (errUpdateCaption , err )
184
193
}
185
194
defer file .Close ()
186
195
call = call .Media (file )
@@ -194,22 +203,22 @@ func (c *caption) Update(output string) {
194
203
195
204
res , err := call .Do ()
196
205
if err != nil {
197
- utils .PrintJSON (c , nil )
198
- log .Fatalln (errors .Join (errUpdateCaption , err ))
206
+ return errors .Join (errUpdateCaption , err )
199
207
}
200
208
201
209
switch output {
202
210
case "json" :
203
- utils .PrintJSON (res , nil )
211
+ utils .PrintJSON (res , writer )
204
212
case "yaml" :
205
- utils .PrintYAML (res , nil )
213
+ utils .PrintYAML (res , writer )
206
214
case "silent" :
207
215
default :
208
- fmt .Printf ( "Caption updated: %s\n " , res .Id )
216
+ _ , _ = fmt .Fprintf ( writer , "Caption updated: %s\n " , res .Id )
209
217
}
218
+ return nil
210
219
}
211
220
212
- func (c * caption ) Delete () {
221
+ func (c * caption ) Delete (writer io. Writer ) error {
213
222
for _ , id := range c .IDs {
214
223
call := service .Captions .Delete (id )
215
224
if c .OnBehalfOf != "" {
@@ -221,15 +230,15 @@ func (c *caption) Delete() {
221
230
222
231
err := call .Do ()
223
232
if err != nil {
224
- utils .PrintJSON (c , nil )
225
- log .Fatalln (errors .Join (errDeleteCaption , err ))
233
+ return errors .Join (errDeleteCaption , err )
226
234
}
227
235
228
- fmt .Printf ( "Caption %s deleted\n " , id )
236
+ _ , _ = fmt .Fprintf ( writer , "Caption %s deleted\n " , id )
229
237
}
238
+ return nil
230
239
}
231
240
232
- func (c * caption ) Download () {
241
+ func (c * caption ) Download (writer io. Writer ) error {
233
242
call := service .Captions .Download (c .IDs [0 ])
234
243
if c .Tfmt != "" {
235
244
call = call .Tfmt (c .Tfmt )
@@ -246,29 +255,28 @@ func (c *caption) Download() {
246
255
247
256
res , err := call .Download ()
248
257
if err != nil {
249
- utils .PrintJSON (c , nil )
250
- log .Fatalln (errors .Join (errDownloadCaption , err ))
258
+ return errors .Join (errDownloadCaption , err )
251
259
}
252
260
defer res .Body .Close ()
261
+
253
262
body , err := io .ReadAll (res .Body )
254
263
if err != nil {
255
- utils .PrintJSON (c , nil )
256
- log .Fatalln (errors .Join (errDownloadCaption , err ))
264
+ return errors .Join (errDownloadCaption , err )
257
265
}
258
266
259
267
file , err := os .Create (c .File )
260
268
if err != nil {
261
- utils .PrintJSON (c , nil )
262
- log .Fatalln (errors .Join (errDownloadCaption , err ))
269
+ return errors .Join (errDownloadCaption , err )
263
270
}
264
271
defer file .Close ()
272
+
265
273
_ , err = file .Write (body )
266
274
if err != nil {
267
- utils .PrintJSON (c , nil )
268
- log .Fatalln (errors .Join (errDownloadCaption , err ))
275
+ return errors .Join (errDownloadCaption , err )
269
276
}
270
277
271
278
fmt .Printf ("Caption %s downloaded to %s\n " , c .IDs [0 ], c .File )
279
+ return nil
272
280
}
273
281
274
282
func WithIDs (ids []string ) Option {
0 commit comments