Skip to content

Commit

Permalink
✨ Support rate a video
Browse files Browse the repository at this point in the history
  • Loading branch information
OpenWaygate committed Mar 23, 2024
1 parent 6a77377 commit bf1ac4a
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions cmd/video/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
Expand All @@ -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")
}
30 changes: 30 additions & 0 deletions cmd/video/rate.go
Original file line number Diff line number Diff line change
@@ -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")
}
2 changes: 1 addition & 1 deletion cmd/video/video.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var (
tags []string
language string
thumbnail string
myRating string
rating string
chart string
channelId string
playListId string
Expand Down
21 changes: 16 additions & 5 deletions pkg/yutuber/video.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)

Expand All @@ -28,7 +29,7 @@ type Video struct {
tags []string
language string
thumbnail string
myRating string
rating string
chart string
channelId string
playlistId string
Expand All @@ -42,6 +43,7 @@ type VideoService interface {
List([]string, string)
Insert()
Update()
Rate()
GetRating()
get([]string) *youtube.Video
setThumbnail()
Expand All @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
}
}

Expand Down

0 comments on commit bf1ac4a

Please # to comment.