✨ Support insert subscription

This commit is contained in:
chaoqun committed 2024-05-22 22:21:10 +08:00
1 parent 0783510187
commit 373efbcdf5
4 files changed
+84 -8

No files matched your search

+30
View File
@@ -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")
}
+2 -2
View File
@@ -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")
+4 -1
View File
@@ -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
)
+48 -5
View File
@@ -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
}
}