Commit 50eb740f authored by Mike Jones's avatar Mike Jones 🌶

Add top commands

parent 63acf5bf
......@@ -20,11 +20,14 @@ func main() {
log.Fatalf("Could not open database: %s", err.Error())
}
// initialise commands
gauntlet.New(config, db)
out := gauntlet.Run("ne7split", `splays Begrime Exemious "Noose for a Monarch"`)
out := gauntlet.Run("ne7split", `topa`)
log.Println(out)
if out != "" {
log.Println(out)
}
/*
st, err := store.NewBoltStore(db)
......
package command
import (
"fmt"
"strings"
"github.com/n7st/lastfm-go/lastfm"
)
func (f *Fn) ArtistInfo(artist string) string {
artistInfo, err := f.getArtistInfo(artist)
if err != nil {
return fmtError(err)
}
return f.fmtArtistInfo(artistInfo)
}
// returns a comma separated list of tags, e.g.:
// death metal, old school death metal, danish, breakbeat, seen live
func (f *Fn) GetArtistTags(artist string) string {
......@@ -14,13 +25,7 @@ func (f *Fn) GetArtistTags(artist string) string {
res, err := f.getArtistInfo(artist)
if err == nil {
var t []string
for i := 0; i <= 4; i++ {
t = append(t, res.Tags[i].Name)
}
tags = strings.Join(t, ", ")
tags = fmtArtistTags(res)
}
return tags
......@@ -31,4 +36,34 @@ func (f *Fn) getArtistInfo(artist string) (lastfm.ArtistGetInfo, error) {
"artist": artist,
"autocorrect": 1,
})
}
func (f *Fn) fmtArtistInfo(artist lastfm.ArtistGetInfo) string {
return fmt.Sprintf("%s have %s plays and %s listeners. Similar artists: %s. Tags: %s",
artist.Name,
artist.Stats.Plays,
artist.Stats.Listeners,
fmtArtistSimilar(artist),
fmtArtistTags(artist),
)
}
func fmtArtistTags(artist lastfm.ArtistGetInfo) string {
var t []string
for i := 0; i <= 4; i++ {
t = append(t, artist.Tags[i].Name)
}
return strings.Join(t, ", ")
}
func fmtArtistSimilar(artist lastfm.ArtistGetInfo) string {
var s []string
for i := 0; i <= 4; i++ {
s = append(s, artist.Similars[i].Name)
}
return strings.Join(s, ", ")
}
\ No newline at end of file
......@@ -10,7 +10,10 @@ import (
"git.netsplit.uk/stella-irc/modlastfm/internal/app/modlastfm/util"
)
type FnCommand func(message string, username string) string
type (
FnCommand func(message string, username string) string
OpenFnCommand func(message string) string
)
type Fn struct {
Config *util.Config
......@@ -42,4 +45,4 @@ func fmtPlaycountPercentage(needle string, haystack string) string {
pc := (n * 100) / h
return fmt.Sprintf("%.3f%%", pc)
}
\ No newline at end of file
}
......@@ -4,11 +4,15 @@ import (
"fmt"
"regexp"
"strconv"
"strings"
"github.com/n7st/lastfm-go/lastfm"
)
const statLineF = "%s by %s has %s total plays, of which %s are %s's (%s)"
const (
statLineF = "%s by %s has %s total plays, of which %s are %s's (%s)"
noTopArtistsForPeriodF = "%s has no listening data for this period"
)
func (f *Fn) NowPlaying(message string, username string) string {
resp, err := f.LastFM.User.GetRecentTracks(lastfm.P{
......@@ -69,6 +73,39 @@ func (f *Fn) UserTrackStats(message string, username string) string {
return f.fmtUserTrackStats(username, userTrackInfo)
}
func (f *Fn) UserTopWeek(message string, username string) string {
return f.getUserTop(username, "7day")
}
func (f *Fn) UserTopMonth(message string, username string) string {
return f.getUserTop(username, "1month")
}
func (f *Fn) UserTopYear(message string, username string) string {
return f.getUserTop(username, "12month")
}
func (f *Fn) UserTopAll(message string, username string) string {
return f.getUserTop(username, "overall")
}
func (f *Fn) getUserTop(username string, period string) string {
userTop, err := f.LastFM.User.GetTopArtists(lastfm.P{
"user": username,
"period": period,
})
if err != nil {
return fmtError(err)
}
if len(userTop.Artists) == 0 {
return fmt.Sprintf(noTopArtistsForPeriodF, username)
}
return f.fmtUserTop(username, userTop)
}
// formats a "now playing" string from recent user tracks, e.g.:
// ne7split is now playing Undergang "Kronisk betændelse i tarmene" (Døden læger
// alle sår) [10x] (death metal, old school death metal, danish, breakbeat, seen
......@@ -147,6 +184,26 @@ func (f *Fn) fmtUserTrackStats(username string, track lastfm.TrackGetInfo) strin
)
}
func (f *Fn) fmtUserTop(username string, top lastfm.UserGetTopArtists) string {
var (
output []string
max = 9
)
if len(top.Artists) - 1 < max {
max = len(top.Artists) - 1
}
for i := 0; i <= max; i++ {
output = append(output, fmt.Sprintf("%s [%s]",
top.Artists[i].Name,
top.Artists[i].PlayCount,
))
}
return strings.Join(output, ", ")
}
func (f *Fn) getUserTrackInfo(username string, artist string, track string) (lastfm.TrackGetInfo, error) {
return f.LastFM.Track.GetInfo(lastfm.P{
"artist": artist,
......
......@@ -10,7 +10,13 @@ import (
"git.netsplit.uk/stella-irc/modlastfm/internal/app/modlastfm/util"
)
var commands = make(map[string]command.FnCommand)
var (
// userCommands require a username
userCommands = make(map[string]command.FnCommand)
// openCommands do not require a username, only arguments
openCommands = make(map[string]command.OpenFnCommand)
)
// sets up fn commands (which contain the configuration, database and an
// instance of shkh/lastfm-go/lastfm)
......@@ -21,21 +27,22 @@ func New(c *util.Config, db *bolt.DB) {
LastFM: lastfm.New(c.APIKey, c.APISecret),
}
commands = map[string]command.FnCommand{
userCommands = map[string]command.FnCommand{
"l": fn.NowPlaying,
"np": fn.NowPlaying,
"nowplaying": fn.NowPlaying,
//"topw": fn.UserTopWeek,
//"topm": fn.UserTopMonth,
//"topy": fn.UserTopYear,
//"topa": fn.UserTopAll,
"plays": fn.UserArtistStats,
"aplays": fn.UserAlbumStats,
"splays": fn.UserTrackStats,
//"artist": fn.ArtistInfo,
//"band": fn.ArtistInfo,
//"similar": fn.SimilarArtists,
"topw": fn.UserTopWeek,
"topm": fn.UserTopMonth,
"topy": fn.UserTopYear,
"topa": fn.UserTopAll,
"plays": fn.UserArtistStats,
"aplays": fn.UserAlbumStats,
"splays": fn.UserTrackStats,
}
openCommands = map[string]command.OpenFnCommand{
"artist": fn.ArtistInfo,
"band": fn.ArtistInfo,
}
}
......@@ -43,11 +50,16 @@ func New(c *util.Config, db *bolt.DB) {
func Run(username string, message string) string {
var arguments = strings.Split(message, " ")
fnCommand := commands[arguments[0]]
userFnCommand := userCommands[arguments[0]]
if userFnCommand != nil {
return userFnCommand(strings.Join(arguments[1:], " "), username)
}
openFnCommand := openCommands[arguments[0]]
if fnCommand != nil {
// TODO: split off command and arguments here?
return fnCommand(strings.Join(arguments[1:], " "), username)
if openFnCommand != nil {
return openFnCommand(strings.Join(arguments[1:], " "))
}
return ""
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment