Skip to content

Commit

Permalink
feat: add top artists subcommands
Browse files Browse the repository at this point in the history
  • Loading branch information
surskitt committed Sep 1, 2024
1 parent a030f44 commit af6cdf0
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 5 deletions.
104 changes: 100 additions & 4 deletions lastfm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"strings"

"github.com/imroc/req/v3"
)
Expand Down Expand Up @@ -29,11 +30,11 @@ func colourList(in []string) (out []string) {
return out
}

type lastfmJSON struct {
type lastfmUserGetRecentTracks struct {
Recenttracks Recenttracks `json:"recenttracks"`
}

func (j lastfmJSON) String() string {
func (j lastfmUserGetRecentTracks) String() string {
return j.Recenttracks.String()
}

Expand All @@ -50,7 +51,7 @@ func (r Recenttracks) String() string {

track := r.Tracks[0]

return fmt.Sprintf(" %s %s: %s ", r.User, track.action(), track)
return fmt.Sprintf("{green}{clear} %s %s: %s {green}{clear}", r.User, track.action(), track)
}

// User represents the user information returned in the lastfm json
Expand Down Expand Up @@ -105,7 +106,7 @@ func (t Track) action() string {
}

func lastfmNewestScrobble(client *req.Client, user string) (msg string, err error) {
j := &lastfmJSON{}
j := &lastfmUserGetRecentTracks{}

_, err = client.R().
SetQueryParam("method", "user.getrecenttracks").
Expand All @@ -121,3 +122,98 @@ func lastfmNewestScrobble(client *req.Client, user string) (msg string, err erro

return j.String(), nil
}

type lastfmUserGetTopArtists struct {
TopArtists struct {
Artists []struct {
Name string `json:"name"`
PlayCount string `json:"playcount"`
} `json:"artist"`
} `json:"topartists"`
}

func lastfmTopArtists(client *req.Client, user, period string) (msg string, err error) {
j := &lastfmUserGetTopArtists{}

_, err = client.R().
SetQueryParam("method", "user.gettopartists").
SetQueryParam("user", user).
SetQueryParam("format", "json").
SetQueryParam("limit", "10").
SetQueryParam("period", period).
SetSuccessResult(&j).
Get(lastfmAPIURL)

if err != nil {
return "", err
}

artists := []string{}
for _, a := range j.TopArtists.Artists {
artists = append(artists, fmt.Sprintf("%s (%s)", a.Name, a.PlayCount))
}

cl := colourList(artists)

return strings.Join(cl, ", "), err
}

func lastfmTopArtistsAllTime(client *req.Client, user string) (msg string, err error) {
topartists, err := lastfmTopArtists(client, user, "overall")

if err != nil {
return "", err
}

return fmt.Sprintf("%s's top artists: %s", user, topartists), nil
}

func lastfmTopArtistsWeekly(client *req.Client, user string) (msg string, err error) {
topartists, err := lastfmTopArtists(client, user, "7day")

if err != nil {
return "", err
}

return fmt.Sprintf("%s's top artists (last week): %s", user, topartists), nil
}

func lastfmTopArtistsMonthly(client *req.Client, user string) (msg string, err error) {
topartists, err := lastfmTopArtists(client, user, "1month")

if err != nil {
return "", err
}

return fmt.Sprintf("%s's top artists (last month): %s", user, topartists), nil
}

func lastfmTopArtists3Monthly(client *req.Client, user string) (msg string, err error) {
topartists, err := lastfmTopArtists(client, user, "3month")

if err != nil {
return "", err
}

return fmt.Sprintf("%s's top artists (last 3 months): %s", user, topartists), nil
}

func lastfmTopArtists6Monthly(client *req.Client, user string) (msg string, err error) {
topartists, err := lastfmTopArtists(client, user, "6month")

if err != nil {
return "", err
}

return fmt.Sprintf("%s's top artists (last 6 months): %s", user, topartists), nil
}

func lastfmTopArtistsYearly(client *req.Client, user string) (msg string, err error) {
topartists, err := lastfmTopArtists(client, user, "12month")

if err != nil {
return "", err
}

return fmt.Sprintf("%s's top artists (last year): %s", user, topartists), nil
}
2 changes: 1 addition & 1 deletion lastfm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestString(t *testing.T) {
{
name: "Valid result",
obj: rt,
expected: " testuser last listened to: testartist - testtrack (testalbum) ",
expected: "{green}{clear} testuser last listened to: testartist - testtrack (testalbum) {green}{clear}",
},
{
name: "Invalid results",
Expand Down
18 changes: 18 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,29 @@ func lastfmHandler(client *req.Client, kv *bolt.DB, m *gowon.Message) (string, e
return setUserHandler(kv, m.Nick, user)
case "l", "scrobbles":
return CommandHandler(client, kv, m.Nick, user, lastfmNewestScrobble)
case "ta", "topartists":
return CommandHandler(client, kv, m.Nick, user, lastfmTopArtistsAllTime)
case "taw", "topartistsweekly":
return CommandHandler(client, kv, m.Nick, user, lastfmTopArtistsWeekly)
case "tam", "topartistsmonthly":
return CommandHandler(client, kv, m.Nick, user, lastfmTopArtistsMonthly)
case "ta3m", "topartists3monhtly":
return CommandHandler(client, kv, m.Nick, user, lastfmTopArtists3Monthly)
case "ta6m", "topartists6monhtly":
return CommandHandler(client, kv, m.Nick, user, lastfmTopArtists6Monthly)
case "tay", "topartistsyearly":
return CommandHandler(client, kv, m.Nick, user, lastfmTopArtistsYearly)
}

commands := []string{
"set (s)",
"scrobbles (l)",
"topartists (ta)",
"topartistsweekly (taw)",
"topartistsmonthly (tam)",
"topartists3monthly (ta3m)",
"topartists6monthly (ta6m)",
"topartistsyearly (tay)",
}

cl := colourList(commands)
Expand Down

0 comments on commit af6cdf0

Please sign in to comment.