diff --git a/cmd/member/list.go b/cmd/member/list.go index 0256613..827c80f 100644 --- a/cmd/member/list.go +++ b/cmd/member/list.go @@ -32,11 +32,11 @@ func init() { "Filter members in the results set to the ones that have access to a level", ) listCmd.Flags().Int64VarP( - &maxResults, "maxResults", "m", 5, + &maxResults, "maxResults", "n", 5, "Specifies the maximum number of items that should be returned", ) listCmd.Flags().StringVarP( - &mode, "mode", "o", "", + &mode, "mode", "m", "", "listMembersModeUnknown, updates or all_current(default)", ) listCmd.Flags().StringSliceVarP( diff --git a/cmd/playlist/list.go b/cmd/playlist/list.go index 9e8185b..fd8873e 100644 --- a/cmd/playlist/list.go +++ b/cmd/playlist/list.go @@ -14,6 +14,11 @@ var listCmd = &cobra.Command{ p := yutuber.NewPlaylist( yutuber.WithPlaylistId(id), yutuber.WithPlaylistChannelId(channelId), + yutuber.WithPlaylistHl(hl), + yutuber.WithPlaylistMaxResults(maxResults), + yutuber.WithPlaylistMine(mine), + yutuber.WithPlaylistOnBehalfOfContentOwner(onBehalfOfContentOwner), + yutuber.WithPlaylistOnBehalfOfContentOwnerChannel(onBehalfOfContentOwnerChannel), ) p.List(parts, output) }, @@ -22,8 +27,36 @@ var listCmd = &cobra.Command{ func init() { playlistCmd.AddCommand(listCmd) - listCmd.Flags().StringVarP(&id, "id", "i", "", "Return the playlists with the given IDs for Stubby or Apiary.") - listCmd.Flags().StringVarP(&channelId, "channelId", "c", "", "Return the playlists owned by the specified channel ID") - listCmd.Flags().StringArrayVarP(&parts, "parts", "p", []string{"id", "snippet", "status"}, "Comma separated parts") - listCmd.Flags().StringVarP(&output, "output", "o", "", "Output format: json or yaml") + listCmd.Flags().StringVarP( + &id, "id", "i", "", + "Return the playlists with the given IDs for Stubby or Apiary.", + ) + listCmd.Flags().StringVarP( + &channelId, "channelId", "c", "", + "Return the playlists owned by the specified channel ID", + ) + listCmd.Flags().StringVarP( + &hl, "hl", "l", "", "Return content in specified language", + ) + listCmd.Flags().Int64VarP( + &maxResults, "maxResults", "n", 5, + "Specifies the maximum number of items that should be returned", + ) + listCmd.Flags().StringVarP( + &mine, "mine", "m", "", + "Return the playlists owned by the authenticated user", + ) + listCmd.Flags().StringVarP( + &onBehalfOfContentOwner, "onBehalfOfContentOwner", "b", "", "", + ) + listCmd.Flags().StringVarP( + &onBehalfOfContentOwnerChannel, "onBehalfOfContentOwnerChannel", "B", "", "", + ) + listCmd.Flags().StringArrayVarP( + &parts, "parts", "p", []string{"id", "snippet", "status"}, + "Comma separated parts", + ) + listCmd.Flags().StringVarP( + &output, "output", "o", "", "Output format: json or yaml", + ) } diff --git a/cmd/playlist/playlist.go b/cmd/playlist/playlist.go index 0d5d3a3..119870e 100644 --- a/cmd/playlist/playlist.go +++ b/cmd/playlist/playlist.go @@ -7,15 +7,21 @@ import ( ) var ( - id string - title string - desc string - tags []string - language string - channelId string - privacy string - output string - parts []string + id string + title string + desc string + hl string + maxResults int64 + mine string + tags []string + language string + channelId string + privacy string + output string + parts []string + + onBehalfOfContentOwner string + onBehalfOfContentOwnerChannel string ) var playlistCmd = &cobra.Command{ diff --git a/cmd/playlistItem/list.go b/cmd/playlistItem/list.go index 81895e1..19ec941 100644 --- a/cmd/playlistItem/list.go +++ b/cmd/playlistItem/list.go @@ -13,6 +13,9 @@ var listCmd = &cobra.Command{ pi := yutuber.NewPlaylistItem( yutuber.WithPlaylistItemId(id), yutuber.WithPlaylistItemPlaylistId(playlistId), + yutuber.WithPlaylistItemMaxResults(maxResults), + yutuber.WithPlaylistItemVideoId(videoId), + yutuber.WithPlaylistItemOnBehalfOfContentOwner(onBehalfOfContentOwner), ) pi.List(parts, output) }, @@ -22,7 +25,26 @@ func init() { playlistItemCmd.AddCommand(listCmd) listCmd.Flags().StringVarP(&id, "id", "i", "", "ID of the playlist item") - listCmd.Flags().StringVarP(&playlistId, "playlistId", "l", "", "Return the playlist items within the given playlist") - listCmd.Flags().StringVarP(&output, "output", "o", "", "Output format: json or yaml") - listCmd.Flags().StringArrayVarP(&parts, "parts", "p", []string{"id", "snippet", "status"}, "Comma separated parts") + listCmd.Flags().StringVarP( + &playlistId, "playlistId", "l", "", + "Return the playlist items within the given playlist", + ) + listCmd.Flags().Int64VarP( + &maxResults, "maxResults", "n", 5, + "Specifies the maximum number of items that should be returned", + ) + listCmd.Flags().StringVarP( + &videoId, "videoId", "v", "", + "Return the playlist items associated with the given video ID", + ) + listCmd.Flags().StringVarP( + &onBehalfOfContentOwner, "onBehalfOfContentOwner", "b", "", "", + ) + listCmd.Flags().StringVarP( + &output, "output", "o", "", "Output format: json or yaml", + ) + listCmd.Flags().StringArrayVarP( + &parts, "parts", "p", []string{"id", "snippet", "status"}, + "Comma separated parts", + ) } diff --git a/cmd/playlistItem/playlistItem.go b/cmd/playlistItem/playlistItem.go index a656392..e18452f 100644 --- a/cmd/playlistItem/playlistItem.go +++ b/cmd/playlistItem/playlistItem.go @@ -12,9 +12,12 @@ var ( videoId string playlistId string channelId string + maxResults int64 privacy string output string parts []string + + onBehalfOfContentOwner string ) var playlistItemCmd = &cobra.Command{ diff --git a/pkg/yutuber/playlist.go b/pkg/yutuber/playlist.go index fdbf713..ee00577 100644 --- a/pkg/yutuber/playlist.go +++ b/pkg/yutuber/playlist.go @@ -3,10 +3,9 @@ package yutuber import ( "errors" "fmt" - "log" - "github.com/eat-pray-ai/yutu/pkg/auth" "github.com/eat-pray-ai/yutu/pkg/utils" + "log" "google.golang.org/api/youtube/v3" ) @@ -18,13 +17,19 @@ var ( ) type playlist struct { - id string - title string - desc string - tags []string - language string - channelId string - privacy string + id string + title string + desc string + hl string + maxResults int64 + mine string + tags []string + language string + channelId string + privacy string + + onBehalfOfContentOwner string + onBehalfOfContentOwnerChannel string } type Playlist interface { @@ -47,6 +52,37 @@ func NewPlaylist(opts ...PlaylistOption) Playlist { return p } +func (p *playlist) get(parts []string) []*youtube.Playlist { + call := service.Playlists.List(parts) + + if p.id != "" { + call = call.Id(p.id) + } + if p.hl != "" { + call = call.Hl(p.hl) + } + if p.mine == "true" { + call = call.Mine(true) + } else if p.mine == "false" { + call = call.Mine(false) + } + + call = call.MaxResults(p.maxResults) + if p.onBehalfOfContentOwner != "" { + call = call.OnBehalfOfContentOwner(p.onBehalfOfContentOwner) + } + if p.onBehalfOfContentOwnerChannel != "" { + call = call.OnBehalfOfContentOwnerChannel(p.onBehalfOfContentOwnerChannel) + } + + res, err := call.Do() + if err != nil { + log.Fatalln(errors.Join(errGetPlaylist, err), p.id) + } + + return res.Items +} + func (p *playlist) List(parts []string, output string) { playlists := p.get(parts) switch output { @@ -114,25 +150,6 @@ func (p *playlist) Update() { utils.PrintJSON(data) } -func (p *playlist) get(parts []string) []*youtube.Playlist { - call := service.Playlists.List(parts) - switch { - case p.id != "": - call = call.Id(p.id) - case p.channelId != "": - call = call.ChannelId(p.channelId) - default: - call = call.Mine(true) - } - - res, err := call.Do() - if err != nil { - log.Fatalln(errors.Join(errGetPlaylist, err), p.id) - } - - return res.Items -} - func WithPlaylistId(id string) PlaylistOption { return func(p *playlist) { p.id = id @@ -174,3 +191,33 @@ func WithPlaylistPrivacy(privacy string) PlaylistOption { p.privacy = privacy } } + +func WithPlaylistHl(hl string) PlaylistOption { + return func(p *playlist) { + p.hl = hl + } +} + +func WithPlaylistMaxResults(maxResults int64) PlaylistOption { + return func(p *playlist) { + p.maxResults = maxResults + } +} + +func WithPlaylistMine(mine string) PlaylistOption { + return func(p *playlist) { + p.mine = mine + } +} + +func WithPlaylistOnBehalfOfContentOwner(contentOwner string) PlaylistOption { + return func(p *playlist) { + p.onBehalfOfContentOwner = contentOwner + } +} + +func WithPlaylistOnBehalfOfContentOwnerChannel(channel string) PlaylistOption { + return func(p *playlist) { + p.onBehalfOfContentOwnerChannel = channel + } +} diff --git a/pkg/yutuber/playlistItem.go b/pkg/yutuber/playlistItem.go index fc8979c..5435da7 100644 --- a/pkg/yutuber/playlistItem.go +++ b/pkg/yutuber/playlistItem.go @@ -24,6 +24,9 @@ type playlistItem struct { playlistId string channelId string privacy string + maxResults int64 + + onBehalfOfContentOwner string } type PlaylistItem interface { @@ -50,9 +53,18 @@ func (pi *playlistItem) get(parts []string) []*youtube.PlaylistItem { call := service.PlaylistItems.List(parts) if pi.id != "" { call = call.Id(pi.id) - } else if pi.playlistId != "" { + } + if pi.playlistId != "" { call = call.PlaylistId(pi.playlistId) } + if pi.onBehalfOfContentOwner != "" { + call = call.OnBehalfOfContentOwner(pi.onBehalfOfContentOwner) + } + if pi.videoId != "" { + call = call.VideoId(pi.videoId) + } + + call = call.MaxResults(pi.maxResults) response, err := call.Do() if err != nil { log.Fatalln(errors.Join(errGetPlaylistItem, err), pi.id) @@ -77,6 +89,7 @@ func (pi *playlistItem) List(parts []string, output string) { } func (pi *playlistItem) Insert() { + // TODO: support Kind of `youtube#channel` and `youtube#playlist` playlistItem := &youtube.PlaylistItem{ Snippet: &youtube.PlaylistItemSnippet{ Title: pi.title, @@ -93,7 +106,9 @@ func (pi *playlistItem) Insert() { }, } - call := service.PlaylistItems.Insert([]string{"snippet", "status"}, playlistItem) + call := service.PlaylistItems.Insert( + []string{"snippet", "status"}, playlistItem, + ) res, err := call.Do() if err != nil { log.Fatalln(errors.Join(errInsertPlaylistItem, err), pi.videoId) @@ -114,7 +129,9 @@ func (pi *playlistItem) Update() { playlistItem.Status.PrivacyStatus = pi.privacy } - call := service.PlaylistItems.Update([]string{"snippet", "status"}, playlistItem) + call := service.PlaylistItems.Update( + []string{"snippet", "status"}, playlistItem, + ) res, err := call.Do() if err != nil { log.Fatalln(errors.Join(errUpdatePlaylistItem, err), pi.id) @@ -164,3 +181,15 @@ func WithPlaylistItemPrivacy(privacy string) PlaylistItemOption { p.privacy = privacy } } + +func WithPlaylistItemMaxResults(maxResults int64) PlaylistItemOption { + return func(p *playlistItem) { + p.maxResults = maxResults + } +} + +func WithPlaylistItemOnBehalfOfContentOwner(onBehalfOfContentOwner string) PlaylistItemOption { + return func(p *playlistItem) { + p.onBehalfOfContentOwner = onBehalfOfContentOwner + } +}