From bf1ac4a6e4e40652aefa5bb428f850d7ae7377dd Mon Sep 17 00:00:00 2001 From: chaoqun <27287694+OpenWaygate@users.noreply.github.com> Date: Sun, 24 Mar 2024 00:03:03 +0800 Subject: [PATCH] :sparkles: Support rate a video --- README.md | 4 ++-- cmd/video/list.go | 4 ++-- cmd/video/rate.go | 30 ++++++++++++++++++++++++++++++ cmd/video/video.go | 2 +- pkg/yutuber/video.go | 21 ++++++++++++++++----- 5 files changed, 51 insertions(+), 10 deletions(-) create mode 100644 cmd/video/rate.go diff --git a/README.md b/README.md index 3e29070..99130ec 100644 --- a/README.md +++ b/README.md @@ -41,8 +41,8 @@ Here are the features that are currently supported by yutu, and the ones that ar - [x] list, 1 - [x] insert, 1600 - [x] update, 50 - - [ ] rate, 50 - - [ ] getRating, 1 + - [x] rate, 50 + - [x] getRating, 1 - [ ] reportAbuse, 50 - [ ] delete, 50 - channels diff --git a/cmd/video/list.go b/cmd/video/list.go index 772f434..a04b359 100644 --- a/cmd/video/list.go +++ b/cmd/video/list.go @@ -13,7 +13,7 @@ var listCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { v := yutuber.NewVideo( yutuber.WithVideoId(id), - yutuber.WithVideoMyRating(myRating), + yutuber.WithVideoRating(rating), ) v.List(parts, output) }, @@ -24,7 +24,7 @@ func init() { parts := []string{"id", "snippet", "status"} listCmd.Flags().StringVarP(&id, "id", "i", "", "ID of the video") - listCmd.Flags().StringVarP(&myRating, "myRating", "r", "", "My rating of the video") + listCmd.Flags().StringVarP(&rating, "rating", "r", "", "My rating of the video") listCmd.Flags().StringVarP(&output, "output", "o", "", "Output format: json or yaml") listCmd.Flags().StringArrayVarP(&parts, "parts", "p", parts, "Comma separated parts") } diff --git a/cmd/video/rate.go b/cmd/video/rate.go new file mode 100644 index 0000000..6558076 --- /dev/null +++ b/cmd/video/rate.go @@ -0,0 +1,30 @@ +package video + +import ( + "github.com/eat-pray-ai/yutu/pkg/yutuber" + + "github.com/spf13/cobra" +) + +var rateCmd = &cobra.Command{ + Use: "rate", + Short: "rate a video on YouTube", + Long: "rate a video on YouTube, with the specified rating", + Run: func(cmd *cobra.Command, args []string) { + v := yutuber.NewVideo( + yutuber.WithVideoId(id), + yutuber.WithVideoRating(rating), + ) + v.Rate() + }, +} + +func init() { + videoCmd.AddCommand(rateCmd) + + rateCmd.Flags().StringVarP(&id, "id", "i", "", "ID of the video") + rateCmd.Flags().StringVarP(&rating, "rating", "r", "", "Rating of the video") + + rateCmd.MarkFlagRequired("id") + rateCmd.MarkFlagRequired("rating") +} diff --git a/cmd/video/video.go b/cmd/video/video.go index 52c3468..73f8e05 100644 --- a/cmd/video/video.go +++ b/cmd/video/video.go @@ -14,7 +14,7 @@ var ( tags []string language string thumbnail string - myRating string + rating string chart string channelId string playListId string diff --git a/pkg/yutuber/video.go b/pkg/yutuber/video.go index 0447e99..2af7f16 100644 --- a/pkg/yutuber/video.go +++ b/pkg/yutuber/video.go @@ -17,6 +17,7 @@ var ( errUpdateVideo error = errors.New("failed to update video") errOpenFile error = errors.New("failed to open file") errSetThumbnail error = errors.New("failed to set thumbnail") + errRating error = errors.New("failed to rate video") errGetRating error = errors.New("failed to get rating") ) @@ -28,7 +29,7 @@ type Video struct { tags []string language string thumbnail string - myRating string + rating string chart string channelId string playlistId string @@ -42,6 +43,7 @@ type VideoService interface { List([]string, string) Insert() Update() + Rate() GetRating() get([]string) *youtube.Video setThumbnail() @@ -65,8 +67,8 @@ func (v *Video) get(parts []string) []*youtube.Video { call := service.Videos.List(parts) if v.id != "" { call = call.Id(v.id) - } else if v.myRating != "" { - call = call.MyRating(v.myRating) + } else if v.rating != "" { + call = call.MyRating(v.rating) } response, err := call.Do() if err != nil { @@ -191,6 +193,15 @@ func (v *Video) Update() { utils.PrintJSON(res) } +func (v *Video) Rate() { + call := service.Videos.Rate(v.id, v.rating) + err := call.Do() + if err != nil { + log.Fatalln(errors.Join(errRating, err), v.id) + } + fmt.Printf("Video %s rated %s\n", v.id, v.rating) +} + func (v *Video) GetRating() { call := service.Videos.GetRating([]string{v.id}) res, err := call.Do() @@ -255,9 +266,9 @@ func WithVideoThumbnail(thumbnail string) VideoOption { } } -func WithVideoMyRating(myRating string) VideoOption { +func WithVideoRating(rating string) VideoOption { return func(v *Video) { - v.myRating = myRating + v.rating = rating } }