-
Notifications
You must be signed in to change notification settings - Fork 0
/
support.go
78 lines (68 loc) · 1.89 KB
/
support.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
package youtubetoolkit
import (
"fmt"
"io"
"time"
"github.com/raffaelecassia/youtubetoolkit/bigg"
)
func (tk *Toolkit) SetLogWriter(output io.Writer) {
tk.logWriter = output
}
func (tk *Toolkit) log(a ...any) {
fmt.Fprintln(tk.logWriter, a...)
}
func (tk *Toolkit) logf(format string, a ...any) {
fmt.Fprintf(tk.logWriter, format, a...)
}
func sinceDatePlaylistItems(since time.Time) func(i *bigg.PlaylistItem) (bool, error) {
return func(i *bigg.PlaylistItem) (bool, error) {
// The date and time that the item was added to the playlist.
// The value is specified in ISO 8601 format.
// https://developers.google.com/youtube/v3/docs/playlistItems#snippet.publishedAt
pub, err := time.Parse(bigg.ISO8601_LAYOUT, i.Snippet.PublishedAt)
if err != nil {
return false, fmt.Errorf("time error '%s': %w", i.Snippet.PublishedAt, err)
}
return pub.After(since), nil
}
}
func allPlaylistItems() func(i *bigg.PlaylistItem) (bool, error) {
return func(i *bigg.PlaylistItem) (bool, error) {
return true, nil
}
}
// MultiErrors is used when one error is not enough
type MultiErrors []error
func (e MultiErrors) Error() string {
if len(e) == 1 {
return e[0].Error()
}
msg := "multiple errors:"
for _, err := range e {
msg += "\n- " + err.Error()
}
return msg
}
// multiErrorsHandler handles multiple errors.
// It returns two channels: a send-only for errors and a receive-only to get a MultiErrors.
// Errors received from the first channel are internally stored until the channel is closed.
// Thus it sends a single MultiErrors to the second channel (or nil if no errors).
func multiErrorsHandler() (chan<- error, <-chan error) {
errs := make(chan error, 1)
err := make(chan error, 1)
var out MultiErrors
go func() {
for e := range errs {
if e != nil {
out = append(out, e)
}
}
if len(out) == 0 {
err <- nil
} else {
err <- out
}
close(err)
}()
return errs, err
}