From 69e97907f15df2ed2a62b8011dc02f5a9282a89f Mon Sep 17 00:00:00 2001 From: Suvorov Daniil Date: Mon, 4 Apr 2022 17:15:19 +0300 Subject: [PATCH] feat: add video start & stop streaming --- api/params/video.go | 102 +++++++++++++++++++++++++++++++++++++++ api/params/video_test.go | 42 ++++++++++++++++ api/video.go | 24 +++++++++ api/video_test.go | 16 ++++++ object/video.go | 17 +++++++ 5 files changed, 201 insertions(+) diff --git a/api/params/video.go b/api/params/video.go index da2f1bef..2e68b08d 100644 --- a/api/params/video.go +++ b/api/params/video.go @@ -1142,3 +1142,105 @@ func (b *VideoSearchBuilder) LegalOwner(v int) *VideoSearchBuilder { b.Params["legal_owner"] = v return b } + +// VideoStartStreamingBuilder builder. +// +// https://vk.com/dev/video.startStreaming +type VideoStartStreamingBuilder struct { + api.Params +} + +// NewVideoStartStreamingBuilder func. +func NewVideoStartStreamingBuilder() *VideoStartStreamingBuilder { + return &VideoStartStreamingBuilder{api.Params{}} +} + +// VideoID parameter. +func (b *VideoStartStreamingBuilder) VideoID(v int) *VideoStartStreamingBuilder { + b.Params["video_id"] = v + return b +} + +// Name parameter. +func (b *VideoStartStreamingBuilder) Name(v string) *VideoStartStreamingBuilder { + b.Params["name"] = v + return b +} + +// Description parameter. +func (b *VideoStartStreamingBuilder) Description(v string) *VideoStartStreamingBuilder { + b.Params["description"] = v + return b +} + +// Wallpost parameter. +func (b *VideoStartStreamingBuilder) Wallpost(v bool) *VideoStartStreamingBuilder { + b.Params["wallpost"] = v + return b +} + +// GroupID parameter. +func (b *VideoStartStreamingBuilder) GroupID(v int) *VideoStartStreamingBuilder { + b.Params["group_id"] = v + return b +} + +// PrivacyView parameter. +func (b *VideoStartStreamingBuilder) PrivacyView(v []string) *VideoStartStreamingBuilder { + b.Params["privacy_view"] = v + return b +} + +// PrivacyComment parameter. +func (b *VideoStartStreamingBuilder) PrivacyComment(v []string) *VideoStartStreamingBuilder { + b.Params["privacy_comment"] = v + return b +} + +// NoComments parameter. +func (b *VideoStartStreamingBuilder) NoComments(v bool) *VideoStartStreamingBuilder { + b.Params["no_comments"] = v + return b +} + +// CategoryID parameter. +func (b *VideoStartStreamingBuilder) CategoryID(v int) *VideoStartStreamingBuilder { + b.Params["category_id"] = v + return b +} + +// Publish parameter. +func (b *VideoStartStreamingBuilder) Publish(v bool) *VideoStartStreamingBuilder { + b.Params["publish"] = v + return b +} + +// VideoStopStreamingBuilder builder. +// +// https://vk.com/dev/video.stopStreaming +type VideoStopStreamingBuilder struct { + api.Params +} + +// NewVideoStopStreamingBuilder func. +func NewVideoStopStreamingBuilder() *VideoStopStreamingBuilder { + return &VideoStopStreamingBuilder{api.Params{}} +} + +// GroupID parameter. +func (b *VideoStopStreamingBuilder) GroupID(v int) *VideoStopStreamingBuilder { + b.Params["group_id"] = v + return b +} + +// OwnerID parameter. +func (b *VideoStopStreamingBuilder) OwnerID(v int) *VideoStopStreamingBuilder { + b.Params["owner_id"] = v + return b +} + +// VideoID parameter. +func (b *VideoStopStreamingBuilder) VideoID(v int) *VideoStopStreamingBuilder { + b.Params["video_id"] = v + return b +} diff --git a/api/params/video_test.go b/api/params/video_test.go index 9a55d010..d2738834 100644 --- a/api/params/video_test.go +++ b/api/params/video_test.go @@ -444,3 +444,45 @@ func TestVideoSearchBuilder(t *testing.T) { assert.Equal(t, b.Params["legal"], true) assert.Equal(t, b.Params["legal_owner"], 1) } + +func TestVideoStartStreamingBuilder(t *testing.T) { + t.Parallel() + + b := params.NewVideoStartStreamingBuilder() + + b.VideoID(1) + b.Name("text") + b.Description("text") + b.Wallpost(true) + b.GroupID(1) + b.PrivacyView([]string{"text"}) + b.PrivacyComment([]string{"text"}) + b.NoComments(true) + b.CategoryID(1) + b.Publish(true) + + assert.Equal(t, b.Params["video_id"], 1) + assert.Equal(t, b.Params["name"], "text") + assert.Equal(t, b.Params["description"], "text") + assert.Equal(t, b.Params["wallpost"], true) + assert.Equal(t, b.Params["group_id"], 1) + assert.Equal(t, b.Params["privacy_view"], []string{"text"}) + assert.Equal(t, b.Params["privacy_comment"], []string{"text"}) + assert.Equal(t, b.Params["no_comments"], true) + assert.Equal(t, b.Params["category_id"], 1) + assert.Equal(t, b.Params["publish"], true) +} + +func TestVideoStopStreamingBuilder(t *testing.T) { + t.Parallel() + + b := params.NewVideoStopStreamingBuilder() + + b.GroupID(1) + b.OwnerID(1) + b.VideoID(1) + + assert.Equal(t, b.Params["group_id"], 1) + assert.Equal(t, b.Params["owner_id"], 1) + assert.Equal(t, b.Params["video_id"], 1) +} diff --git a/api/video.go b/api/video.go index be6b09df..01b7f83e 100644 --- a/api/video.go +++ b/api/video.go @@ -347,3 +347,27 @@ func (vk *VK) VideoSearchExtended(params Params) (response VideoSearchExtendedRe return } + +// VideoStartStreamingResponse struct. +type VideoStartStreamingResponse object.VideoLive + +// VideoStartStreaming method. +// +// https://vk.com/dev/video.startStreaming +func (vk *VK) VideoStartStreaming(params Params) (response VideoStartStreamingResponse, err error) { + err = vk.RequestUnmarshal("video.startStreaming", &response, params) + return +} + +// VideoStopStreamingResponse struct. +type VideoStopStreamingResponse struct { + UniqueViewers int `json:"unique_viewers"` +} + +// VideoStopStreaming method. +// +// https://vk.com/dev/video.stopStreaming +func (vk *VK) VideoStopStreaming(params Params) (response VideoStopStreamingResponse, err error) { + err = vk.RequestUnmarshal("video.stopStreaming", &response, params) + return +} diff --git a/api/video_test.go b/api/video_test.go index 47ca8ecb..19d4470f 100644 --- a/api/video_test.go +++ b/api/video_test.go @@ -326,3 +326,19 @@ func TestVK_VideoSearchExtended(t *testing.T) { assert.NotEmpty(t, res.Count) assert.NotEmpty(t, res.Items) } + +func TestVK_VideoStartStreaming(t *testing.T) { + t.Parallel() + + needUserToken(t) + + res, err := vkUser.VideoStartStreaming(nil) + noError(t, err) + + assert.NotEmpty(t, res) + + _, err = vkUser.VideoStopStreaming(api.Params{ + "video_id": res.VideoID, + }) + noError(t, err) +} diff --git a/object/video.go b/object/video.go index 31be189d..d86143a9 100644 --- a/object/video.go +++ b/object/video.go @@ -298,6 +298,23 @@ type VideoVideoImage struct { WithPadding BaseBoolInt `json:"with_padding"` } +// VideoLive struct. +type VideoLive struct { + OwnerID int `json:"owner_id"` + VideoID int `json:"video_id"` + Name string `json:"name"` + Description string `json:"description"` + AccessKey string `json:"access_key"` + Stream VideoLiveStream `json:"stream"` +} + +// VideoLiveStream struct. +type VideoLiveStream struct { + URL string `json:"url"` + Key string `json:"key"` + OKMPURL string `json:"okmp_url"` +} + // VideoLiveCategory struct. type VideoLiveCategory struct { ID int `json:"id"`