Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
yanosea committed Feb 27, 2025
1 parent 90b716e commit 7168f5c
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 12 deletions.
46 changes: 46 additions & 0 deletions app/application/spotlike/check_like_usecase.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package spotlike

import (
"context"

"github.com/zmb3/spotify/v2"
)

// checkLikeUseCase is a struct that contains the use case of checking for an artist.
type CheckLikeUseCase struct {
Client *spotify.Client
}

// NewCheckLikeUseCase returns a new instance of the checkLikeUseCase struct.
func NewCheckLikeUseCase(client *spotify.Client) *CheckLikeUseCase {
return &CheckLikeUseCase{
Client: client,
}
}

// Run returns the check result of the artist.
func (uc *CheckLikeUseCase) Run(id string, contentType SpotifyContentType) (bool, error) {
var alreadyLiked bool
switch contentType {
case Artist:
result, err := uc.Client.CurrentUserFollows(context.Background(), "artist", spotify.ID(id))
if err != nil {
return false, err
}
alreadyLiked = result[0]
case Album:
result, err := uc.Client.UserHasAlbums(context.Background(), spotify.ID(id))
if err != nil {
return false, err
}
alreadyLiked = result[0]
case Track:
result, err := uc.Client.UserHasTracks(context.Background(), spotify.ID(id))
if err != nil {
return false, err
}
alreadyLiked = result[0]
}

return alreadyLiked, nil
}
24 changes: 24 additions & 0 deletions app/application/spotlike/like_artist_usecase.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package spotlike

import (
"context"

"github.com/zmb3/spotify/v2"
)

// likeArtistUseCase is a struct that contains the use case of likeing for an artist.
type likeArtistUseCase struct {
Client *spotify.Client
}

// NewLikeArtistUseCase returns a new instance of the likeArtistUseCase struct.
func NewLikeArtistUseCase(client *spotify.Client) *likeArtistUseCase {
return &likeArtistUseCase{
Client: client,
}
}

// Run returns the like result of the artist.
func (uc *likeArtistUseCase) Run(id string) error {
return uc.Client.FollowArtist(context.Background(), spotify.ID(id))
}
1 change: 1 addition & 0 deletions app/presentation/cli/spotlike/command/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func NewRootCommand(
cmd.SetUse("spotlike")
cmd.SetUsageTemplate(rootUsageTemplate)
cmd.SetHelpTemplate(rootHelpTemplate)
cmd.SetArgs(cobra.ExactArgs(0))
cmd.SetSilenceErrors(true)
cmd.PersistentFlags().BoolVarP(
&rootOps.Version,
Expand Down
93 changes: 83 additions & 10 deletions app/presentation/cli/spotlike/command/spotlike/like/artist.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,56 @@
package like

import (
"os"

c "github.com/spf13/cobra"

spotlikeApp "github.com/yanosea/spotlike/app/application/spotlike"
"github.com/yanosea/spotlike/app/presentation/cli/spotlike/command/spotlike"
"github.com/yanosea/spotlike/app/presentation/cli/spotlike/config"
"github.com/yanosea/spotlike/app/presentation/cli/spotlike/formatter"
"github.com/yanosea/spotlike/app/presentation/cli/spotlike/presenter"

"github.com/yanosea/spotlike/pkg/proxy"
)

type LikeArtistOptions struct {
NoConfirm bool
Format string
}

var (
LikeArtistOps = LikeArtistOptions{
NoConfirm: false,
Format: "table",
}
)

func NewArtistCommand(
cobra proxy.Cobra,
conf *config.SpotlikeCliConfig,
output *string,
) proxy.Command {
cmd := cobra.NewCommand()
cmd.SetUse("artist")
cmd.SetAliases([]string{"ar", "a"})
cmd.SetUsageTemplate(artistUsageTemplate)
cmd.SetHelpTemplate(artistHelpTemplate)
cmd.SetAliases([]string{"ar", "A"})
cmd.SetUsageTemplate(likeArtistUsageTemplate)
cmd.SetHelpTemplate(likeArtistHelpTemplate)
cmd.SetSilenceErrors(true)
cmd.Flags().BoolVarP(
&LikeArtistOps.NoConfirm,
"no-confirm",
"",
false,
"🚫 do not confirm before liking the artist",
)
cmd.Flags().StringVarP(
&LikeArtistOps.Format,
"format",
"f",
"table",
"πŸ“ format of the output (default \"table\", e.g: \"plain\")",
)

cmd.SetRunE(
func(_ *c.Command, args []string) error {
Expand All @@ -44,7 +73,7 @@ func runArtist(output *string, args []string, conf *config.SpotlikeCliConfig) er
return err
}

var gctucDtoDtos []*spotlikeApp.GetContentTypeUseCaseOutputDto
var gctucDtos []*spotlikeApp.GetContentTypeUseCaseOutputDto
for _, id := range args {
gctuc := spotlikeApp.NewgetContentTypeUseCase(client)
gctucDto, err := gctuc.Run(id)
Expand All @@ -53,30 +82,74 @@ func runArtist(output *string, args []string, conf *config.SpotlikeCliConfig) er
}

if gctucDto.Type != spotlikeApp.Artist {
o := formatter.Yellow("⚑ The id %s is not an artist...", id)
o := formatter.Yellow("⚑ The id " + gctucDto.Name + " is not an artist...")
*output = o
return nil
}

gctucDtoDtos = append(gctucDtoDtos, gctucDto)
gctucDtos = append(gctucDtos, gctucDto)
}

for _, gctucDto := range gctucDtoDtos {
for _, gctucDto := range gctucDtos {
cfuc := spotlikeApp.NewCheckLikeUseCase(client)
alreadyLiked, err := cfuc.Run(gctucDto.ID, spotlikeApp.Artist)
if err != nil {
return err
}
if alreadyLiked {
presenter.Print(os.Stdout, formatter.Yellow("⚑ ["+gctucDto.Name+"] is already liked..."))
continue
}

if !LikeArtistOps.NoConfirm {
if answer, err := presenter.RunPrompt(
"Proceed with liking " + gctucDto.Name + "? [y/N]",
); err != nil {
return err
} else if answer != "y" && answer != "Y" {
o := formatter.Yellow("🚫 Cancelled liking [" + gctucDto.Name + "] ...")
*output = o
continue
}
}

lauc := spotlikeApp.NewLikeArtistUseCase(client)
if err := lauc.Run(gctucDto.ID); err != nil {
return err
}

}

f, err := formatter.NewFormatter(LikeOps.Format)
f, err := formatter.NewFormatter(LikeArtistOps.Format)
if err != nil {
o := formatter.Red("❌ Failed to create a formatter...")
*output = o
return err
}
o := "\n" + f.Format(gctucDtoDtos)
o := "\n" + f.Format(gctucDtos)
*output = o

return nil
}

const (
likeArtistHelpTemplate = `🀍🎀 Like artists on Spotify by ID.
You can like artists on Spotify by ID.
Before using this command,
you need to get the ID of the artist you want to like by using the search command.
` + likeArtistUsageTemplate
likeArtistUsageTemplate = `Usage:
spotlike like artist [flags] [arguments]
spotlike lk artist [flags] [arguments]
spotlike l artist [flags] [arguments]
Flags:
--no-confirm 🚫 do not confirm before liking the artist
-f, --format πŸ“ format of the output (default "table", e.g: "plain")
Arguments:
ID πŸ†” like with the ID of the artist (e.g. : 00DuPiLri3mNomvvM3nZvU 3B9O5mYYw89fFXkwKh7jCS)
`
)
15 changes: 13 additions & 2 deletions app/presentation/cli/spotlike/command/spotlike/like/like.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,15 @@ func NewLikeCommand(
cmd.SetAliases([]string{"lk", "l"})
cmd.SetUsageTemplate(likeUsageTemplate)
cmd.SetHelpTemplate(likeHelpTemplate)
cmd.SetArgs(cobra.ExactArgs(0))
cmd.SetSilenceErrors(true)
cmd.AddCommand()
cmd.AddCommand(
NewArtistCommand(
cobra,
conf,
output,
),
)

cmd.SetRunE(
func(cmd *c.Command, args []string) error {
Expand Down Expand Up @@ -57,10 +64,14 @@ you need to get the ID of the content you want to like by using the search comma
` + likeUsageTemplate
likeUsageTemplate = `Usage:
spotlike like [flags]
spotlike lk [flags]
spotlike l [flags]
spotlike like [command]
spotlike lk [command]
spotlike l [command]
Available Commands:
artist, ar, a 🎀 Like an artist on Spotify by ID.
artist, ar, A 🎀 Like an artist on Spotify by ID.
album, al, a πŸ’Ώ Like albums on Spotify by ID.
track, tr, t 🎡 Like tracks on Spotify by ID.
Expand Down

0 comments on commit 7168f5c

Please sign in to comment.