add lastfm command

This commit is contained in:
lyx0 2023-05-06 23:51:50 +02:00
parent ab83e11624
commit 1d23f3945d
4 changed files with 51 additions and 0 deletions

View file

@ -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": case "phonetic":
msg := evt.Content.AsMessage().Body[9:len(evt.Content.AsMessage().Body)] msg := evt.Content.AsMessage().Body[9:len(evt.Content.AsMessage().Body)]
if resp, err := commands.Phonetic(msg); err != nil { if resp, err := commands.Phonetic(msg); err != nil {

1
go.mod
View file

@ -14,6 +14,7 @@ require (
github.com/briandowns/openweathermap v0.19.0 // indirect github.com/briandowns/openweathermap v0.19.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.18 // 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/gjson v1.14.4 // indirect
github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect github.com/tidwall/pretty v1.2.1 // indirect

2
go.sum
View file

@ -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/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 h1:cO+d60CHkknCbvzEWxP0S9K6KqyTjrCNUy1LdQLCGPc=
github.com/rs/zerolog v1.29.1/go.mod h1:Le6ESbR7hc+DP6Lt1THiV8CQSdkkNrd3R0XbEgp3ZBU= 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/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.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM= github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM=

View file

@ -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
}