Commit 63acf5bf authored by Mike Jones's avatar Mike Jones 馃尪

Song plays

parent c1cfbc44
...@@ -22,7 +22,7 @@ func main() { ...@@ -22,7 +22,7 @@ func main() {
gauntlet.New(config, db) gauntlet.New(config, db)
out := gauntlet.Run("ne7split", `aplays Odraza "Esperalem tkane"`) out := gauntlet.Run("ne7split", `splays Begrime Exemious "Noose for a Monarch"`)
log.Println(out) log.Println(out)
......
...@@ -2,9 +2,10 @@ package command ...@@ -2,9 +2,10 @@ package command
import ( import (
"fmt" "fmt"
"strconv"
"github.com/boltdb/bolt" "github.com/boltdb/bolt"
"github.com/n7st/lastfm-go/lastfm" "github.com/n7st/lastfm-go/lastfm"
"strconv"
"git.netsplit.uk/stella-irc/modlastfm/internal/app/modlastfm/util" "git.netsplit.uk/stella-irc/modlastfm/internal/app/modlastfm/util"
) )
......
...@@ -8,6 +8,8 @@ import ( ...@@ -8,6 +8,8 @@ import (
"github.com/n7st/lastfm-go/lastfm" "github.com/n7st/lastfm-go/lastfm"
) )
const statLineF = "%s by %s has %s total plays, of which %s are %s's (%s)"
func (f *Fn) NowPlaying(message string, username string) string { func (f *Fn) NowPlaying(message string, username string) string {
resp, err := f.LastFM.User.GetRecentTracks(lastfm.P{ resp, err := f.LastFM.User.GetRecentTracks(lastfm.P{
"autocorrect": 1, "autocorrect": 1,
...@@ -51,14 +53,20 @@ func (f *Fn) UserAlbumStats(message string, username string) string { ...@@ -51,14 +53,20 @@ func (f *Fn) UserAlbumStats(message string, username string) string {
return f.fmtUserAlbumStats(username, userAlbumInfo) return f.fmtUserAlbumStats(username, userAlbumInfo)
} }
func (f *Fn) fmtUserArtistStats(username string, info lastfm.ArtistGetInfo) string { func (f *Fn) UserTrackStats(message string, username string) string {
return fmt.Sprintf("%s have %s total plays, of which %s are %s's (%s)", artist, songTitle := extractTitles(message)
info.Name,
info.Stats.Plays, if artist == "" || songTitle == "" {
info.Stats.UserPlays, return `Format: splays Artist Name "Song Title"`
username, }
fmtPlaycountPercentage(info.Stats.UserPlays, info.Stats.Plays),
) userTrackInfo, err := f.getUserTrackInfo(username, artist, songTitle)
if err != nil {
return fmtError(err)
}
return f.fmtUserTrackStats(username, userTrackInfo)
} }
// formats a "now playing" string from recent user tracks, e.g.: // formats a "now playing" string from recent user tracks, e.g.:
...@@ -107,8 +115,18 @@ func (f *Fn) fmtNowPlaying(username string, np lastfm.UserGetRecentTracks) strin ...@@ -107,8 +115,18 @@ func (f *Fn) fmtNowPlaying(username string, np lastfm.UserGetRecentTracks) strin
) )
} }
func (f *Fn) fmtUserArtistStats(username string, info lastfm.ArtistGetInfo) string {
return fmt.Sprintf("%s have %s total plays, of which %s are %s's (%s)",
info.Name,
info.Stats.Plays,
info.Stats.UserPlays,
username,
fmtPlaycountPercentage(info.Stats.UserPlays, info.Stats.Plays),
)
}
func (f *Fn) fmtUserAlbumStats(username string, album lastfm.AlbumGetInfo) string { func (f *Fn) fmtUserAlbumStats(username string, album lastfm.AlbumGetInfo) string {
return fmt.Sprintf("%s by %s has %s total plays, of which %s are %s's (%s)", return fmt.Sprintf(statLineF,
album.Name, album.Name,
album.Artist, album.Artist,
album.PlayCount, album.PlayCount,
...@@ -118,6 +136,17 @@ func (f *Fn) fmtUserAlbumStats(username string, album lastfm.AlbumGetInfo) strin ...@@ -118,6 +136,17 @@ func (f *Fn) fmtUserAlbumStats(username string, album lastfm.AlbumGetInfo) strin
) )
} }
func (f *Fn) fmtUserTrackStats(username string, track lastfm.TrackGetInfo) string {
return fmt.Sprintf(statLineF,
track.Artist.Name,
track.Name,
track.PlayCount,
track.UserPlayCount,
username,
fmtPlaycountPercentage(track.UserPlayCount, track.PlayCount),
)
}
func (f *Fn) getUserTrackInfo(username string, artist string, track string) (lastfm.TrackGetInfo, error) { func (f *Fn) getUserTrackInfo(username string, artist string, track string) (lastfm.TrackGetInfo, error) {
return f.LastFM.Track.GetInfo(lastfm.P{ return f.LastFM.Track.GetInfo(lastfm.P{
"artist": artist, "artist": artist,
......
...@@ -25,9 +25,17 @@ func New(c *util.Config, db *bolt.DB) { ...@@ -25,9 +25,17 @@ func New(c *util.Config, db *bolt.DB) {
"l": fn.NowPlaying, "l": fn.NowPlaying,
"np": fn.NowPlaying, "np": fn.NowPlaying,
"nowplaying": fn.NowPlaying, "nowplaying": fn.NowPlaying,
//"topw": fn.UserTopWeek,
//"topm": fn.UserTopMonth,
//"topy": fn.UserTopYear,
//"topa": fn.UserTopAll,
"plays": fn.UserArtistStats, "plays": fn.UserArtistStats,
"aplays": fn.UserAlbumStats, "aplays": fn.UserAlbumStats,
"splays": fn.UserTrackStats,
//"artist": fn.ArtistInfo,
//"band": fn.ArtistInfo,
//"similar": fn.SimilarArtists,
} }
} }
......
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