forked from 0x263b/Porygon2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
charts.go
51 lines (40 loc) · 1.3 KB
/
charts.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package lastfm
import (
"fmt"
"github.com/Seventy-Two/Cara"
"github.com/Seventy-Two/Cara/web"
"strings"
)
const (
ChartsURL = "http://ws.audioscrobbler.com/2.0/?method=user.gettopartists&user=%s&period=7day&limit=5&api_key=%s&format=json"
)
func charts(command *bot.Cmd, matches []string) (msg string, err error) {
username := checkLastfm(command.Nick, matches[1])
if username == "" {
return "Lastfm not provided, nor on file. Use `.set lastfm <lastfm>` to save", nil
}
data := &WeeklyCharts{}
err = web.GetJSON(fmt.Sprintf(ChartsURL, username, bot.Config.API.Lastfm), data)
if err != nil || data.Error > 0 {
return fmt.Sprintf("Could not get charts for %s", username), nil
}
if data.Topartists.Attr.Total == "0" {
return fmt.Sprintf("Could not get charts for %s", username), nil
}
var fmtcharts string
var trunc int = 5
if len(data.Topartists.Artist) < trunc {
trunc = len(data.Topartists.Artist)
}
for i := range data.Topartists.Artist[:trunc] {
fmtcharts += fmt.Sprintf("%s (%s), ", data.Topartists.Artist[i].Name, data.Topartists.Artist[i].Playcount)
}
fmtcharts = strings.TrimSuffix(fmtcharts, ", ")
output := fmt.Sprintf("Last.fm | Top 5 Weekly artists for %s | %s", username, fmtcharts)
return output, nil
}
func init() {
bot.RegisterCommand(
"^charts(?: (\\S+))?$",
charts)
}