From 2aff3cb889b2b61a260609a31f11da691dbebedb Mon Sep 17 00:00:00 2001 From: chaoqun <27287694+OpenWaygate@users.noreply.github.com> Date: Sun, 26 May 2024 18:25:20 +0800 Subject: [PATCH] :sparkles: Support delete item from playlist --- cmd/playlistItem/delete.go | 26 ++++++++++++++++++++++++++ cmd/playlistItem/insert.go | 7 +++---- cmd/playlistItem/list.go | 2 +- pkg/yutuber/playlistItem.go | 22 +++++++++++++++++++--- 4 files changed, 49 insertions(+), 8 deletions(-) create mode 100644 cmd/playlistItem/delete.go diff --git a/cmd/playlistItem/delete.go b/cmd/playlistItem/delete.go new file mode 100644 index 0000000..5f4505a --- /dev/null +++ b/cmd/playlistItem/delete.go @@ -0,0 +1,26 @@ +package playlistItem + +import ( + "github.com/eat-pray-ai/yutu/pkg/yutuber" + "github.com/spf13/cobra" +) + +var deleteCmd = &cobra.Command{ + Use: "delete", + Short: "delete a item from a playlist", + Long: "delete a item from a playlist", + Run: func(cmd *cobra.Command, args []string) { + pi := yutuber.NewPlaylistItem( + yutuber.WithPlaylistItemId(id), + yutuber.WithPlaylistItemOnBehalfOfContentOwner(onBehalfOfContentOwner), + ) + pi.Delete() + }, +} + +func init() { + playlistItemCmd.AddCommand(deleteCmd) + + deleteCmd.Flags().StringVarP(&id, "id", "i", "", "ID of the playlist item to be deleted") + deleteCmd.Flags().StringVarP(&onBehalfOfContentOwner, "onBehalfOfContentOwner", "b", "", "") +} diff --git a/cmd/playlistItem/insert.go b/cmd/playlistItem/insert.go index 0aafeee..b5f77ff 100644 --- a/cmd/playlistItem/insert.go +++ b/cmd/playlistItem/insert.go @@ -7,8 +7,8 @@ import ( var insertCmd = &cobra.Command{ Use: "insert", - Short: "insert playlist item", - Long: "insert playlist item into a playlist", + Short: "insert a playlist item into a playlist", + Long: "insert a playlist item into a playlist", Run: func(cmd *cobra.Command, args []string) { pi := yutuber.NewPlaylistItem( yutuber.WithPlaylistItemTitle(title), @@ -43,8 +43,7 @@ func init() { &kChannelId, "kChannelId", "C", "", "ID of the channel if kind is channel", ) insertCmd.Flags().StringVarP( - &kPlaylistId, "kPlaylistId", "P", "", - "ID of the playlist if kind is playlist", + &kPlaylistId, "kPlaylistId", "P", "", "ID of the playlist if kind is playlist", ) insertCmd.Flags().StringVarP( &playlistId, "playlistId", "p", "", diff --git a/cmd/playlistItem/list.go b/cmd/playlistItem/list.go index 19ec941..2b643a7 100644 --- a/cmd/playlistItem/list.go +++ b/cmd/playlistItem/list.go @@ -26,7 +26,7 @@ func init() { listCmd.Flags().StringVarP(&id, "id", "i", "", "ID of the playlist item") listCmd.Flags().StringVarP( - &playlistId, "playlistId", "l", "", + &playlistId, "playlistId", "I", "", "Return the playlist items within the given playlist", ) listCmd.Flags().Int64VarP( diff --git a/pkg/yutuber/playlistItem.go b/pkg/yutuber/playlistItem.go index 21e1a7f..5434659 100644 --- a/pkg/yutuber/playlistItem.go +++ b/pkg/yutuber/playlistItem.go @@ -11,9 +11,10 @@ import ( ) var ( - errGetPlaylistItem = fmt.Errorf("failed to get playlist item") - errUpdatePlaylistItem = fmt.Errorf("failed to update playlist item") - errInsertPlaylistItem = fmt.Errorf("failed to insert playlist item") + errGetPlaylistItem = errors.New("failed to get playlist item") + errUpdatePlaylistItem = errors.New("failed to update playlist item") + errInsertPlaylistItem = errors.New("failed to insert playlist item") + errDeletePlaylistItem = errors.New("failed to delete playlist item") ) type playlistItem struct { @@ -37,6 +38,7 @@ type PlaylistItem interface { List([]string, string) Insert() Update() + Delete() get([]string) []*youtube.PlaylistItem } @@ -159,6 +161,20 @@ func (pi *playlistItem) Update() { utils.PrintJSON(res) } +func (pi *playlistItem) Delete() { + call := service.PlaylistItems.Delete(pi.id) + if pi.onBehalfOfContentOwner != "" { + call = call.OnBehalfOfContentOwner(pi.onBehalfOfContentOwner) + } + + err := call.Do() + if err != nil { + log.Fatalln(errors.Join(errDeletePlaylistItem, err), pi.id) + } + + fmt.Printf("Playlsit Item %s deleted", pi.id) +} + func WithPlaylistItemId(id string) PlaylistItemOption { return func(p *playlistItem) { p.id = id