✨ Support more flags when list playlist(item)

This commit is contained in:
OpenWaygate committed 2024-05-14 23:59:36 +08:00
1 parent ca23e1595c
commit cff74d4ca8
7 files changed
+189 -49

No files matched your search

+2 -2
View File
@@ -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(
+37 -4
View File
@@ -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",
)
}
+15 -9
View File
@@ -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{
+25 -3
View File
@@ -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",
)
}
+3
View File
@@ -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{
+75 -28
View File
@@ -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
}
}
+32 -3
View File
@@ -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
}
}