diff --git a/cmd/video/getRating.go b/cmd/video/getRating.go new file mode 100644 index 0000000..51eab60 --- /dev/null +++ b/cmd/video/getRating.go @@ -0,0 +1,26 @@ +package video + +import ( + "github.com/eat-pray-ai/yutu/pkg/yutuber" + + "github.com/spf13/cobra" +) + +var getRatingCmd = &cobra.Command{ + Use: "getRating", + Short: "get the rating of a video", + Long: "get the rating of a video, with the specified video ID", + Run: func(cmd *cobra.Command, args []string) { + v := yutuber.NewVideo( + yutuber.WithVideoId(id), + ) + v.GetRating() + }, +} + +func init() { + videoCmd.AddCommand(getRatingCmd) + + getRatingCmd.Flags().StringVarP(&id, "id", "i", "", "ID of the video") + getRatingCmd.MarkFlagRequired("id") +} diff --git a/cmd/video/video.go b/cmd/video/video.go index 890fdc6..52c3468 100644 --- a/cmd/video/video.go +++ b/cmd/video/video.go @@ -14,6 +14,8 @@ var ( tags []string language string thumbnail string + myRating string + chart string channelId string playListId string category string diff --git a/pkg/yutuber/video.go b/pkg/yutuber/video.go index 2842e06..0447e99 100644 --- a/pkg/yutuber/video.go +++ b/pkg/yutuber/video.go @@ -17,28 +17,32 @@ 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") + errGetRating error = errors.New("failed to get rating") ) type Video struct { id string - path string + file string title string desc string tags []string language string thumbnail string - forKids bool - embeddable bool - category string - privacy string + myRating string + chart string channelId string playlistId string + category string + privacy string + forKids bool + embeddable bool } type VideoService interface { List([]string, string) Insert() Update() + GetRating() get([]string) *youtube.Video setThumbnail() validate() @@ -61,6 +65,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) } response, err := call.Do() if err != nil { @@ -86,9 +92,9 @@ func (v *Video) List(parts []string, output string) { } func (v *Video) Insert() { - file, err := os.Open(v.path) + file, err := os.Open(v.file) if err != nil { - log.Fatalln(errors.Join(errOpenFile, err), v.path) + log.Fatalln(errors.Join(errOpenFile, err), v.file) } defer file.Close() @@ -185,6 +191,16 @@ func (v *Video) Update() { utils.PrintJSON(res) } +func (v *Video) GetRating() { + call := service.Videos.GetRating([]string{v.id}) + res, err := call.Do() + if err != nil { + log.Fatalln(errors.Join(errGetRating, err), v.id) + } + + utils.PrintJSON(res) +} + func (v *Video) setThumbnail(thumbnail string, service *youtube.Service) { file, err := os.Open(thumbnail) if err != nil { @@ -203,9 +219,9 @@ func WithVideoId(id string) VideoOption { } } -func WithVideoPath(path string) VideoOption { +func WithVideoFile(file string) VideoOption { return func(v *Video) { - v.path = path + v.file = file } } @@ -239,6 +255,18 @@ func WithVideoThumbnail(thumbnail string) VideoOption { } } +func WithVideoMyRating(myRating string) VideoOption { + return func(v *Video) { + v.myRating = myRating + } +} + +func WithVideoChart(chart string) VideoOption { + return func(v *Video) { + v.chart = chart + } +} + func WithVideoForKids(forKids bool) VideoOption { return func(v *Video) { v.forKids = forKids