✨ Support update playlist

This commit is contained in:
OpenWaygate committed 2024-03-13 10:17:25 +08:00
1 parent 110c852301
commit f873e08122
2 files changed
+67

No files matched your search

+36
View File
@@ -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")
}
+31
View File
@@ -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 {