-
Notifications
You must be signed in to change notification settings - Fork 6
/
youtube.go
70 lines (59 loc) · 1.63 KB
/
youtube.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
package youtube
import (
"context"
"errors"
"fmt"
"log/slog"
"strings"
"github.com/gabe565/castsponsorskip/internal/config"
"github.com/gabe565/castsponsorskip/internal/util"
"google.golang.org/api/option"
"google.golang.org/api/youtube/v3"
)
var (
ErrNotConnected = errors.New("not connected to YouTube")
ErrNoVideos = errors.New("search returned no videos")
ErrNoMatches = errors.New("no search results matched video metadata")
ErrNoId = errors.New("search result missing video ID")
)
var service *youtube.Service
func CreateService(ctx context.Context, opts ...option.ClientOption) error {
var err error
opts = append(
opts,
option.WithAPIKey(config.Default.YouTubeAPIKey),
option.WithTelemetryDisabled(),
)
service, err = youtube.NewService(ctx, opts...)
return err
}
func QueryVideoId(ctx context.Context, artist, title string) (string, error) {
if service == nil {
return "", util.HaltRetries(ErrNotConnected)
}
query := fmt.Sprintf(`%q+intitle:%q`, artist, title)
slog.Debug("Searching for video ID", "query", query)
response, err := service.Search.List([]string{"id", "snippet"}).
Q(query).
Context(ctx).
Do()
if err != nil {
return "", err
}
if len(response.Items) == 0 {
return "", util.HaltRetries(ErrNoVideos)
}
for _, item := range response.Items {
if item == nil || item.Snippet == nil {
continue
}
if !strings.Contains(strings.ToLower(item.Snippet.ChannelTitle), strings.ToLower(artist)) {
continue
}
if item.Id == nil || item.Id.VideoId == "" {
return "", util.HaltRetries(ErrNoId)
}
return item.Id.VideoId, nil
}
return "", util.HaltRetries(ErrNoMatches)
}