-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleanup_keys.go
61 lines (49 loc) · 1.21 KB
/
cleanup_keys.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
package pkg
import (
"regexp"
"time"
"github.com/araddon/dateparse"
"github.com/mniak/ytlive/internal"
"github.com/pkg/errors"
"google.golang.org/api/option"
"google.golang.org/api/youtube/v3"
)
func CleanupKeys(since time.Time) ([]string, error) {
config := internal.GetOAuthConfig()
ctx, tokenSource := internal.GetTokenSource(config)
svc, err := youtube.NewService(ctx, option.WithTokenSource(tokenSource))
if err != nil {
return nil, errors.Wrap(err, "could not create Youtube API client")
}
streams, err := svc.LiveStreams.List(
[]string{"snippet"},
).Do()
if err != nil {
return nil, errors.Wrap(err, "could not create a new stream")
}
cleaned := make([]string, 0)
r := regexp.MustCompile("\\[(.*)\\].*")
for _, stream := range streams.Items {
title := stream.Snippet.Title
if !r.MatchString(title) {
continue
}
sm := r.FindStringSubmatch(stream.Snippet.Title)
if len(sm) < 2 {
continue
}
date, err := dateparse.ParseAny(sm[1])
if err != nil {
continue
}
if date.Before(since) {
continue
}
err = svc.LiveStreams.Delete(stream.Id).Do()
if err != nil {
return cleaned, err
}
cleaned = append(cleaned, stream.Snippet.Title)
}
return cleaned, nil
}