-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
42 lines (34 loc) · 935 Bytes
/
client.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
package yt
import (
"context"
"fmt"
"net/url"
"strings"
"google.golang.org/api/option"
"google.golang.org/api/youtube/v3"
)
type Client struct {
service *youtube.Service
}
func New(ctx context.Context, key string) (*Client, error) {
service, err := youtube.NewService(ctx, option.WithAPIKey(key))
if err != nil {
return nil, fmt.Errorf("failed to create youtube service: %s", err)
}
return &Client{service}, nil
}
func NormaliseURL(raw string) string {
raw = strings.Replace(raw, "music.youtube.com", "youtube.com", 1)
raw = strings.Replace(raw, "youtu.be", "youtube.com", 1)
raw = strings.Replace(raw, "/v/", "/watch?v=", 1)
raw = strings.Replace(raw, "/watch#", "/watch?", 1)
raw = strings.Replace(raw, "/youtube.com/shorts/", "youtube.com/watch?v=", 1)
return raw
}
func GetVideoIDFromURL(input string) string {
url, err := url.Parse(input)
if err != nil {
return ""
}
return url.Query().Get("q")
}