youtube embed

This commit is contained in:
Asim Aslam
2021-12-10 11:07:11 +00:00
parent d5fa802db2
commit 13dd1af1d4
4 changed files with 283 additions and 52 deletions

View File

@@ -2,6 +2,8 @@ package handler
import (
"context"
"fmt"
"net/url"
"strings"
"github.com/micro/micro/v3/service/errors"
@@ -15,6 +17,10 @@ type Youtube struct {
Client *youtube.Service
}
var (
iframe = `<iframe width="560" height="315" src="%s" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>`
)
func New(apiKey string) *Youtube {
ctx := context.TODO()
yt, _ := youtube.NewService(ctx, option.WithAPIKey(apiKey))
@@ -24,6 +30,44 @@ func New(apiKey string) *Youtube {
}
}
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")
}
if strings.HasPrefix(req.Url, "https://youtu.be/") {
id := strings.TrimPrefix(req.Url, "https://youtu.be/")
rsp.Link = "https://www.youtube.com/embed/" + id
rsp.Script = fmt.Sprintf(iframe, rsp.Link)
rsp.ShortUrl = req.Url
return nil
}
if !strings.HasPrefix(req.Url, "https://www.youtube.com/watch") {
return errors.BadRequest("youtube.embed", "invalid url")
}
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.Link = "https://www.youtube.com/embed/" + id
rsp.Script = fmt.Sprintf(iframe, rsp.Link)
rsp.ShortUrl = "https://youtu.be/" + id
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")

View File

@@ -20,6 +20,121 @@ const (
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
)
// Embed a YouTube video
type EmbedRequest struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
// provide the youtube url e.g https://www.youtube.com/watch?v=GWRWZu7XsJ0
Url string `protobuf:"bytes,1,opt,name=url,proto3" json:"url,omitempty"`
}
func (x *EmbedRequest) Reset() {
*x = EmbedRequest{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_youtube_proto_msgTypes[0]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *EmbedRequest) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*EmbedRequest) ProtoMessage() {}
func (x *EmbedRequest) ProtoReflect() protoreflect.Message {
mi := &file_proto_youtube_proto_msgTypes[0]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use EmbedRequest.ProtoReflect.Descriptor instead.
func (*EmbedRequest) Descriptor() ([]byte, []int) {
return file_proto_youtube_proto_rawDescGZIP(), []int{0}
}
func (x *EmbedRequest) GetUrl() string {
if x != nil {
return x.Url
}
return ""
}
type EmbedResponse struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
// the embeddable link e.g https://www.youtube.com/watch?v=GWRWZu7XsJ0
Link string `protobuf:"bytes,1,opt,name=link,proto3" json:"link,omitempty"`
// the script code
Script string `protobuf:"bytes,2,opt,name=script,proto3" json:"script,omitempty"`
// the short link
ShortUrl string `protobuf:"bytes,3,opt,name=short_url,json=shortUrl,proto3" json:"short_url,omitempty"`
}
func (x *EmbedResponse) Reset() {
*x = EmbedResponse{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_youtube_proto_msgTypes[1]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *EmbedResponse) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*EmbedResponse) ProtoMessage() {}
func (x *EmbedResponse) ProtoReflect() protoreflect.Message {
mi := &file_proto_youtube_proto_msgTypes[1]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use EmbedResponse.ProtoReflect.Descriptor instead.
func (*EmbedResponse) Descriptor() ([]byte, []int) {
return file_proto_youtube_proto_rawDescGZIP(), []int{1}
}
func (x *EmbedResponse) GetLink() string {
if x != nil {
return x.Link
}
return ""
}
func (x *EmbedResponse) GetScript() string {
if x != nil {
return x.Script
}
return ""
}
func (x *EmbedResponse) GetShortUrl() string {
if x != nil {
return x.ShortUrl
}
return ""
}
type SearchResult struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
@@ -49,7 +164,7 @@ type SearchResult struct {
func (x *SearchResult) Reset() {
*x = SearchResult{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_youtube_proto_msgTypes[0]
mi := &file_proto_youtube_proto_msgTypes[2]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@@ -62,7 +177,7 @@ func (x *SearchResult) String() string {
func (*SearchResult) ProtoMessage() {}
func (x *SearchResult) ProtoReflect() protoreflect.Message {
mi := &file_proto_youtube_proto_msgTypes[0]
mi := &file_proto_youtube_proto_msgTypes[2]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@@ -75,7 +190,7 @@ func (x *SearchResult) ProtoReflect() protoreflect.Message {
// Deprecated: Use SearchResult.ProtoReflect.Descriptor instead.
func (*SearchResult) Descriptor() ([]byte, []int) {
return file_proto_youtube_proto_rawDescGZIP(), []int{0}
return file_proto_youtube_proto_rawDescGZIP(), []int{2}
}
func (x *SearchResult) GetId() string {
@@ -154,7 +269,7 @@ type SearchRequest struct {
func (x *SearchRequest) Reset() {
*x = SearchRequest{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_youtube_proto_msgTypes[1]
mi := &file_proto_youtube_proto_msgTypes[3]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@@ -167,7 +282,7 @@ func (x *SearchRequest) String() string {
func (*SearchRequest) ProtoMessage() {}
func (x *SearchRequest) ProtoReflect() protoreflect.Message {
mi := &file_proto_youtube_proto_msgTypes[1]
mi := &file_proto_youtube_proto_msgTypes[3]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@@ -180,7 +295,7 @@ func (x *SearchRequest) ProtoReflect() protoreflect.Message {
// Deprecated: Use SearchRequest.ProtoReflect.Descriptor instead.
func (*SearchRequest) Descriptor() ([]byte, []int) {
return file_proto_youtube_proto_rawDescGZIP(), []int{1}
return file_proto_youtube_proto_rawDescGZIP(), []int{3}
}
func (x *SearchRequest) GetQuery() string {
@@ -202,7 +317,7 @@ type SearchResponse struct {
func (x *SearchResponse) Reset() {
*x = SearchResponse{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_youtube_proto_msgTypes[2]
mi := &file_proto_youtube_proto_msgTypes[4]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@@ -215,7 +330,7 @@ func (x *SearchResponse) String() string {
func (*SearchResponse) ProtoMessage() {}
func (x *SearchResponse) ProtoReflect() protoreflect.Message {
mi := &file_proto_youtube_proto_msgTypes[2]
mi := &file_proto_youtube_proto_msgTypes[4]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@@ -228,7 +343,7 @@ func (x *SearchResponse) ProtoReflect() protoreflect.Message {
// Deprecated: Use SearchResponse.ProtoReflect.Descriptor instead.
func (*SearchResponse) Descriptor() ([]byte, []int) {
return file_proto_youtube_proto_rawDescGZIP(), []int{2}
return file_proto_youtube_proto_rawDescGZIP(), []int{4}
}
func (x *SearchResponse) GetResults() []*SearchResult {
@@ -242,37 +357,48 @@ var File_proto_youtube_proto protoreflect.FileDescriptor
var file_proto_youtube_proto_rawDesc = []byte{
0x0a, 0x13, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x79, 0x6f, 0x75, 0x74, 0x75, 0x62, 0x65, 0x2e,
0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x07, 0x79, 0x6f, 0x75, 0x74, 0x75, 0x62, 0x65, 0x22, 0x87,
0x02, 0x0a, 0x0c, 0x53, 0x65, 0x61, 0x72, 0x63, 0x68, 0x52, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x12,
0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12,
0x12, 0x0a, 0x04, 0x6b, 0x69, 0x6e, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6b,
0x69, 0x6e, 0x64, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x69, 0x74, 0x6c, 0x65, 0x18, 0x03, 0x20, 0x01,
0x28, 0x09, 0x52, 0x05, 0x74, 0x69, 0x74, 0x6c, 0x65, 0x12, 0x20, 0x0a, 0x0b, 0x64, 0x65, 0x73,
0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0b,
0x64, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x1d, 0x0a, 0x0a, 0x63,
0x68, 0x61, 0x6e, 0x6e, 0x65, 0x6c, 0x5f, 0x69, 0x64, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52,
0x09, 0x63, 0x68, 0x61, 0x6e, 0x6e, 0x65, 0x6c, 0x49, 0x64, 0x12, 0x23, 0x0a, 0x0d, 0x63, 0x68,
0x61, 0x6e, 0x6e, 0x65, 0x6c, 0x5f, 0x74, 0x69, 0x74, 0x6c, 0x65, 0x18, 0x06, 0x20, 0x01, 0x28,
0x09, 0x52, 0x0c, 0x63, 0x68, 0x61, 0x6e, 0x6e, 0x65, 0x6c, 0x54, 0x69, 0x74, 0x6c, 0x65, 0x12,
0x21, 0x0a, 0x0c, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x73, 0x68, 0x65, 0x64, 0x5f, 0x61, 0x74, 0x18,
0x07, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0b, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x73, 0x68, 0x65, 0x64,
0x41, 0x74, 0x12, 0x22, 0x0a, 0x0c, 0x62, 0x72, 0x6f, 0x61, 0x64, 0x63, 0x61, 0x73, 0x74, 0x69,
0x6e, 0x67, 0x18, 0x08, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0c, 0x62, 0x72, 0x6f, 0x61, 0x64, 0x63,
0x61, 0x73, 0x74, 0x69, 0x6e, 0x67, 0x12, 0x10, 0x0a, 0x03, 0x75, 0x72, 0x6c, 0x18, 0x09, 0x20,
0x01, 0x28, 0x09, 0x52, 0x03, 0x75, 0x72, 0x6c, 0x22, 0x25, 0x0a, 0x0d, 0x53, 0x65, 0x61, 0x72,
0x63, 0x68, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x71, 0x75, 0x65,
0x72, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x71, 0x75, 0x65, 0x72, 0x79, 0x22,
0x41, 0x0a, 0x0e, 0x53, 0x65, 0x61, 0x72, 0x63, 0x68, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73,
0x65, 0x12, 0x2f, 0x0a, 0x07, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x73, 0x18, 0x01, 0x20, 0x03,
0x28, 0x0b, 0x32, 0x15, 0x2e, 0x79, 0x6f, 0x75, 0x74, 0x75, 0x62, 0x65, 0x2e, 0x53, 0x65, 0x61,
0x72, 0x63, 0x68, 0x52, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x52, 0x07, 0x72, 0x65, 0x73, 0x75, 0x6c,
0x74, 0x73, 0x32, 0x46, 0x0a, 0x07, 0x59, 0x6f, 0x75, 0x74, 0x75, 0x62, 0x65, 0x12, 0x3b, 0x0a,
0x06, 0x53, 0x65, 0x61, 0x72, 0x63, 0x68, 0x12, 0x16, 0x2e, 0x79, 0x6f, 0x75, 0x74, 0x75, 0x62,
0x65, 0x2e, 0x53, 0x65, 0x61, 0x72, 0x63, 0x68, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a,
0x17, 0x2e, 0x79, 0x6f, 0x75, 0x74, 0x75, 0x62, 0x65, 0x2e, 0x53, 0x65, 0x61, 0x72, 0x63, 0x68,
0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42, 0x11, 0x5a, 0x0f, 0x2e, 0x2f,
0x70, 0x72, 0x6f, 0x74, 0x6f, 0x3b, 0x79, 0x6f, 0x75, 0x74, 0x75, 0x62, 0x65, 0x62, 0x06, 0x70,
0x72, 0x6f, 0x74, 0x6f, 0x33,
0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x07, 0x79, 0x6f, 0x75, 0x74, 0x75, 0x62, 0x65, 0x22, 0x20,
0x0a, 0x0c, 0x45, 0x6d, 0x62, 0x65, 0x64, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x10,
0x0a, 0x03, 0x75, 0x72, 0x6c, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x75, 0x72, 0x6c,
0x22, 0x58, 0x0a, 0x0d, 0x45, 0x6d, 0x62, 0x65, 0x64, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73,
0x65, 0x12, 0x12, 0x0a, 0x04, 0x6c, 0x69, 0x6e, 0x6b, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52,
0x04, 0x6c, 0x69, 0x6e, 0x6b, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x18,
0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x12, 0x1b, 0x0a,
0x09, 0x73, 0x68, 0x6f, 0x72, 0x74, 0x5f, 0x75, 0x72, 0x6c, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09,
0x52, 0x08, 0x73, 0x68, 0x6f, 0x72, 0x74, 0x55, 0x72, 0x6c, 0x22, 0x87, 0x02, 0x0a, 0x0c, 0x53,
0x65, 0x61, 0x72, 0x63, 0x68, 0x52, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x12, 0x0e, 0x0a, 0x02, 0x69,
0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x6b,
0x69, 0x6e, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6b, 0x69, 0x6e, 0x64, 0x12,
0x14, 0x0a, 0x05, 0x74, 0x69, 0x74, 0x6c, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05,
0x74, 0x69, 0x74, 0x6c, 0x65, 0x12, 0x20, 0x0a, 0x0b, 0x64, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70,
0x74, 0x69, 0x6f, 0x6e, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0b, 0x64, 0x65, 0x73, 0x63,
0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x1d, 0x0a, 0x0a, 0x63, 0x68, 0x61, 0x6e, 0x6e,
0x65, 0x6c, 0x5f, 0x69, 0x64, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x63, 0x68, 0x61,
0x6e, 0x6e, 0x65, 0x6c, 0x49, 0x64, 0x12, 0x23, 0x0a, 0x0d, 0x63, 0x68, 0x61, 0x6e, 0x6e, 0x65,
0x6c, 0x5f, 0x74, 0x69, 0x74, 0x6c, 0x65, 0x18, 0x06, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0c, 0x63,
0x68, 0x61, 0x6e, 0x6e, 0x65, 0x6c, 0x54, 0x69, 0x74, 0x6c, 0x65, 0x12, 0x21, 0x0a, 0x0c, 0x70,
0x75, 0x62, 0x6c, 0x69, 0x73, 0x68, 0x65, 0x64, 0x5f, 0x61, 0x74, 0x18, 0x07, 0x20, 0x01, 0x28,
0x09, 0x52, 0x0b, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x73, 0x68, 0x65, 0x64, 0x41, 0x74, 0x12, 0x22,
0x0a, 0x0c, 0x62, 0x72, 0x6f, 0x61, 0x64, 0x63, 0x61, 0x73, 0x74, 0x69, 0x6e, 0x67, 0x18, 0x08,
0x20, 0x01, 0x28, 0x09, 0x52, 0x0c, 0x62, 0x72, 0x6f, 0x61, 0x64, 0x63, 0x61, 0x73, 0x74, 0x69,
0x6e, 0x67, 0x12, 0x10, 0x0a, 0x03, 0x75, 0x72, 0x6c, 0x18, 0x09, 0x20, 0x01, 0x28, 0x09, 0x52,
0x03, 0x75, 0x72, 0x6c, 0x22, 0x25, 0x0a, 0x0d, 0x53, 0x65, 0x61, 0x72, 0x63, 0x68, 0x52, 0x65,
0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x71, 0x75, 0x65, 0x72, 0x79, 0x18, 0x01,
0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x71, 0x75, 0x65, 0x72, 0x79, 0x22, 0x41, 0x0a, 0x0e, 0x53,
0x65, 0x61, 0x72, 0x63, 0x68, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x2f, 0x0a,
0x07, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x15,
0x2e, 0x79, 0x6f, 0x75, 0x74, 0x75, 0x62, 0x65, 0x2e, 0x53, 0x65, 0x61, 0x72, 0x63, 0x68, 0x52,
0x65, 0x73, 0x75, 0x6c, 0x74, 0x52, 0x07, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x73, 0x32, 0x80,
0x01, 0x0a, 0x07, 0x59, 0x6f, 0x75, 0x74, 0x75, 0x62, 0x65, 0x12, 0x3b, 0x0a, 0x06, 0x53, 0x65,
0x61, 0x72, 0x63, 0x68, 0x12, 0x16, 0x2e, 0x79, 0x6f, 0x75, 0x74, 0x75, 0x62, 0x65, 0x2e, 0x53,
0x65, 0x61, 0x72, 0x63, 0x68, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x17, 0x2e, 0x79,
0x6f, 0x75, 0x74, 0x75, 0x62, 0x65, 0x2e, 0x53, 0x65, 0x61, 0x72, 0x63, 0x68, 0x52, 0x65, 0x73,
0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x38, 0x0a, 0x05, 0x45, 0x6d, 0x62, 0x65, 0x64,
0x12, 0x15, 0x2e, 0x79, 0x6f, 0x75, 0x74, 0x75, 0x62, 0x65, 0x2e, 0x45, 0x6d, 0x62, 0x65, 0x64,
0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x16, 0x2e, 0x79, 0x6f, 0x75, 0x74, 0x75, 0x62,
0x65, 0x2e, 0x45, 0x6d, 0x62, 0x65, 0x64, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22,
0x00, 0x42, 0x11, 0x5a, 0x0f, 0x2e, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x3b, 0x79, 0x6f, 0x75,
0x74, 0x75, 0x62, 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
@@ -287,18 +413,22 @@ func file_proto_youtube_proto_rawDescGZIP() []byte {
return file_proto_youtube_proto_rawDescData
}
var file_proto_youtube_proto_msgTypes = make([]protoimpl.MessageInfo, 3)
var file_proto_youtube_proto_msgTypes = make([]protoimpl.MessageInfo, 5)
var file_proto_youtube_proto_goTypes = []interface{}{
(*SearchResult)(nil), // 0: youtube.SearchResult
(*SearchRequest)(nil), // 1: youtube.SearchRequest
(*SearchResponse)(nil), // 2: youtube.SearchResponse
(*EmbedRequest)(nil), // 0: youtube.EmbedRequest
(*EmbedResponse)(nil), // 1: youtube.EmbedResponse
(*SearchResult)(nil), // 2: youtube.SearchResult
(*SearchRequest)(nil), // 3: youtube.SearchRequest
(*SearchResponse)(nil), // 4: youtube.SearchResponse
}
var file_proto_youtube_proto_depIdxs = []int32{
0, // 0: youtube.SearchResponse.results:type_name -> youtube.SearchResult
1, // 1: youtube.Youtube.Search:input_type -> youtube.SearchRequest
2, // 2: youtube.Youtube.Search:output_type -> youtube.SearchResponse
2, // [2:3] is the sub-list for method output_type
1, // [1:2] is the sub-list for method input_type
2, // 0: youtube.SearchResponse.results:type_name -> youtube.SearchResult
3, // 1: youtube.Youtube.Search:input_type -> youtube.SearchRequest
0, // 2: youtube.Youtube.Embed:input_type -> youtube.EmbedRequest
4, // 3: youtube.Youtube.Search:output_type -> youtube.SearchResponse
1, // 4: youtube.Youtube.Embed:output_type -> youtube.EmbedResponse
3, // [3:5] is the sub-list for method output_type
1, // [1:3] is the sub-list for method input_type
1, // [1:1] is the sub-list for extension type_name
1, // [1:1] is the sub-list for extension extendee
0, // [0:1] is the sub-list for field type_name
@@ -311,7 +441,7 @@ func file_proto_youtube_proto_init() {
}
if !protoimpl.UnsafeEnabled {
file_proto_youtube_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*SearchResult); i {
switch v := v.(*EmbedRequest); i {
case 0:
return &v.state
case 1:
@@ -323,7 +453,7 @@ func file_proto_youtube_proto_init() {
}
}
file_proto_youtube_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*SearchRequest); i {
switch v := v.(*EmbedResponse); i {
case 0:
return &v.state
case 1:
@@ -335,6 +465,30 @@ func file_proto_youtube_proto_init() {
}
}
file_proto_youtube_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*SearchResult); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_proto_youtube_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*SearchRequest); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_proto_youtube_proto_msgTypes[4].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*SearchResponse); i {
case 0:
return &v.state
@@ -353,7 +507,7 @@ func file_proto_youtube_proto_init() {
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_proto_youtube_proto_rawDesc,
NumEnums: 0,
NumMessages: 3,
NumMessages: 5,
NumExtensions: 0,
NumServices: 1,
},

View File

@@ -43,6 +43,7 @@ func NewYoutubeEndpoints() []*api.Endpoint {
type YoutubeService interface {
Search(ctx context.Context, in *SearchRequest, opts ...client.CallOption) (*SearchResponse, error)
Embed(ctx context.Context, in *EmbedRequest, opts ...client.CallOption) (*EmbedResponse, error)
}
type youtubeService struct {
@@ -67,15 +68,27 @@ func (c *youtubeService) Search(ctx context.Context, in *SearchRequest, opts ...
return out, nil
}
func (c *youtubeService) Embed(ctx context.Context, in *EmbedRequest, opts ...client.CallOption) (*EmbedResponse, error) {
req := c.c.NewRequest(c.name, "Youtube.Embed", in)
out := new(EmbedResponse)
err := c.c.Call(ctx, req, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// Server API for Youtube service
type YoutubeHandler interface {
Search(context.Context, *SearchRequest, *SearchResponse) error
Embed(context.Context, *EmbedRequest, *EmbedResponse) error
}
func RegisterYoutubeHandler(s server.Server, hdlr YoutubeHandler, opts ...server.HandlerOption) error {
type youtube interface {
Search(ctx context.Context, in *SearchRequest, out *SearchResponse) error
Embed(ctx context.Context, in *EmbedRequest, out *EmbedResponse) error
}
type Youtube struct {
youtube
@@ -91,3 +104,7 @@ type youtubeHandler struct {
func (h *youtubeHandler) Search(ctx context.Context, in *SearchRequest, out *SearchResponse) error {
return h.YoutubeHandler.Search(ctx, in, out)
}
func (h *youtubeHandler) Embed(ctx context.Context, in *EmbedRequest, out *EmbedResponse) error {
return h.YoutubeHandler.Embed(ctx, in, out)
}

View File

@@ -6,6 +6,22 @@ option go_package = "./proto;youtube";
service Youtube {
rpc Search(SearchRequest) returns (SearchResponse) {}
rpc Embed(EmbedRequest) returns (EmbedResponse) {}
}
// Embed a YouTube video
message EmbedRequest {
// provide the youtube url e.g https://www.youtube.com/watch?v=GWRWZu7XsJ0
string url = 1;
}
message EmbedResponse {
// the embeddable link e.g https://www.youtube.com/watch?v=GWRWZu7XsJ0
string link = 1;
// the script code
string script = 2;
// the short link
string short_url = 3;
}
message SearchResult {