From 1d23f3945dad1332e3876bf74d65ac869d08d59b Mon Sep 17 00:00:00 2001 From: lyx0 <66651385+lyx0@users.noreply.github.com> Date: Sat, 6 May 2023 23:51:50 +0200 Subject: [PATCH] add lastfm command --- cmd/nourybot/commands.go | 15 +++++++++++++++ go.mod | 1 + go.sum | 2 ++ internal/commands/lastfm.go | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 51 insertions(+) create mode 100644 internal/commands/lastfm.go diff --git a/cmd/nourybot/commands.go b/cmd/nourybot/commands.go index 0f1bba8..61e1e6d 100644 --- a/cmd/nourybot/commands.go +++ b/cmd/nourybot/commands.go @@ -46,6 +46,21 @@ func (app *Application) ParseCommand(evt *event.Event) { } } + case "lastfm": + if msgLen == 1 { + app.SendText(evt, "Not enough arguments provided") + return + } else { + if resp, err := commands.LastFmUserRecent(cmdParams[1]); err != nil { + app.Log.Error().Err(err).Msg("failed to handle lastfm command") + app.SendText(evt, "Something went wrong.") + return + } else { + app.SendText(evt, resp) + return + } + } + case "phonetic": msg := evt.Content.AsMessage().Body[9:len(evt.Content.AsMessage().Body)] if resp, err := commands.Phonetic(msg); err != nil { diff --git a/go.mod b/go.mod index bbe0942..03e90b9 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,7 @@ require ( github.com/briandowns/openweathermap v0.19.0 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.18 // indirect + github.com/shkh/lastfm-go v0.0.0-20191215035245-89a801c244e0 // indirect github.com/tidwall/gjson v1.14.4 // indirect github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/pretty v1.2.1 // indirect diff --git a/go.sum b/go.sum index a47274b..44803bf 100644 --- a/go.sum +++ b/go.sum @@ -26,6 +26,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= github.com/rs/zerolog v1.29.1 h1:cO+d60CHkknCbvzEWxP0S9K6KqyTjrCNUy1LdQLCGPc= github.com/rs/zerolog v1.29.1/go.mod h1:Le6ESbR7hc+DP6Lt1THiV8CQSdkkNrd3R0XbEgp3ZBU= +github.com/shkh/lastfm-go v0.0.0-20191215035245-89a801c244e0 h1:cgqwZtnR+IQfUYDLJ3Kiy4aE+O/wExTzEIg8xwC4Qfs= +github.com/shkh/lastfm-go v0.0.0-20191215035245-89a801c244e0/go.mod h1:n3nudMl178cEvD44PaopxH9jhJaQzthSxUzLO5iKMy4= github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM= diff --git a/internal/commands/lastfm.go b/internal/commands/lastfm.go new file mode 100644 index 0000000..d1e5e22 --- /dev/null +++ b/internal/commands/lastfm.go @@ -0,0 +1,33 @@ +package commands + +import ( + "fmt" + "os" + + "github.com/joho/godotenv" + "github.com/shkh/lastfm-go/lastfm" +) + +func LastFmUserRecent(user string) (string, error) { + err := godotenv.Load() + if err != nil { + return "", ErrInternalServerError + } + + apiKey := os.Getenv("LAST_FM_API_KEY") + apiSecret := os.Getenv("LAST_FM_SECRET") + + api := lastfm.New(apiKey, apiSecret) + result, _ := api.User.GetRecentTracks(lastfm.P{"user": user}) //discarding error + + var reply string + for i, track := range result.Tracks { + // The 0th result is the most recent one since it goes from most recent + // to least recent. + if i == 0 { + reply = fmt.Sprintf("Most recently played track for user %v: %v - %v", user, track.Artist.Name, track.Name) + return reply, nil + } + } + return "", ErrInternalServerError +}