From 4eb34bb36dc8ded5f9792c84169cd05774f2dbf1 Mon Sep 17 00:00:00 2001 From: chaoqun <27287694+OpenWaygate@users.noreply.github.com> Date: Sat, 20 Apr 2024 23:57:17 +0800 Subject: [PATCH] :bug: Deconfuse struct and interface --- pkg/yutuber/activity.go | 22 ++++---- pkg/yutuber/channel.go | 98 ++++++++++++++++----------------- pkg/yutuber/member.go | 16 +++--- pkg/yutuber/membershipsLevel.go | 14 ++--- pkg/yutuber/playlist.go | 32 +++++------ pkg/yutuber/playlistItem.go | 32 +++++------ pkg/yutuber/video.go | 61 ++++++++++---------- pkg/yutuber/videoCategory.go | 22 ++++---- 8 files changed, 148 insertions(+), 149 deletions(-) diff --git a/pkg/yutuber/activity.go b/pkg/yutuber/activity.go index 2d0e7c7..981f61c 100644 --- a/pkg/yutuber/activity.go +++ b/pkg/yutuber/activity.go @@ -14,19 +14,19 @@ var ( errGetActivity error = errors.New("failed to get activity") ) -type Activity struct { +type activity struct { channelId string } -type ActivityService interface { +type Activity interface { List([]string, string) - get() *youtube.Activity + get([]string) []*youtube.Activity } -type ActivityOption func(*Activity) +type ActivityOption func(*activity) -func NewActivity(opts ...ActivityOption) *Activity { - a := &Activity{} +func NewActivity(opts ...ActivityOption) Activity { + a := &activity{} for _, opt := range opts { opt(a) @@ -35,9 +35,9 @@ func NewActivity(opts ...ActivityOption) *Activity { return a } -func (a *Activity) get() []*youtube.Activity { +func (a *activity) get(parts []string) []*youtube.Activity { service := auth.NewY2BService() - call := service.Activities.List(part) + call := service.Activities.List(parts) if a.channelId != "" { call = call.ChannelId(a.channelId) } else { @@ -52,8 +52,8 @@ func (a *Activity) get() []*youtube.Activity { return response.Items } -func (a *Activity) List(parts []string, output string) { - activities := a.get() +func (a *activity) List(parts []string, output string) { + activities := a.get(parts) switch output { case "json": utils.PrintJSON(activities) @@ -68,7 +68,7 @@ func (a *Activity) List(parts []string, output string) { } func WithActivityChannelId(channelId string) ActivityOption { - return func(a *Activity) { + return func(a *activity) { a.channelId = channelId } } diff --git a/pkg/yutuber/channel.go b/pkg/yutuber/channel.go index 93ab518..b387ff9 100644 --- a/pkg/yutuber/channel.go +++ b/pkg/yutuber/channel.go @@ -13,28 +13,27 @@ import ( var ( service *youtube.Service - part []string = []string{"snippet"} - errGetChannel error = errors.New("failed to get channel") - errUpdateChannel error = errors.New("failed to update channel") + errGetChannel error = errors.New("failed to get channel") + errUpdateChannel error = errors.New("failed to update channel") ) -type Channel struct { +type channel struct { id string title string desc string user string } -type ChannelService interface { +type Channel interface { List([]string, string) Update() - get() *youtube.Channel + get([]string) []*youtube.Channel } -type ChannelOption func(*Channel) +type ChannelOption func(*channel) -func NewChannel(opts ...ChannelOption) *Channel { - c := &Channel{} +func NewChannel(opts ...ChannelOption) Channel { + c := &channel{} service = auth.NewY2BService() for _, opt := range opts { @@ -44,42 +43,8 @@ func NewChannel(opts ...ChannelOption) *Channel { return c } -func (c *Channel) List(parts []string, output string) { - channels := c.get() - switch output { - case "json": - utils.PrintJSON(channels) - case "yaml": - utils.PrintYAML(channels) - default: - fmt.Println("ID\tTitle") - for _, channel := range channels { - fmt.Printf("%s\t%s\n", channel.Id, channel.Snippet.Title) - } - } -} - -func (c *Channel) Update() { - channel := c.get()[0] - // TODO: is there a better way to check and update? - if c.title != "" { - channel.Snippet.Title = c.title - } - if c.desc != "" { - channel.Snippet.Description = c.desc - } - - call := service.Channels.Update(part, channel) - res, err := call.Do() - if err != nil { - log.Fatalln(errors.Join(errUpdateChannel, err), c.id) - } - fmt.Println("Channel updated:") - utils.PrintJSON(res) -} - -func (c *Channel) get() []*youtube.Channel { - call := service.Channels.List(part) +func (c *channel) get(parts []string) []*youtube.Channel { + call := service.Channels.List(parts) switch { case c.id != "": call = call.Id(c.id) @@ -96,26 +61,61 @@ func (c *Channel) get() []*youtube.Channel { return resp.Items } +func (c *channel) List(parts []string, output string) { + channels := c.get(parts) + switch output { + case "json": + utils.PrintJSON(channels) + case "yaml": + utils.PrintYAML(channels) + default: + fmt.Println("ID\tTitle") + for _, channel := range channels { + fmt.Printf("%s\t%s\n", channel.Id, channel.Snippet.Title) + } + } +} + +func (c *channel) Update() { + parts := []string{"snippet"} + channel := c.get(parts)[0] + // TODO: is there a better way to check and update? + if c.title != "" { + channel.Snippet.Title = c.title + } + if c.desc != "" { + channel.Snippet.Description = c.desc + } + + call := service.Channels.Update(parts, channel) + res, err := call.Do() + if err != nil { + log.Fatalln(errors.Join(errUpdateChannel, err), c.id) + } + fmt.Println("Channel updated:") + utils.PrintJSON(res) +} + func WithChannelID(id string) ChannelOption { - return func(c *Channel) { + return func(c *channel) { c.id = id } } func WithChannelTitle(title string) ChannelOption { - return func(c *Channel) { + return func(c *channel) { c.title = title } } func WithChannelDesc(desc string) ChannelOption { - return func(c *Channel) { + return func(c *channel) { c.desc = desc } } func WithChannelUser(user string) ChannelOption { - return func(c *Channel) { + return func(c *channel) { c.user = user } } diff --git a/pkg/yutuber/member.go b/pkg/yutuber/member.go index 62cb8d2..a055f04 100644 --- a/pkg/yutuber/member.go +++ b/pkg/yutuber/member.go @@ -13,19 +13,19 @@ var ( errGetMember error = errors.New("failed to get member") ) -type Member struct { +type member struct { memberChannelId string } -type MemberService interface { +type Member interface { List([]string, string) get([]string) []*youtube.Member } -type MemberOption func(*Member) +type MemberOption func(*member) -func NewMember(opts ...MemberOption) *Member { - m := &Member{} +func NewMember(opts ...MemberOption) Member { + m := &member{} service = auth.NewY2BService() for _, opt := range opts { @@ -35,7 +35,7 @@ func NewMember(opts ...MemberOption) *Member { return m } -func (m *Member) get(parts []string) []*youtube.Member { +func (m *member) get(parts []string) []*youtube.Member { call := service.Members.List(parts) if m.memberChannelId != "" { call = call.FilterByMemberChannelId(m.memberChannelId) @@ -48,7 +48,7 @@ func (m *Member) get(parts []string) []*youtube.Member { return response.Items } -func (m *Member) List(parts []string, output string) { +func (m *member) List(parts []string, output string) { members := m.get(parts) switch output { case "json": @@ -68,7 +68,7 @@ func (m *Member) List(parts []string, output string) { } func WithMemberChannelId(channelId string) MemberOption { - return func(m *Member) { + return func(m *member) { m.memberChannelId = channelId } } diff --git a/pkg/yutuber/membershipsLevel.go b/pkg/yutuber/membershipsLevel.go index f292c30..697a96e 100644 --- a/pkg/yutuber/membershipsLevel.go +++ b/pkg/yutuber/membershipsLevel.go @@ -13,17 +13,17 @@ var ( errGetMembershipsLevel error = errors.New("failed to get memberships level") ) -type MembershipsLevel struct{} +type membershipsLevel struct{} -type MembershipsLevelService interface { +type MembershipsLevel interface { List([]string, string) get([]string) []*youtube.MembershipsLevel } -type MembershipsLevelOption func(*MembershipsLevel) +type MembershipsLevelOption func(*membershipsLevel) -func NewMembershipsLevel(opts ...MembershipsLevelOption) *MembershipsLevel { - m := &MembershipsLevel{} +func NewMembershipsLevel(opts ...MembershipsLevelOption) MembershipsLevel { + m := &membershipsLevel{} service = auth.NewY2BService() for _, opt := range opts { @@ -33,7 +33,7 @@ func NewMembershipsLevel(opts ...MembershipsLevelOption) *MembershipsLevel { return m } -func (m *MembershipsLevel) get(parts []string) []*youtube.MembershipsLevel { +func (m *membershipsLevel) get(parts []string) []*youtube.MembershipsLevel { call := service.MembershipsLevels.List(parts) response, err := call.Do() if err != nil { @@ -43,7 +43,7 @@ func (m *MembershipsLevel) get(parts []string) []*youtube.MembershipsLevel { return response.Items } -func (m *MembershipsLevel) List(parts []string, output string) { +func (m *membershipsLevel) List(parts []string, output string) { membershipsLevels := m.get(parts) switch output { case "json": diff --git a/pkg/yutuber/playlist.go b/pkg/yutuber/playlist.go index 6689ba8..a82cb99 100644 --- a/pkg/yutuber/playlist.go +++ b/pkg/yutuber/playlist.go @@ -17,7 +17,7 @@ var ( errUpdatePlaylist error = errors.New("failed to update playlist") ) -type Playlist struct { +type playlist struct { id string title string desc string @@ -27,17 +27,17 @@ type Playlist struct { privacy string } -type PlaylistService interface { +type Playlist interface { List([]string, string) Insert() Update() get([]string) []*youtube.Playlist } -type PlaylistOption func(*Playlist) +type PlaylistOption func(*playlist) -func NewPlaylist(opts ...PlaylistOption) *Playlist { - p := &Playlist{} +func NewPlaylist(opts ...PlaylistOption) Playlist { + p := &playlist{} service = auth.NewY2BService() for _, opt := range opts { @@ -47,7 +47,7 @@ func NewPlaylist(opts ...PlaylistOption) *Playlist { return p } -func (p *Playlist) List(parts []string, output string) { +func (p *playlist) List(parts []string, output string) { playlists := p.get(parts) switch output { case "json": @@ -62,7 +62,7 @@ func (p *Playlist) List(parts []string, output string) { } } -func (p *Playlist) Insert() { +func (p *playlist) Insert() { upload := &youtube.Playlist{ Snippet: &youtube.PlaylistSnippet{ Title: p.title, @@ -85,7 +85,7 @@ func (p *Playlist) Insert() { fmt.Printf("Playlist %s inserted\n", playlist.Id) } -func (p *Playlist) Update() { +func (p *playlist) Update() { playlist := p.get([]string{"id", "snippet", "status"})[0] if p.title != "" { playlist.Snippet.Title = p.title @@ -114,7 +114,7 @@ func (p *Playlist) Update() { utils.PrintJSON(data) } -func (p *Playlist) get(parts []string) []*youtube.Playlist { +func (p *playlist) get(parts []string) []*youtube.Playlist { call := service.Playlists.List(parts) switch { case p.id != "": @@ -134,43 +134,43 @@ func (p *Playlist) get(parts []string) []*youtube.Playlist { } func WithPlaylistId(id string) PlaylistOption { - return func(p *Playlist) { + return func(p *playlist) { p.id = id } } func WithPlaylistTitle(title string) PlaylistOption { - return func(p *Playlist) { + return func(p *playlist) { p.title = title } } func WithPlaylistDesc(desc string) PlaylistOption { - return func(p *Playlist) { + return func(p *playlist) { p.desc = desc } } func WithPlaylistTags(tags []string) PlaylistOption { - return func(p *Playlist) { + return func(p *playlist) { p.tags = tags } } func WithPlaylistLanguage(language string) PlaylistOption { - return func(p *Playlist) { + return func(p *playlist) { p.language = language } } func WithPlaylistChannelId(channelId string) PlaylistOption { - return func(p *Playlist) { + return func(p *playlist) { p.channelId = channelId } } func WithPlaylistPrivacy(privacy string) PlaylistOption { - return func(p *Playlist) { + return func(p *playlist) { p.privacy = privacy } } diff --git a/pkg/yutuber/playlistItem.go b/pkg/yutuber/playlistItem.go index 172c87d..ad114a2 100644 --- a/pkg/yutuber/playlistItem.go +++ b/pkg/yutuber/playlistItem.go @@ -16,7 +16,7 @@ var ( errInsertPlaylistItem error = fmt.Errorf("failed to insert playlist item") ) -type PlaylistItem struct { +type playlistItem struct { id string title string desc string @@ -26,17 +26,17 @@ type PlaylistItem struct { privacy string } -type PlaylistItemService interface { +type PlaylistItem interface { List([]string, string) Insert() Update() get([]string) []*youtube.PlaylistItem } -type PlaylistItemOption func(*PlaylistItem) +type PlaylistItemOption func(*playlistItem) -func NewPlaylistItem(opts ...PlaylistItemOption) *PlaylistItem { - p := &PlaylistItem{} +func NewPlaylistItem(opts ...PlaylistItemOption) PlaylistItem { + p := &playlistItem{} service = auth.NewY2BService() for _, opt := range opts { @@ -46,7 +46,7 @@ func NewPlaylistItem(opts ...PlaylistItemOption) *PlaylistItem { return p } -func (pi *PlaylistItem) List(parts []string, output string) { +func (pi *playlistItem) List(parts []string, output string) { playlistItems := pi.get(parts) switch output { case "json": @@ -61,7 +61,7 @@ func (pi *PlaylistItem) List(parts []string, output string) { } } -func (pi *PlaylistItem) Insert() { +func (pi *playlistItem) Insert() { playlistItem := &youtube.PlaylistItem{ Snippet: &youtube.PlaylistItemSnippet{ Title: pi.title, @@ -87,7 +87,7 @@ func (pi *PlaylistItem) Insert() { utils.PrintJSON(res) } -func (pi *PlaylistItem) Update() { +func (pi *playlistItem) Update() { playlistItem := pi.get([]string{"id", "snippet", "status"})[0] if pi.title != "" { playlistItem.Snippet.Title = pi.title @@ -108,7 +108,7 @@ func (pi *PlaylistItem) Update() { utils.PrintJSON(res) } -func (pi *PlaylistItem) get(parts []string) []*youtube.PlaylistItem { +func (pi *playlistItem) get(parts []string) []*youtube.PlaylistItem { call := service.PlaylistItems.List(parts) if pi.id != "" { call = call.Id(pi.id) @@ -124,43 +124,43 @@ func (pi *PlaylistItem) get(parts []string) []*youtube.PlaylistItem { } func WithPlaylistItemId(id string) PlaylistItemOption { - return func(p *PlaylistItem) { + return func(p *playlistItem) { p.id = id } } func WithPlaylistItemTitle(title string) PlaylistItemOption { - return func(p *PlaylistItem) { + return func(p *playlistItem) { p.title = title } } func WithPlaylistItemDesc(desc string) PlaylistItemOption { - return func(p *PlaylistItem) { + return func(p *playlistItem) { p.desc = desc } } func WithPlaylistItemVideoId(videoId string) PlaylistItemOption { - return func(p *PlaylistItem) { + return func(p *playlistItem) { p.videoId = videoId } } func WithPlaylistItemPlaylistId(playlistId string) PlaylistItemOption { - return func(p *PlaylistItem) { + return func(p *playlistItem) { p.playlistId = playlistId } } func WithPlaylistItemChannelId(channelId string) PlaylistItemOption { - return func(p *PlaylistItem) { + return func(p *playlistItem) { p.channelId = channelId } } func WithPlaylistItemPrivacy(privacy string) PlaylistItemOption { - return func(p *PlaylistItem) { + return func(p *playlistItem) { p.privacy = privacy } } diff --git a/pkg/yutuber/video.go b/pkg/yutuber/video.go index 2af7f16..c0f4834 100644 --- a/pkg/yutuber/video.go +++ b/pkg/yutuber/video.go @@ -21,7 +21,7 @@ var ( errGetRating error = errors.New("failed to get rating") ) -type Video struct { +type video struct { id string file string title string @@ -39,21 +39,20 @@ type Video struct { embeddable bool } -type VideoService interface { +type Video interface { List([]string, string) Insert() Update() Rate() GetRating() - get([]string) *youtube.Video - setThumbnail() - validate() + get([]string) []*youtube.Video + setThumbnail(string, *youtube.Service) } -type VideoOption func(*Video) +type VideoOption func(*video) -func NewVideo(opts ...VideoOption) *Video { - v := &Video{} +func NewVideo(opts ...VideoOption) Video { + v := &video{} service = auth.NewY2BService() for _, opt := range opts { @@ -63,7 +62,7 @@ func NewVideo(opts ...VideoOption) *Video { return v } -func (v *Video) get(parts []string) []*youtube.Video { +func (v *video) get(parts []string) []*youtube.Video { call := service.Videos.List(parts) if v.id != "" { call = call.Id(v.id) @@ -78,8 +77,8 @@ func (v *Video) get(parts []string) []*youtube.Video { return response.Items } -func (v *Video) List(parts []string, output string) { - videos := v.get([]string{"id", "snippet", "status", "statistics"}) +func (v *video) List(parts []string, output string) { + videos := v.get(parts) switch output { case "json": utils.PrintJSON(videos) @@ -93,7 +92,7 @@ func (v *Video) List(parts []string, output string) { } } -func (v *Video) Insert() { +func (v *video) Insert() { file, err := os.Open(v.file) if err != nil { log.Fatalln(errors.Join(errOpenFile, err), v.file) @@ -144,7 +143,7 @@ func (v *Video) Insert() { utils.PrintJSON(res) } -func (v *Video) Update() { +func (v *video) Update() { video := v.get([]string{"id", "snippet", "status"})[0] if v.title != "" { video.Snippet.Title = v.title @@ -193,7 +192,7 @@ func (v *Video) Update() { utils.PrintJSON(res) } -func (v *Video) Rate() { +func (v *video) Rate() { call := service.Videos.Rate(v.id, v.rating) err := call.Do() if err != nil { @@ -202,7 +201,7 @@ func (v *Video) Rate() { fmt.Printf("Video %s rated %s\n", v.id, v.rating) } -func (v *Video) GetRating() { +func (v *video) GetRating() { call := service.Videos.GetRating([]string{v.id}) res, err := call.Do() if err != nil { @@ -212,7 +211,7 @@ func (v *Video) GetRating() { utils.PrintJSON(res) } -func (v *Video) setThumbnail(thumbnail string, service *youtube.Service) { +func (v *video) setThumbnail(thumbnail string, service *youtube.Service) { file, err := os.Open(thumbnail) if err != nil { log.Fatalln(errors.Join(errOpenFile, err), thumbnail) @@ -225,91 +224,91 @@ func (v *Video) setThumbnail(thumbnail string, service *youtube.Service) { } func WithVideoId(id string) VideoOption { - return func(v *Video) { + return func(v *video) { v.id = id } } func WithVideoFile(file string) VideoOption { - return func(v *Video) { + return func(v *video) { v.file = file } } func WithVideoTitle(title string) VideoOption { - return func(v *Video) { + return func(v *video) { v.title = title } } func WithVideoDesc(desc string) VideoOption { - return func(v *Video) { + return func(v *video) { v.desc = desc } } func WithVideoTags(tags []string) VideoOption { - return func(v *Video) { + return func(v *video) { v.tags = tags } } func WithVideoLanguage(language string) VideoOption { - return func(v *Video) { + return func(v *video) { v.language = language } } func WithVideoThumbnail(thumbnail string) VideoOption { - return func(v *Video) { + return func(v *video) { v.thumbnail = thumbnail } } func WithVideoRating(rating string) VideoOption { - return func(v *Video) { + return func(v *video) { v.rating = rating } } func WithVideoChart(chart string) VideoOption { - return func(v *Video) { + return func(v *video) { v.chart = chart } } func WithVideoForKids(forKids bool) VideoOption { - return func(v *Video) { + return func(v *video) { v.forKids = forKids } } func WithVideoEmbeddable(embeddable bool) VideoOption { - return func(v *Video) { + return func(v *video) { v.embeddable = embeddable } } func WithVideoCategory(category string) VideoOption { - return func(v *Video) { + return func(v *video) { v.category = category } } func WithVideoPrivacy(privacy string) VideoOption { - return func(v *Video) { + return func(v *video) { v.privacy = privacy } } func WithVideoChannelId(channelId string) VideoOption { - return func(v *Video) { + return func(v *video) { v.channelId = channelId } } func WithVideoPlaylistId(playlistId string) VideoOption { - return func(v *Video) { + return func(v *video) { v.playlistId = playlistId } } diff --git a/pkg/yutuber/videoCategory.go b/pkg/yutuber/videoCategory.go index 67ccfbf..73f99a3 100644 --- a/pkg/yutuber/videoCategory.go +++ b/pkg/yutuber/videoCategory.go @@ -14,28 +14,28 @@ var ( errGetVideoCategory error = errors.New("failed to get video category") ) -type VideoCategory struct { +type videoCategory struct { regionCode string } -type VideoCategoryService interface { - get([]string) *youtube.VideoCategory +type VideoCategory interface { + get([]string) []*youtube.VideoCategory List([]string, string) } -type VideoCategoryOption func(*VideoCategory) +type VideoCategoryOption func(*videoCategory) -func NewVideoCategory(opt ...VideoCategoryOption) *VideoCategory { +func NewVideoCategory(opt ...VideoCategoryOption) VideoCategory { service = auth.NewY2BService() - vc := &VideoCategory{} + vc := &videoCategory{} for _, o := range opt { o(vc) } return vc } -func (vc *VideoCategory) get(part []string) []*youtube.VideoCategory { - call := service.VideoCategories.List(part).RegionCode(vc.regionCode) +func (vc *videoCategory) get(parts []string) []*youtube.VideoCategory { + call := service.VideoCategories.List(parts).RegionCode(vc.regionCode) response, err := call.Do() if err != nil { log.Fatalln(errors.Join(errGetVideoCategory, err), vc.regionCode) @@ -44,8 +44,8 @@ func (vc *VideoCategory) get(part []string) []*youtube.VideoCategory { return response.Items } -func (vc *VideoCategory) List(part []string, output string) { - videoCategories := vc.get(part) +func (vc *videoCategory) List(parts []string, output string) { + videoCategories := vc.get(parts) switch output { case "json": utils.PrintJSON(videoCategories) @@ -63,7 +63,7 @@ func (vc *VideoCategory) List(part []string, output string) { } func WithRegionCode(regionCode string) VideoCategoryOption { - return func(vc *VideoCategory) { + return func(vc *videoCategory) { vc.regionCode = regionCode } }