/
nowplaying.go
231 lines (191 loc) · 6.95 KB
/
nowplaying.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
package commands
import (
"fmt"
"os"
"github.com/andersfylling/disgord"
"github.com/fogleman/gg"
"github.com/nfnt/resize"
"github.com/pazuzu156/atlas"
"github.com/pazuzu156/persephone/fm"
"github.com/pazuzu156/persephone/lib"
)
// Nowplaying command.
type Nowplaying struct{ Command }
// InitNowPlaying initializes the nowplaying command.
func InitNowPlaying() Nowplaying {
return Nowplaying{Init(&CommandItem{
Name: "nowplaying",
Description: "Shows what you're currently listening to",
Aliases: []string{"np"},
})}
}
// Register registers and runs the nowplaying command.
func (c Nowplaying) Register() *atlas.Command {
c.CommandInterface.Run = func(ctx atlas.Context) {
track, err := fm.GetNowPlayingTrack(ctx.Message.Author, c.Lastfm)
lfmuser, _ := lib.GetLastfmUserInfo(ctx.Message.Author, c.Lastfm)
if err == nil {
res, _ := lib.Grab(track.Images[3].URL)
// Open base images
bg, _ := lib.OpenImage(lib.LocGet("static/images/background.png"))
aa, aaf := lib.OpenImage(res.Filename)
if aa == nil {
aa, aaf = lib.OpenImage(lib.LocGet("static/images/bm.png"))
}
avURL, _ := ctx.Message.Author.AvatarURL(256, false)
res, _ = lib.Grab(avURL)
av, avf := lib.OpenImage(res.Filename)
// delete downloaded images (they're already loaded into memory)
os.Remove(aaf.Name())
os.Remove(avf.Name())
// Some resizing for avatar and album art
aar := resize.Resize(240, 240, aa, resize.Bicubic)
avr := resize.Resize(72, 72, av, resize.Bicubic)
// New image context, and add background image
dc := gg.NewContext(1000, 600)
dc.DrawImage(bg, 0, 0)
// Draw avatar (also add the white bar that goes behind the avatar image)
dc.SetRGBA(1, 1, 1, 0.2)
dc.DrawRectangle(0, 100, 1000, 72)
dc.Fill()
// Draw avatar and add username + scrobble count
dc.DrawImage(avr, 315, 100)
dc.LoadFontFace(FontBold, 26)
dc.SetRGB(0.2, 0.2, 0.2)
dc.DrawString(ctx.Message.Author.Username+" ("+lfmuser.Name+")", 391, 131)
dc.SetRGB(0.9, 0.9, 0.9)
dc.DrawString(ctx.Message.Author.Username+" ("+lfmuser.Name+")", 390, 130)
// scrobble count
dc.LoadFontFace(FontRegular, 20)
dc.SetRGB(0.2, 0.2, 0.2)
dc.DrawString(fmt.Sprintf("%s scrobbles", lib.HumanNumber(lfmuser.PlayCount)), 391, 161)
dc.SetRGB(0.9, 0.9, 0.9)
dc.DrawString(fmt.Sprintf("%s scrobbles", lib.HumanNumber(lfmuser.PlayCount)), 390, 160)
// Draw white box that goes behind album art + draw album art
dc.SetRGBA(1, 1, 1, 0.2)
dc.DrawRectangle(50, 0, 250, 600)
dc.Fill()
dc.DrawImage(aar, 55, 105)
// Draw artist name
dc.LoadFontFace(FontBold, 20)
dc.SetRGB(0.2, 0.2, 0.2)
dc.DrawStringWrapped(lib.ShortStr(track.Name, 33), 71, 371, 0, 0, 200, 1.5, gg.AlignLeft)
dc.SetRGB(0.9, 0.9, 0.9)
dc.DrawStringWrapped(lib.ShortStr(track.Name, 33), 70, 370, 0, 0, 200, 1.5, gg.AlignLeft)
// Draw album + track name
dc.LoadFontFace(FontRegular, 20)
dc.SetRGB(0.2, 0.2, 0.2)
dc.DrawStringWrapped(track.Artist.Name+" - "+track.Album.Name, 71, 441, 0, 0, 200, 1.5, gg.AlignLeft)
dc.SetRGB(0.9, 0.9, 0.9)
dc.DrawStringWrapped(track.Artist.Name+" - "+track.Album.Name, 70, 440, 0, 0, 200, 1.5, gg.AlignLeft)
// This gets the last 3 listened tracks and draws
// images + text for each
// tracks are layered in reverse order 3 -> 2 -> 1 displayed
// in ascending order
tracks, _ := fm.GetRecentTracks(ctx.Message.Author, c.Lastfm, "3")
if len(tracks) > 3 {
t1 := tracks[1] // most recent track
t2 := tracks[2] // second most recent track
t3 := tracks[3] // third most recent track
// This needs to be rendered first, as it'll be in the back
// behind the other 2 recent tracks
if img := t3.Images[3].URL; img != "" {
dc.SetRGBA(0, 0, 0, 0.3)
dc.DrawRoundedRectangle(336, 396, 168, 168, 85)
dc.Fill()
dc.DrawRoundedRectangle(340, 400, 160, 160, 80)
dc.Clip()
i, _ := lib.Grab(img)
ii, _ := lib.OpenImage(i.Filename)
if ii != nil {
iir := resize.Resize(160, 160, ii, resize.Bicubic)
dc.DrawImage(iir, 340, 400)
}
dc.ResetClip()
os.Remove(i.Filename)
dc.LoadFontFace(FontBold, 25)
dc.SetRGB(0.2, 0.2, 0.2)
dc.DrawString(lib.ShortStr(t3.Name, 33), 511, 481)
dc.SetRGB(0.9, 0.9, 0.9)
dc.DrawString(lib.ShortStr(t3.Name, 33), 510, 480)
dc.LoadFontFace(FontRegular, 25)
dc.SetRGB(0.2, 0.2, 0.2)
dc.DrawString(t3.Artist.Name, 511, 521)
dc.SetRGB(0.9, 0.9, 0.9)
dc.DrawString(t3.Artist.Name, 510, 520)
}
// Track 2
if img := t2.Images[3].URL; img != "" {
dc.SetRGBA(0, 0, 0, 0.3)
dc.DrawRoundedRectangle(336, 306, 168, 168, 85)
dc.Fill()
dc.DrawRoundedRectangle(340, 310, 160, 160, 80)
dc.Clip()
i, _ := lib.Grab(img)
ii, _ := lib.OpenImage(i.Filename)
if ii != nil {
iir := resize.Resize(160, 160, ii, resize.Bicubic)
dc.DrawImage(iir, 340, 310)
}
dc.ResetClip()
os.Remove(i.Filename)
dc.LoadFontFace(FontBold, 25)
dc.SetRGB(0.2, 0.2, 0.2)
dc.DrawString(lib.ShortStr(t2.Name, 33), 511, 381)
dc.SetRGB(0.9, 0.9, 0.9)
dc.DrawString(lib.ShortStr(t2.Name, 33), 510, 380)
dc.LoadFontFace(FontRegular, 25)
dc.SetRGB(0.2, 0.2, 0.2)
dc.DrawString(t2.Artist.Name, 511, 421)
dc.SetRGB(0.9, 0.9, 0.9)
dc.DrawString(t2.Artist.Name, 510, 420)
}
// First most recent track
if img := t1.Images[3].URL; img != "" {
dc.SetRGBA(0, 0, 0, 0.3)
dc.DrawRoundedRectangle(336, 216, 168, 168, 85)
dc.Fill()
dc.DrawRoundedRectangle(340, 220, 160, 160, 80)
dc.Clip()
i, _ := lib.Grab(img)
ii, _ := lib.OpenImage(i.Filename)
if ii != nil {
iir := resize.Resize(160, 160, ii, resize.Bicubic)
dc.DrawImage(iir, 340, 220)
}
dc.ResetClip()
os.Remove(i.Filename)
dc.LoadFontFace(FontBold, 25)
dc.SetRGB(0.2, 0.2, 0.2)
dc.DrawString(lib.ShortStr(t1.Name, 33), 511, 281)
dc.SetRGB(0.9, 0.9, 0.9)
dc.DrawString(lib.ShortStr(t1.Name, 33), 510, 280)
dc.LoadFontFace(FontRegular, 25)
dc.SetRGB(0.2, 0.2, 0.2)
dc.DrawString(t1.Artist.Name, 511, 321)
dc.SetRGB(0.9, 0.9, 0.9)
dc.DrawString(t1.Artist.Name, 510, 320)
}
}
lib.BrandImage(dc) // brand imagea
r, _ := lib.SaveImage(dc, ctx, "np")
// create new message with the image + embed with a link to the user's Last.fm profile page
ctx.Atlas.CreateMessage(ctx.Context, ctx.Message.ChannelID, &disgord.CreateMessageParams{
Files: []disgord.CreateMessageFileParams{
{FileName: lib.TagImageName(ctx, "np") + ".png", Reader: r},
},
Embed: &disgord.Embed{
Title: fmt.Sprintf("View %s's Profile on Last.fm", ctx.Message.Author.Username),
URL: fmt.Sprintf("https://last.fm/user/%s", lfmuser.Name),
Color: lib.RandomColor(),
},
})
// Close and delete the image
r.Close()
os.Remove(r.Name())
} else {
ctx.Message.Reply(ctx.Context, ctx.Atlas, err.Error())
}
}
return c.CommandInterface
}