forked from Jessidhia/go-lastfm-bot
/
bot.go
executable file
·564 lines (526 loc) · 15.6 KB
/
bot.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
package main
import (
"compress/zlib"
"crypto/tls"
"flag"
"fmt"
"github.com/Kovensky/go-lastfm"
"github.com/fluffle/goirc/client"
"log"
"os"
"os/signal"
"regexp"
"strings"
"syscall"
"time"
)
var (
botNick = flag.String("nick", "Lastfm_bot", `The nickname the bot should use.`)
server = flag.String("server", "", `The IRC server to connect to. Required. If a ":" is present, uses the right side as the port, otherwise connects to port 6667.`)
useSSL = flag.Bool("ssl", false, `Whether to use explicit SSL. Changes the default port to 6697.`)
password = flag.String("password", "", `The password needed to connect to the server, if any.`)
nickPass = flag.String("nickserv-password", "", `A NickServ password to authenticate the bot, if any. Tested on Freenode and SynIRC.`)
channelList = flag.String("channels", "", `Comma-separated list of channels to join on the server. Required`)
apiKey = flag.String("api-key", "", `The Last.fm API key. Required.`)
cmdPrefix = flag.String("cmd-prefix", ".", `The prefix to user commands.`)
cacheFile = flag.String("cache-file", "", `File used to persist the last.fm API cache. If blank, the cache is only kept in memory. Not multiprocess safe.`)
lfm lastfm.LastFM
nickMap = NewNickMap()
cacheTimer *time.Timer
)
var whitespace = regexp.MustCompile(`\s+`)
func onPrivmsg(irc *client.Conn, line *client.Line) {
words := whitespace.Split(line.Args[1], -1)
switch words[0] {
case *cmdPrefix + "wp":
go reportAllNowPlaying(irc, line.Nick, line.Args[0])
case *cmdPrefix + "np":
who := line.Nick
if len(words) > 1 && words[1] != "" {
who = words[1]
}
go reportNowPlaying(irc, line.Args[0], line.Nick, who, false)
case *cmdPrefix + "compare":
who := line.Nick
target := ""
if len(words) > 2 && words[2] != "" {
who = words[1]
target = words[2]
} else if len(words) > 1 {
target = words[1]
} else {
irc.Privmsg(line.Args[0], fmt.Sprintf("%s: tell me who to compare to!", line.Nick))
return
}
go doCompare(irc, line.Args[0], line.Nick, who, target)
case *cmdPrefix + "top5":
who := line.Nick
period := lastfm.Overall
if len(words) > 2 && words[2] != "" {
who = words[2]
}
if len(words) > 1 {
switch words[1] {
case "overall":
period = lastfm.Overall
case "year":
period = lastfm.OneYear
case "month":
period = lastfm.OneMonth
case "week":
period = lastfm.OneWeek
default:
irc.Privmsg(line.Args[0],
fmt.Sprintf("%s: usage: %stop5 ((overall|year|month|week) ($nick)?)?", line.Nick, *cmdPrefix))
return
}
}
go doTop5(irc, line.Args[0], line.Nick, period, who)
case *cmdPrefix + "setuser":
if len(words) < 2 || words[1] == "" {
irc.Privmsg(line.Args[0], fmt.Sprintf("%s: tell the username to associate with", line.Nick))
return
}
go nickMap.AddNick(irc, line.Args[0], line.Nick, words[1])
case *cmdPrefix + "ignore":
go nickMap.IgnoreNick(irc, line.Args[0], line.Nick)
case *cmdPrefix + "deluser":
go nickMap.DelNick(irc, line.Args[0], line.Nick)
case *cmdPrefix + "whois":
who := line.Nick
if len(words) > 1 && words[1] != "" {
who = words[1]
}
go nickMap.QueryNick(irc, line.Args[0], line.Nick, who)
case *cmdPrefix + "aka":
if len(words) < 2 {
irc.Privmsg(line.Args[0], fmt.Sprintf("%s: tell the username to know about", line.Nick))
return
}
go nickMap.ListAllNicks(irc, line.Args[0], line.Nick, words[1])
case *cmdPrefix + "help":
go sendHelp(irc, line.Nick)
}
}
var helpSplit = regexp.MustCompile(`\s{2,}`)
func sendHelp(irc *client.Conn, nick string) {
helpStr := `
Last.fm commands:
` + *cmdPrefix + `np ($user)?: Shows your now playing song. If you give $user, queries for that $user.
` + *cmdPrefix + `compare ($user1) ($user2)?: Runs a tasteometer compare between you and $user1, or between $user1 and $user2 if present.
` + *cmdPrefix + `top5 ((overall|year|month|week) ($user)?)?: Shows the top5 artists in the chosen period for you or the $user.
` + *cmdPrefix + `whois ($nick)?: Shows your associated last.fm username, or the username associated with $nick.
` + *cmdPrefix + `aka ($username): Shows the nicks that have been associated with $username.
A nick can be used in place of a username if it's associated with a last.fm account.
`
if *requireAuth {
helpStr += `Commands that require that you be authenticated with NickServ:`
}
// There's also *cmdPrefix + "wp", but we don't document it to not encourage abuse.
helpStr += `
` + *cmdPrefix + `ignore: Makes the bot ignore you for most commands. Use ` +
*cmdPrefix + `setuser or ` + *cmdPrefix + `deluser to be unignored.
` + *cmdPrefix + `setuser ($username): Associates your nick with the given last.fm $username.
` + *cmdPrefix + `deluser: Removes your nick's association, if any.
` // + *cmdPrefix + `wp: Shows what's playing for everyone in the channel.` // uncomment this at your peril :)
for _, line := range helpSplit.Split(helpStr, -1) {
if line != "" {
irc.Notice(nick, line)
}
}
}
func reportIgnored(irc *client.Conn, asker, who string) {
if asker == who {
irc.Notice(asker, "You asked to be ignored by last.fm commands")
} else {
irc.Notice(asker, fmt.Sprintf("%s asked to be ignored by last.fm commands", who))
}
}
func onInvite(irc *client.Conn, line *client.Line) {
who, channel := line.Args[0], line.Args[1]
log.Println(line.Nick, "invited bot to", channel)
if who == irc.Me.Nick {
// some IRCds only allow operators to INVITE, and on registered channels normally only identified users are operators
// check anyway, since there are some corner cases where that doesn't happen
if checkIdentified(irc, line.Nick) {
log.Println("Accepting invite to", channel)
irc.Join(channel)
} else {
irc.Notice(line.Nick, "you must be identified to invite")
log.Println("Ignoring invite, user is not identified")
}
}
}
func doTop5(irc *client.Conn, target, asker string, period lastfm.Period, user string) {
log.Println("Listing top", period, "5 artists for", user)
lfmUser, _ := nickMap.GetUser(user)
if lfmUser == "" {
reportIgnored(irc, asker, user)
return
}
top5, err := lfm.GetUserTopArtists(lfmUser, period, 5)
if err != nil {
irc.Privmsg(target, fmt.Sprintf("[%s] %v", user, err))
return
}
artists := []string{}
for _, a := range top5.Artists {
artists = append(artists, a.Name)
}
r := fmt.Sprintf("[%s] %v top5: %s",
user, period, strings.Join(artists, ", "))
log.Println("Reply:", r)
irc.Privmsg(target, r)
saveCache()
}
func doCompare(irc *client.Conn, target, asker, user1, user2 string) {
log.Println("Comparing", user1, "with", user2)
lfmUser1, _ := nickMap.GetUser(user1)
lfmUser2, _ := nickMap.GetUser(user2)
if lfmUser1 == "" || lfmUser2 == "" {
if lfmUser1 == "" {
reportIgnored(irc, asker, user1)
} else {
reportIgnored(irc, asker, user2)
}
return
}
taste, err := lfm.CompareTaste(lfmUser1, lfmUser2)
if err != nil {
irc.Privmsg(target, fmt.Sprintf("[%s vs %s] %v", user1, user2, err))
return
}
r := fmt.Sprintf("[%s vs %s] %.2f%% -- %s",
user1, user2, taste.Score*100, strings.Join(taste.Artists, ", "))
log.Println("Reply:", r)
irc.Privmsg(target, r)
saveCache()
}
func reportNowPlaying(irc *client.Conn, target, asker, who string, onlyReportSuccess bool) bool {
log.Println("Reporting Now Playing for", who, "on channel", target)
user, _ := nickMap.GetUser(who)
if user == "" {
if !onlyReportSuccess {
reportIgnored(irc, asker, who)
}
return false
}
recent, err := lfm.GetRecentTracks(user, 1)
if err != nil {
extra := ""
lfmerr, ok := err.(*lastfm.LastFMError)
// lfmerr.Code is unreliable; a lot of things may be code 6...
if ok && lfmerr.Error() == "No user with that name was found" {
// plus their error messages are misleading...
extra = ", or user never scrobbled anything"
}
r := fmt.Sprintf("[%s] %v%s", who, err, extra)
if !onlyReportSuccess {
log.Println("Reply:", r)
irc.Privmsg(target, r)
} else {
log.Println(r)
}
saveCache()
return false
}
np := recent.NowPlaying
if np != nil {
c := make(chan interface{})
go func() {
r, err := lfm.GetTrackInfo(*np, user, true)
if err != nil {
c <- err
} else {
c <- r
}
}()
go func() {
r, err := lfm.GetTrackTopTags(*np, true)
if err != nil {
c <- err
} else {
c <- r
}
}()
go func() {
r, err := lfm.GetArtistTopTags(np.Artist, true)
if err != nil {
c <- err
} else {
c <- r
}
}()
var ti *lastfm.TrackInfo
var topTags *lastfm.TopTags
for reads := 0; reads < 3; reads++ {
select {
case rep := <-c:
switch r := rep.(type) {
case *lastfm.TrackInfo:
ti = r
case *lastfm.TopTags:
if r.Track != "" { // track tags
// only use if present
if len(r.Tags) > 0 {
topTags = r
}
} else { // artist tags
// only use if there are no track tags
if topTags == nil || len(topTags.Tags) == 0 {
topTags = r
}
}
case error:
log.Println(err)
default:
panic(r)
}
}
}
close(c)
if ti == nil {
ti = &lastfm.TrackInfo{
Artist: np.Artist,
Name: np.Name,
Duration: -1,
UserPlaycount: -1,
}
}
reply := []string{
fmt.Sprintf("[%s] np: %s - %s", who, ti.Artist.Name, ti.Name)}
info := []string{}
if ti.UserLoved {
info = append(info, "<3")
}
if ti.UserPlaycount > 0 {
info = append(info, fmt.Sprintf("playcount %dx", ti.UserPlaycount))
} else {
info = append(info, "first listen")
}
reply = append(reply, fmt.Sprintf("[%s]", strings.Join(info, " - ")))
tags := []string{}
for i := 0; topTags != nil && i < 5 && i < len(topTags.Tags); i++ {
tags = append(tags, topTags.Tags[i].Name)
}
reply = append(reply, fmt.Sprintf("(%s)", strings.Join(tags, ", ")))
if ti.Duration != 0 {
reply = append(reply, fmt.Sprintf("[%v]", ti.Duration))
}
r := strings.Join(reply, " ")
log.Println("Reply:", r)
irc.Privmsg(target, r)
saveCache()
return true
} else if len(recent.Tracks) > 0 && !onlyReportSuccess {
tr := recent.Tracks[0]
reply := []string{
fmt.Sprintf("[%s] last listened to %s - %s,", who, tr.Artist.Name, tr.Name)}
if (tr.Date != time.Time{}) {
diff := time.Now().Sub(tr.Date)
// round the diff to the nearest second
diff += time.Second / 2
diff -= diff % time.Second
pre := ""
years := 0
for diff > 365*24*time.Hour {
// I know it's inaccurate, it's just for simplification
diff -= 365 * 24 * time.Hour
years += 1
}
if years > 0 {
pre += fmt.Sprintf("%dy", years)
}
days := 0
for diff > 24*time.Hour {
diff -= 24 * time.Hour
days += 1
}
if days > 0 {
pre += fmt.Sprintf("%dd", days)
}
reply = append(reply, fmt.Sprintf("%s%v ago", pre, diff))
} else {
reply = append(reply, "not even last.fm knows when")
}
r := strings.Join(reply, " ")
log.Println("Reply:", r)
irc.Privmsg(target, r)
} else if !onlyReportSuccess {
r := fmt.Sprintf("[%s] never scrobbled anything", who)
log.Println("Reply:", r)
irc.Privmsg(target, r)
} else {
log.Printf("[%s] is not listening to anything\n", who)
}
saveCache()
return false
}
var sig chan os.Signal
func main() {
flag.Parse()
if *apiKey == "" {
log.Fatalln("Missing API key, provide one using -api-key")
}
lfm = lastfm.New(*apiKey)
loadNickMap()
loadCache()
cacheCleaner := time.Tick(time.Hour)
go func() {
for _ = range cacheCleaner {
cleanCache()
}
}()
if *cacheFile != "" {
cleanCache()
cacheTimer = time.NewTimer(time.Hour)
cacheTimer.Stop()
go func() {
for _ = range cacheTimer.C {
saveCacheNow()
}
}()
}
if *server == "" {
log.Fatalln("No server to connect to")
}
if *channelList == "" {
log.Fatalln("No channels to join")
}
irc := client.SimpleClient(*botNick)
irc.SSL = *useSSL
irc.Flood = false
// TODO: CA management?
irc.SSLConfig = &tls.Config{InsecureSkipVerify: true}
addNickHandlers(irc)
addWhoHandlers(irc)
irc.AddHandler(client.CONNECTED, func(irc *client.Conn, line *client.Line) {
if *nickPass != "" {
if irc.Me.Nick != *botNick {
log.Println("Nick", *botNick, "was not available; trying to retake it")
irc.Privmsg("NickServ", fmt.Sprintf("GHOST %s %s", *botNick, *nickPass))
} else {
log.Println("Identifying with NickServ")
irc.Privmsg("NickServ", fmt.Sprintf("IDENTIFY %s", *nickPass))
}
}
log.Println("Connected; joining", *channelList)
irc.Join(*channelList)
})
irc.AddHandler("NOTICE", func(irc *client.Conn, line *client.Line) {
if strings.ToLower(line.Nick) == "nickserv" {
log.Println("NickServ:", line.Args[1])
switch {
case strings.Contains(strings.ToLower(line.Args[1]), "ghost"):
log.Println("Ghost command successful")
log.Println("Changing nick to", *botNick)
irc.Nick(*botNick)
case strings.Contains(line.Args[1], "identified"),
strings.Contains(line.Args[1], "recognized"):
log.Println("Successfully identified with NickServ; joining", *channelList)
irc.Join(*channelList)
}
}
})
irc.AddHandler("QUIT", func(irc *client.Conn, line *client.Line) {
if line.Nick == *botNick {
log.Println("Nick", *botNick, "now available, changing to it")
irc.Nick(*botNick)
}
})
irc.AddHandler("NICK", func(irc *client.Conn, line *client.Line) {
if line.Args[len(line.Args)-1] == irc.Me.Nick {
log.Println("Nick successfully changed to", irc.Me.Nick)
if *nickPass != "" {
log.Println("Identifying with NickServ")
irc.Privmsg("NickServ", fmt.Sprintf("IDENTIFY %s", *nickPass))
}
}
})
irc.AddHandler("332", func(irc *client.Conn, line *client.Line) {
log.Println("Joined", line.Args[1])
})
irc.AddHandler("INVITE", onInvite)
irc.AddHandler("PRIVMSG", onPrivmsg)
quitting := false
quit := make(chan bool)
irc.AddHandler(client.DISCONNECTED, func(irc *client.Conn, line *client.Line) {
if quitting {
quit <- true
return
}
resetIdentifiedCache()
log.Println("Disconnected; waiting 10 seconds then reconnecting...")
saveCacheNow()
go func() {
time.Sleep(10 * time.Second)
log.Println("Reconnecting...")
irc.Connect(*server, *password)
}()
})
if *useSSL {
log.Println("Using SSL")
}
log.Println("Connecting to", *server)
irc.Connect(*server, *password)
sig = make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt, os.Kill, syscall.SIGTERM)
<-sig
quitting = true
log.Println("Disconnecting")
irc.Quit("Exiting")
saveCacheNow()
<-quit // wait until the QUIT is sent to server
}
func loadCache() {
if *cacheFile != "" {
if fh, err := os.Open(*cacheFile); err != nil {
log.Println("Error opening cache file:", err)
} else {
defer fh.Close()
zr, _ := zlib.NewReader(fh)
if err = lfm.Cache.Load(zr); err != nil {
log.Println("Error reading cache file:", err)
} else {
log.Println("Loaded", lfm.Cache.Len(), "cache entries")
}
zr.Close()
}
}
}
func saveCache() {
if *cacheFile != "" {
cacheTimer.Reset(10 * time.Second)
}
}
func saveCacheNow() {
if *cacheFile != "" {
if fh, err := os.Create(*cacheFile); err != nil {
log.Printf("Error creating cache file:", err)
} else {
defer fh.Close()
zw, _ := zlib.NewWriterLevel(fh, zlib.BestCompression)
if err = lfm.Cache.Store(zw); err != nil {
log.Println("Error storing cache:", err)
} else {
log.Println("Cache saved with", lfm.Cache.Len(), "entries")
}
zw.Close()
}
}
}
func cleanCache() {
// postpone the cacheTimer if it's running
if cacheTimer != nil && cacheTimer.Stop() {
cacheTimer.Reset(10 * time.Second)
}
n := lfm.Cache.Clean()
if n > 0 {
log.Println("Cache: cleaned", n, "stale entries")
}
hr := lfm.Cache.HitRate(true)
if hr == hr { // !NaN
log.Printf("Hourly cache stats: %v entries, %.1f%% hit rate\n", lfm.Cache.Len(), 100*hr)
}
lfm.Cache.ResetStats()
}