mirror of
https://github.com/lyx0/nourybot.git
synced 2024-11-13 19:49:55 +01:00
add command looking up a users recently played last.fm track
This commit is contained in:
parent
b8625e1901
commit
d507bfafa0
|
@ -153,6 +153,20 @@ func (app *Application) handleCommand(message twitch.PrivateMessage) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case "lastfm":
|
||||||
|
if msgLen == 1 {
|
||||||
|
common.Send(target, "Usage: ()firstline <channel> <user>", app.TwitchClient)
|
||||||
|
return
|
||||||
|
} else if cmdParams[1] == "artist" && cmdParams[2] == "top" {
|
||||||
|
commands.LastFmArtistTop(target, message, app.TwitchClient)
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
// Default to first argument supplied being the name
|
||||||
|
// of the user to look up recently played.
|
||||||
|
commands.LastFmUserRecent(target, cmdParams[1], app.TwitchClient)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
case "help":
|
case "help":
|
||||||
if msgLen == 1 {
|
if msgLen == 1 {
|
||||||
common.Send(target, "Provides information for a given command. Usage: ()help <commandname>", app.TwitchClient)
|
common.Send(target, "Provides information for a given command. Usage: ()help <commandname>", app.TwitchClient)
|
||||||
|
|
76
internal/commands/lastfm.go
Normal file
76
internal/commands/lastfm.go
Normal file
|
@ -0,0 +1,76 @@
|
||||||
|
package commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/gempir/go-twitch-irc/v3"
|
||||||
|
"github.com/joho/godotenv"
|
||||||
|
"github.com/lyx0/nourybot/internal/common"
|
||||||
|
"github.com/shkh/lastfm-go/lastfm"
|
||||||
|
"go.uber.org/zap"
|
||||||
|
)
|
||||||
|
|
||||||
|
func LastFmArtistTop(target string, message twitch.PrivateMessage, tc *twitch.Client) {
|
||||||
|
sugar := zap.NewExample().Sugar()
|
||||||
|
defer sugar.Sync()
|
||||||
|
// snipLength is the length we need to "snip" off of the start
|
||||||
|
// of `message` to only have the artists name left.
|
||||||
|
// `()lastfm artist top` = +20
|
||||||
|
// trailing space = +1
|
||||||
|
// zero-based = +1
|
||||||
|
// = 22
|
||||||
|
snipLength := 20
|
||||||
|
|
||||||
|
artist := message.Message[snipLength:len(message.Message)]
|
||||||
|
|
||||||
|
err := godotenv.Load()
|
||||||
|
if err != nil {
|
||||||
|
sugar.Error("Error loading OpenWeatherMap API key from .env file")
|
||||||
|
}
|
||||||
|
apiKey := os.Getenv("LAST_FM_API_KEY")
|
||||||
|
apiSecret := os.Getenv("LAST_FM_SECRET")
|
||||||
|
|
||||||
|
api := lastfm.New(apiKey, apiSecret)
|
||||||
|
result, _ := api.Artist.GetTopTracks(lastfm.P{"artist": artist}) //discarding error
|
||||||
|
for _, track := range result.Tracks {
|
||||||
|
sugar.Infow("Top tracks: ",
|
||||||
|
"artist:", artist,
|
||||||
|
"track", track.Name,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func LastFmUserRecent(target, user string, tc *twitch.Client) {
|
||||||
|
sugar := zap.NewExample().Sugar()
|
||||||
|
defer sugar.Sync()
|
||||||
|
|
||||||
|
err := godotenv.Load()
|
||||||
|
if err != nil {
|
||||||
|
sugar.Error("Error loading LASTFM API keys from .env file")
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
sugar.Infow("Most recent: ",
|
||||||
|
"user:", user,
|
||||||
|
"track", track.Name,
|
||||||
|
"artist", track.Artist.Name,
|
||||||
|
)
|
||||||
|
|
||||||
|
reply = fmt.Sprintf("Most recently played track for user %v: %v - %v", user, track.Artist.Name, track.Name)
|
||||||
|
common.Send(target, reply, tc)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue