diff --git a/cmd/playlist/update.go b/cmd/playlist/update.go new file mode 100644 index 0000000..75d9d42 --- /dev/null +++ b/cmd/playlist/update.go @@ -0,0 +1,36 @@ +package playlist + +import ( + "github.com/eat-pray-ai/yutu/pkg/yutuber" + "github.com/spf13/cobra" +) + +var updateCmd = &cobra.Command{ + Use: "update", + Short: "update an existing playlist", + Long: "update an existing playlist, with the specified title, description, tags, etc.", + Run: func(cmd *cobra.Command, args []string) { + p := yutuber.NewPlaylist( + yutuber.WithPlaylistId(id), + yutuber.WithPlaylistTitle(title), + yutuber.WithPlaylistDesc(desc), + yutuber.WithPlaylistTags(tags), + yutuber.WithPlaylistLanguage(language), + yutuber.WithPlaylistPrivacy(privacy), + ) + p.Update() + }, +} + +func init() { + playlistCmd.AddCommand(updateCmd) + + updateCmd.Flags().StringVarP(&id, "id", "i", "", "ID of the playlist") + updateCmd.Flags().StringVarP(&title, "title", "t", "", "Title of the playlist") + updateCmd.Flags().StringVarP(&desc, "desc", "d", "", "Description of the playlist") + updateCmd.Flags().StringVarP(&tags, "tags", "g", "", "Comma separated tags") + updateCmd.Flags().StringVarP(&language, "language", "l", "", "Language of the playlist") + updateCmd.Flags().StringVarP(&privacy, "privacy", "p", "", "Privacy status of the playlist") + + updateCmd.MarkFlagRequired("id") +} diff --git a/pkg/yutuber/playlist.go b/pkg/yutuber/playlist.go index fd94b5f..6cfaa08 100644 --- a/pkg/yutuber/playlist.go +++ b/pkg/yutuber/playlist.go @@ -12,6 +12,7 @@ import ( var ( errGetPlaylist error = errors.New("failed to get playlist") errInsertPlaylist error = errors.New("failed to insert playlist") + errUpdatePlaylist error = errors.New("failed to update playlist") ) type Playlist struct { @@ -27,6 +28,7 @@ type Playlist struct { type PlaylistService interface { List() Insert() + Update() get(parts []string) []*youtube.Playlist } @@ -47,6 +49,7 @@ func (p *Playlist) List() { for _, playlist := range playlists { fmt.Printf(" ID: %s\n", playlist.Id) fmt.Printf(" Title: %s\n", playlist.Snippet.Title) + fmt.Printf(" Tags: %s\n", strings.Join(playlist.Snippet.Tags, ", ")) fmt.Printf(" Description: %s\n", playlist.Snippet.Description) fmt.Printf("Published At: %s\n", playlist.Snippet.PublishedAt) fmt.Printf(" Channel: %s\n\n", playlist.Snippet.ChannelId) @@ -76,6 +79,34 @@ func (p *Playlist) Insert() { fmt.Printf("Playlist %s inserted\n", playlist.Id) } +func (p *Playlist) Update() { + playlist := p.get([]string{"id", "snippet", "status"})[0] + if p.title != "" { + playlist.Snippet.Title = p.title + } + if p.desc != "" { + playlist.Snippet.Description = p.desc + } + if p.tags != nil { + playlist.Snippet.Tags = p.tags + } + if p.language != "" { + playlist.Snippet.DefaultLanguage = p.language + } + if p.privacy != "" { + playlist.Status.PrivacyStatus = p.privacy + } + + call := service.Playlists.Update([]string{"snippet", "status"}, playlist) + _, err := call.Do() + if err != nil { + log.Fatalln(errors.Join(errUpdatePlaylist, err), p.id) + } + + fmt.Println("Playlist updated:") + p.List() +} + func (p *Playlist) get(parts []string) []*youtube.Playlist { call := service.Playlists.List(parts) switch {