This repository has been archived by the owner on Feb 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lastfm.go
350 lines (311 loc) · 9.48 KB
/
lastfm.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package dhelpers
import (
"strconv"
"time"
"context"
"github.com/Seklfreak/lastfm-go/lastfm"
"github.com/opentracing/opentracing-go"
"gitlab.com/Cacophony/dhelpers/cache"
)
// LastFmPeriod is a type for periods used for Last.FM requests
type LastFmPeriod string
// defines possible LastFM periods
const (
LastFmPeriodOverall LastFmPeriod = "overall"
LastFmPeriod7day = "7day"
LastFmPeriod1month = "1month"
LastFmPeriod3month = "3month"
LastFmPeriod6month = "6month"
LastFmPeriod12month = "12month"
)
const (
lastFmTargetImageSize = "extralarge"
)
// LastfmUserData contains information about an User on LastFM
type LastfmUserData struct {
Username string
Name string
Icon string
Scrobbles int
Country string
AccountCreation time.Time
}
// LastfmTrackData contains information about a Track on LastFM
type LastfmTrackData struct {
Name string
URL string
ImageURL string
Artist string
ArtistURL string
ArtistImageURL string
Album string
Time time.Time
Loved bool
NowPlaying bool
Scrobbles int
// used for guild stats
Users int
}
// LastfmArtistData contains information about an Artist on LastFM
type LastfmArtistData struct {
Name string
URL string
ImageURL string
Scrobbles int
}
// LastfmAlbumData contains information about an Album on LastFM
type LastfmAlbumData struct {
Name string
URL string
ImageURL string
Artist string
ArtistURL string
Scrobbles int
}
// LastFmGuildTopTracks contains the top tracks for a guild, it is built by the Worker and stored in redis
type LastFmGuildTopTracks struct {
GuildID string
NumberOfUsers int
Period LastFmPeriod
Tracks []LastfmTrackData
CachedAt time.Time
}
// LastFmGuildTopTracksKey returns the redis key for LastFmGuildTopTracks
func LastFmGuildTopTracksKey(guildID string, period LastFmPeriod) (key string) {
return "project-d:lastfm:guild-top-tracks:" + guildID + ":" + string(period)
}
// LastFmGetUserinfo returns information about a LastFM user
func LastFmGetUserinfo(ctx context.Context, lastfmUsername string) (userData LastfmUserData, err error) {
// start tracing span
var span opentracing.Span
span, _ = opentracing.StartSpanFromContext(ctx, "dhelpers.LastFmGetUserinfo")
defer span.Finish()
// request data
var lastfmUser lastfm.UserGetInfo
lastfmUser, err = cache.GetLastFm().User.GetInfo(lastfm.P{"user": lastfmUsername})
if err != nil {
return userData, err
}
// parse fields into lastfmUserData
userData.Username = lastfmUser.Name
userData.Name = lastfmUser.RealName
userData.Country = lastfmUser.Country
if lastfmUser.PlayCount != "" {
userData.Scrobbles, _ = strconv.Atoi(lastfmUser.PlayCount) // nolint: errcheck, gas
}
if len(lastfmUser.Images) > 0 {
for _, image := range lastfmUser.Images {
if image.Size == lastFmTargetImageSize {
userData.Icon = image.Url
}
}
}
if lastfmUser.Registered.Unixtime != "" {
timeI, err := strconv.ParseInt(lastfmUser.Registered.Unixtime, 10, 64)
if err == nil {
userData.AccountCreation = time.Unix(timeI, 0)
}
}
return userData, nil
}
// LastFmGetRecentTracks returns recent tracks listened to by an user
func LastFmGetRecentTracks(ctx context.Context, lastfmUsername string, limit int) (tracksData []LastfmTrackData, err error) {
// start tracing span
var span opentracing.Span
span, _ = opentracing.StartSpanFromContext(ctx, "dhelpers.LastFmGetRecentTracks")
defer span.Finish()
// request data
var lastfmRecentTracks lastfm.UserGetRecentTracksExtended
lastfmRecentTracks, err = cache.GetLastFm().User.GetRecentTracksExtended(lastfm.P{
"limit": limit + 1, // in case nowplaying + already scrobbled
"user": lastfmUsername,
})
if err != nil {
return nil, err
}
// parse fields
if lastfmRecentTracks.Total > 0 {
for i, track := range lastfmRecentTracks.Tracks {
if i == 1 {
// prevent nowplaying + already scrobbled
if lastfmRecentTracks.Tracks[0].Url == track.Url {
continue
}
}
lastTrack := LastfmTrackData{
Name: track.Name,
URL: track.Url,
Artist: track.Artist.Name,
ArtistURL: track.Artist.Url,
Album: track.Album.Name,
Loved: false,
}
for _, image := range track.Images {
if image.Size == lastFmTargetImageSize {
lastTrack.ImageURL = image.Url
}
}
for _, image := range track.Artist.Image {
if image.Size == lastFmTargetImageSize {
lastTrack.ArtistImageURL = image.Url
}
}
if track.Loved == "1" || track.Loved == "true" {
lastTrack.Loved = true
}
if track.NowPlaying == "1" || track.NowPlaying == "true" {
lastTrack.NowPlaying = true
}
timestamp, err := strconv.Atoi(track.Date.Uts)
if err == nil {
lastTrack.Time = time.Unix(int64(timestamp), 0)
}
tracksData = append(tracksData, lastTrack)
if len(tracksData) >= limit {
break
}
}
}
return tracksData, nil
}
// LastFmGetTopArtists returns the top artists of an user
func LastFmGetTopArtists(ctx context.Context, lastfmUsername string, limit int, period LastFmPeriod) (artistsData []LastfmArtistData, err error) {
// start tracing span
var span opentracing.Span
span, _ = opentracing.StartSpanFromContext(ctx, "dhelpers.LastFmGetTopArtists")
defer span.Finish()
// request data
var lastfmTopArtists lastfm.UserGetTopArtists
lastfmTopArtists, err = cache.GetLastFm().User.GetTopArtists(lastfm.P{
"limit": limit,
"user": lastfmUsername,
"period": string(period),
})
if err != nil {
return nil, err
}
// parse fields
if lastfmTopArtists.Total > 0 {
for _, artist := range lastfmTopArtists.Artists {
lastArtist := LastfmArtistData{
Name: artist.Name,
URL: artist.Url,
}
for _, image := range artist.Images {
if image.Size == lastFmTargetImageSize {
lastArtist.ImageURL = image.Url
}
}
lastArtist.Scrobbles, _ = strconv.Atoi(artist.PlayCount) // nolint: gas
artistsData = append(artistsData, lastArtist)
if len(artistsData) >= limit {
break
}
}
}
return artistsData, nil
}
// LastFmGetTopTracks returns the top tracks of an user
func LastFmGetTopTracks(ctx context.Context, lastfmUsername string, limit int, period LastFmPeriod) (tracksData []LastfmTrackData, err error) {
// start tracing span
var span opentracing.Span
span, _ = opentracing.StartSpanFromContext(ctx, "dhelpers.LastFmGetTopTracks")
defer span.Finish()
// request data
var lastfmTopTracks lastfm.UserGetTopTracks
lastfmTopTracks, err = cache.GetLastFm().User.GetTopTracks(lastfm.P{
"limit": limit,
"user": lastfmUsername,
"period": string(period),
})
if err != nil {
return nil, err
}
// parse fields
if lastfmTopTracks.Total > 0 {
for _, track := range lastfmTopTracks.Tracks {
lastTrack := LastfmTrackData{
Name: track.Name,
URL: track.Url,
Artist: track.Artist.Name,
ArtistURL: track.Artist.Url,
}
for _, image := range track.Images {
if image.Size == lastFmTargetImageSize {
lastTrack.ImageURL = image.Url
}
}
lastTrack.Scrobbles, _ = strconv.Atoi(track.PlayCount) // nolint: gas
tracksData = append(tracksData, lastTrack)
if len(tracksData) >= limit {
break
}
}
}
return tracksData, nil
}
// LastFmGetTopAlbums returns the top albums of an user
func LastFmGetTopAlbums(ctx context.Context, lastfmUsername string, limit int, period LastFmPeriod) (albumsData []LastfmAlbumData, err error) {
// start tracing span
var span opentracing.Span
span, _ = opentracing.StartSpanFromContext(ctx, "dhelpers.LastFmGetTopAlbums")
defer span.Finish()
// request data
var lastfmTopAlbums lastfm.UserGetTopAlbums
lastfmTopAlbums, err = cache.GetLastFm().User.GetTopAlbums(lastfm.P{
"limit": limit,
"user": lastfmUsername,
"period": string(period),
})
if err != nil {
return nil, err
}
// parse fields
if lastfmTopAlbums.Total > 0 {
for _, album := range lastfmTopAlbums.Albums {
lastAlbum := LastfmAlbumData{
Name: album.Name,
URL: album.Url,
Artist: album.Artist.Name,
ArtistURL: album.Artist.Url,
}
for _, image := range album.Images {
if image.Size == lastFmTargetImageSize {
lastAlbum.ImageURL = image.Url
}
}
lastAlbum.Scrobbles, _ = strconv.Atoi(album.PlayCount) // nolint: gas
albumsData = append(albumsData, lastAlbum)
if len(albumsData) >= limit {
break
}
}
}
return albumsData, nil
}
// LastFmGetPeriodFromArgs parses args to figure out the correct period
func LastFmGetPeriodFromArgs(args []string) (period LastFmPeriod, newArgs []string) {
for i, arg := range args {
switch arg {
case "7day", "7days", "week", "7", "seven":
newArgs = append(args[:i], args[i+1:]...)
return LastFmPeriod7day, newArgs
case "1month", "month", "1", "one":
newArgs = append(args[:i], args[i+1:]...)
return LastFmPeriod1month, newArgs
case "3month", "threemonths", "quarter", "3", "three":
newArgs = append(args[:i], args[i+1:]...)
return LastFmPeriod3month, newArgs
case "6month", "halfyear", "half", "sixmonths", "6", "six":
newArgs = append(args[:i], args[i+1:]...)
return LastFmPeriod6month, newArgs
case "12month", "year", "twelvemonths", "12", "twelve":
newArgs = append(args[:i], args[i+1:]...)
return LastFmPeriod12month, newArgs
case "overall", "all", "alltime", "all-time": // nolint: misspell
newArgs = append(args[:i], args[i+1:]...)
return LastFmPeriodOverall, newArgs
}
}
return LastFmPeriodOverall, args
}