-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathyoutube.go
100 lines (84 loc) · 2.8 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package handler
import (
"context"
"net/url"
"strings"
"github.com/micro/micro/v3/service/errors"
"github.com/micro/micro/v3/service/logger"
pb "github.com/micro/services/youtube/proto"
"google.golang.org/api/option"
"google.golang.org/api/youtube/v3"
)
type Youtube struct {
Client *youtube.Service
}
func New(apiKey string) *Youtube {
ctx := context.TODO()
yt, _ := youtube.NewService(ctx, option.WithAPIKey(apiKey))
return &Youtube{
Client: yt,
}
}
func (y *Youtube) Embed(ctx context.Context, req *pb.EmbedRequest, rsp *pb.EmbedResponse) error {
if len(req.Url) == 0 {
return errors.BadRequest("youtube.embed", "missing url")
}
var id string
if strings.HasPrefix(req.Url, "https://youtu.be/") {
id = strings.TrimPrefix(req.Url, "https://youtu.be/")
} else if !strings.HasPrefix(req.Url, "https://www.youtube.com/watch") {
return errors.BadRequest("youtube.embed", "invalid url")
} else {
uri, err := url.Parse(req.Url)
if err != nil {
return errors.BadRequest("youtube.embed", "invalid url")
}
vals := uri.Query()
id = vals.Get("v")
if len(id) == 0 {
return errors.BadRequest("youtube.embed", "invalid url")
}
}
rsp.LongUrl = "https://www.youtube.com/watch?v=" + id
rsp.ShortUrl = "https://youtu.be/" + id
rsp.EmbedUrl = "https://www.youtube.com/embed/" + id
rsp.HtmlScript = `<iframe width="560" height="315" src="` + rsp.EmbedUrl + `" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>`
return nil
}
func (y *Youtube) Search(ctx context.Context, req *pb.SearchRequest, rsp *pb.SearchResponse) error {
if len(req.Query) == 0 {
return errors.BadRequest("youtube.search", "missing query")
}
resp, err := y.Client.Search.List([]string{"id", "snippet"}).Q(req.Query).MaxResults(25).Do()
if err != nil {
logger.Errorf("failed to search youtube for %v: %v", req.Query, err)
return errors.InternalServerError("youtube.search", "Failed to search for "+req.Query)
}
for _, item := range resp.Items {
var id, url string
kind := strings.Split(item.Id.Kind, "#")[1]
switch kind {
case "video":
id = item.Id.VideoId
url = "https://www.youtube.com/watch?v=" + id
case "playlist":
id = item.Id.PlaylistId
url = "https://www.youtube.com/playlist?list=" + id
case "channel":
id = item.Id.ChannelId
url = "https://www.youtube.com/channel/" + id
}
rsp.Results = append(rsp.Results, &pb.SearchResult{
Id: id,
Kind: kind,
Title: item.Snippet.Title,
ChannelId: item.Snippet.ChannelId,
ChannelTitle: item.Snippet.ChannelTitle,
Description: item.Snippet.Description,
PublishedAt: item.Snippet.PublishedAt,
Broadcasting: item.Snippet.LiveBroadcastContent,
Url: url,
})
}
return nil
}