// Copyright 2025 eat-pray-ai & OpenWaygate
// SPDX-License-Identifier: Apache-2.0
package subscription
import (
"errors"
"fmt"
"io"
"math"
"github.com/eat-pray-ai/yutu/pkg"
"github.com/eat-pray-ai/yutu/pkg/common"
"github.com/eat-pray-ai/yutu/pkg/utils"
"github.com/jedib0t/go-pretty/v6/table"
"google.golang.org/api/youtube/v3"
)
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 {
*common.Fields
Ids []string `yaml:"ids" json:"ids"`
SubscriberChannelId string `yaml:"subscriber_channel_id" json:"subscriber_channel_id"`
Description string `yaml:"description" json:"description"`
ChannelId string `yaml:"channel_id" json:"channel_id"`
ForChannelId string `yaml:"for_channel_id" json:"for_channel_id"`
MaxResults int64 `yaml:"max_results" json:"max_results"`
Mine *bool `yaml:"mine" json:"mine"`
MyRecentSubscribers *bool `yaml:"my_recent_subscribers" json:"my_recent_subscribers"`
MySubscribers *bool `yaml:"my_subscribers" json:"my_subscribers"`
OnBehalfOfContentOwner string `yaml:"on_behalf_of_content_owner" json:"on_behalf_of_content_owner"`
OnBehalfOfContentOwnerChannel string `yaml:"on_behalf_of_content_owner_channel" json:"on_behalf_of_content_owner_channel"`
Order string `yaml:"order" json:"order"`
Title string `yaml:"title" json:"title"`
}
type ISubscription[T any] interface {
Get() ([]*T, error)
List(io.Writer) error
Insert(io.Writer) error
Delete(io.Writer) error
}
type Option func(*Subscription)
func NewSubscription(opts ...Option) ISubscription[youtube.Subscription] {
s := &Subscription{Fields: &common.Fields{}}
for _, opt := range opts {
opt(s)
}
return s
}
func (s *Subscription) Get() ([]*youtube.Subscription, error) {
s.EnsureService()
call := s.Service.Subscriptions.List(s.Parts)
if len(s.Ids) > 0 {
call = call.Id(s.Ids...)
}
if s.ChannelId != "" {
call = call.ChannelId(s.ChannelId)
}
if s.ForChannelId != "" {
call = call.ForChannelId(s.ForChannelId)
}
if s.Mine != nil {
call = call.Mine(*s.Mine)
}
if s.MyRecentSubscribers != nil {
call = call.MyRecentSubscribers(*s.MyRecentSubscribers)
}
if s.MySubscribers != nil {
call = call.MySubscribers(*s.MySubscribers)
}
if s.OnBehalfOfContentOwner != "" {
call = call.OnBehalfOfContentOwner(s.OnBehalfOfContentOwner)
}
if s.OnBehalfOfContentOwnerChannel != "" {
call = call.OnBehalfOfContentOwnerChannel(s.OnBehalfOfContentOwnerChannel)
}
if s.Order != "" {
call = call.Order(s.Order)
}
var items []*youtube.Subscription
pageToken := ""
for s.MaxResults > 0 {
call = call.MaxResults(min(s.MaxResults, pkg.PerPage))
s.MaxResults -= pkg.PerPage
if pageToken != "" {
call = call.PageToken(pageToken)
}
res, err := call.Do()
if err != nil {
return items, errors.Join(errGetSubscription, err)
}
items = append(items, res.Items...)
pageToken = res.NextPageToken
if pageToken == "" || len(res.Items) == 0 {
break
}
}
return items, nil
}
func (s *Subscription) List(writer io.Writer) error {
subscriptions, err := s.Get()
if err != nil && subscriptions == nil {
return err
}
switch s.Output {
case "json":
utils.PrintJSON(subscriptions, s.Jsonpath, writer)
case "yaml":
utils.PrintYAML(subscriptions, s.Jsonpath, writer)
case "table":
tb := table.NewWriter()
defer tb.Render()
tb.SetOutputMirror(writer)
tb.SetStyle(pkg.TableStyle)
tb.AppendHeader(table.Row{"ID", "Kind", "Resource ID", "Channel Title"})
for _, sub := range subscriptions {
var resourceId string
switch sub.Snippet.ResourceId.Kind {
case "youtube#video":
resourceId = sub.Snippet.ResourceId.VideoId
case "youtube#channel":
resourceId = sub.Snippet.ResourceId.ChannelId
case "youtube#playlist":
resourceId = sub.Snippet.ResourceId.PlaylistId
}
tb.AppendRow(
table.Row{
sub.Id, sub.Snippet.ResourceId.Kind, resourceId, sub.Snippet.Title,
},
)
}
}
return err
}
func (s *Subscription) Insert(writer io.Writer) error {
s.EnsureService()
subscription := &youtube.Subscription{
Snippet: &youtube.SubscriptionSnippet{
ChannelId: s.SubscriberChannelId,
Description: s.Description,
ResourceId: &youtube.ResourceId{
ChannelId: s.ChannelId,
},
Title: s.Title,
},
}
call := s.Service.Subscriptions.Insert([]string{"snippet"}, subscription)
res, err := call.Do()
if err != nil {
return errors.Join(errInsertSubscription, err)
}
switch s.Output {
case "json":
utils.PrintJSON(res, s.Jsonpath, writer)
case "yaml":
utils.PrintYAML(res, s.Jsonpath, writer)
default:
_, _ = fmt.Fprintf(writer, "Subscription inserted: %s\n", res.Id)
}
return nil
}
func (s *Subscription) Delete(writer io.Writer) error {
s.EnsureService()
for _, id := range s.Ids {
call := s.Service.Subscriptions.Delete(id)
err := call.Do()
if err != nil {
return errors.Join(errDeleteSubscription, err)
}
_, _ = fmt.Fprintf(writer, "Subscription %s deleted", id)
}
return nil
}
func WithIds(ids []string) Option {
return func(s *Subscription) {
s.Ids = ids
}
}
func WithSubscriberChannelId(id string) Option {
return func(s *Subscription) {
s.SubscriberChannelId = id
}
}
func WithDescription(description string) Option {
return func(s *Subscription) {
s.Description = description
}
}
func WithChannelId(channelId string) Option {
return func(s *Subscription) {
s.ChannelId = channelId
}
}
func WithForChannelId(forChannelId string) Option {
return func(s *Subscription) {
s.ForChannelId = forChannelId
}
}
func WithMaxResults(maxResults int64) Option {
return func(s *Subscription) {
if maxResults < 0 {
maxResults = 1
} else if maxResults == 0 {
maxResults = math.MaxInt64
}
s.MaxResults = maxResults
}
}
func WithMine(mine *bool) Option {
return func(s *Subscription) {
if mine != nil {
s.Mine = mine
}
}
}
func WithMyRecentSubscribers(myRecentSubscribers *bool) Option {
return func(s *Subscription) {
if myRecentSubscribers != nil {
s.MyRecentSubscribers = myRecentSubscribers
}
}
}
func WithMySubscribers(mySubscribers *bool) Option {
return func(s *Subscription) {
if mySubscribers != nil {
s.MySubscribers = mySubscribers
}
}
}
func WithOnBehalfOfContentOwner(onBehalfOfContentOwner string) Option {
return func(s *Subscription) {
s.OnBehalfOfContentOwner = onBehalfOfContentOwner
}
}
func WithOnBehalfOfContentOwnerChannel(onBehalfOfContentOwnerChannel string) Option {
return func(s *Subscription) {
s.OnBehalfOfContentOwnerChannel = onBehalfOfContentOwnerChannel
}
}
func WithOrder(order string) Option {
return func(s *Subscription) {
s.Order = order
}
}
func WithTitle(title string) Option {
return func(s *Subscription) {
s.Title = title
}
}
var (
WithParts = common.WithParts[*Subscription]
WithOutput = common.WithOutput[*Subscription]
WithJsonpath = common.WithJsonpath[*Subscription]
WithService = common.WithService[*Subscription]
)