diff --git a/cmd/subscription/insert.go b/cmd/subscription/insert.go new file mode 100644 index 0000000..eddde8e --- /dev/null +++ b/cmd/subscription/insert.go @@ -0,0 +1,30 @@ +package subscription + +import ( + "github.com/eat-pray-ai/yutu/pkg/yutuber" + "github.com/spf13/cobra" +) + +var insertCmd = &cobra.Command{ + Use: "insert", + Short: "Insert a subscription", + Long: "Insert a subscription", + Run: func(cmd *cobra.Command, args []string) { + s := yutuber.NewSubscription( + yutuber.WithSubscriptionSubscriberChannelId(subscriberChannelId), + yutuber.WithSubscriptionDescription(desc), + yutuber.WithSubscriptionChannelId(channelId), + yutuber.WithSubscriptionTitle(title), + ) + s.Insert() + }, +} + +func init() { + subscriptionCmd.AddCommand(insertCmd) + + insertCmd.Flags().StringVarP(&subscriberChannelId, "subscriberChannelId", "s", "", "Subscriber's channel ID") + insertCmd.Flags().StringVarP(&desc, "desc", "d", "", "Description of the subscription") + insertCmd.Flags().StringVarP(&channelId, "channelId", "c", "", "ID of the channel to be subscribed") + insertCmd.Flags().StringVarP(&title, "title", "t", "", "Title of the subscription") +} diff --git a/cmd/subscription/list.go b/cmd/subscription/list.go index aa27811..23a9c5e 100644 --- a/cmd/subscription/list.go +++ b/cmd/subscription/list.go @@ -14,7 +14,7 @@ var listCmd = &cobra.Command{ yutuber.WithSubscriptionId(id), yutuber.WithSubscriptionChannelId(channelId), yutuber.WithSubscriptionForChannelId(forChannelId), - yutuber.WithSubscriptionMaxResult(maxResult), + yutuber.WithSubscriptionMaxResults(maxResults), yutuber.WithSubscriptionMine(mine), yutuber.WithSubscriptionMyRecentSubscribers(myRecentSubscribers), yutuber.WithSubscriptionMySubscribers(mySubscribers), @@ -42,7 +42,7 @@ func init() { "Return the subscriptions to the subset of these channels that the authenticated user is subscribed to", ) listCmd.Flags().Int64VarP( - &maxResult, "maxResult", "n", 5, + &maxResults, "maxResults", "n", 5, "Specifies the maximum number of items that should be returned", ) listCmd.Flags().StringVarP(&mine, "mine", "m", "", "true or false") diff --git a/cmd/subscription/subscription.go b/cmd/subscription/subscription.go index b83c99e..ff084ad 100644 --- a/cmd/subscription/subscription.go +++ b/cmd/subscription/subscription.go @@ -7,15 +7,18 @@ import ( var ( id string + subscriberChannelId string + desc string channelId string forChannelId string - maxResult int64 + maxResults int64 mine string myRecentSubscribers string mySubscribers string onBehalfOfContentOwner string onBehalfOfContentOwnerChannel string order string + title string parts []string output string ) diff --git a/pkg/yutuber/subscription.go b/pkg/yutuber/subscription.go index 00d39d0..5892b33 100644 --- a/pkg/yutuber/subscription.go +++ b/pkg/yutuber/subscription.go @@ -12,25 +12,29 @@ import ( var ( errGetSubscription = errors.New("failed to get subscription") errDeleteSubscription = errors.New("failed to delete subscription") + errInsertSubscription = errors.New("failed to insert subscription") ) type subscription struct { id string + subscriberChannelId string + desc string channelId string forChannelId string - maxResult int64 + maxResults int64 mine string myRecentSubscribers string mySubscribers string onBehalfOfContentOwner string onBehalfOfContentOwnerChannel string order string + title string } type Subscription interface { get([]string) []*youtube.Subscription List([]string, string) - // Insert() + Insert() Delete() } @@ -58,7 +62,7 @@ func (s *subscription) get(parts []string) []*youtube.Subscription { if s.forChannelId != "" { call = call.ForChannelId(s.forChannelId) } - call = call.MaxResults(s.maxResult) + call = call.MaxResults(s.maxResults) if s.mine == "true" { call = call.Mine(true) @@ -109,6 +113,27 @@ func (s *subscription) List(parts []string, output string) { } } +func (s *subscription) Insert() { + subscription := &youtube.Subscription{ + Snippet: &youtube.SubscriptionSnippet{ + ChannelId: s.subscriberChannelId, + Description: s.desc, + ResourceId: &youtube.ResourceId{ + ChannelId: s.channelId, + }, + Title: s.title, + }, + } + + call := service.Subscriptions.Insert([]string{"snippet"}, subscription) + res, err := call.Do() + if err != nil { + log.Fatalln(errors.Join(errInsertSubscription, err)) + } + fmt.Println("Subscription inserted") + utils.PrintJSON(res) +} + func (s *subscription) Delete() { call := service.Subscriptions.Delete(s.id) err := call.Do() @@ -125,6 +150,18 @@ func WithSubscriptionId(id string) SubscriptionOption { } } +func WithSubscriptionSubscriberChannelId(id string) SubscriptionOption { + return func(s *subscription) { + s.subscriberChannelId = id + } +} + +func WithSubscriptionDescription(desc string) SubscriptionOption { + return func(s *subscription) { + s.desc = desc + } +} + func WithSubscriptionChannelId(channelId string) SubscriptionOption { return func(s *subscription) { s.channelId = channelId @@ -137,9 +174,9 @@ func WithSubscriptionForChannelId(forChannelId string) SubscriptionOption { } } -func WithSubscriptionMaxResult(maxResult int64) SubscriptionOption { +func WithSubscriptionMaxResults(maxResults int64) SubscriptionOption { return func(s *subscription) { - s.maxResult = maxResult + s.maxResults = maxResults } } @@ -178,3 +215,9 @@ func WithSubscriptionOrder(order string) SubscriptionOption { s.order = order } } + +func WithSubscriptionTitle(title string) SubscriptionOption { + return func(s *subscription) { + s.title = title + } +}