/
main.go
75 lines (63 loc) · 1.28 KB
/
main.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
package main
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"time"
)
func main() {
// Read and set config.
err := setConfig()
if err != nil {
fmt.Println("config -", err)
return
}
// Init discord client.
err = initDiscord()
if err != nil {
fmt.Println("discord -", err)
return
}
defer discord.Close()
// Start Nyaa RSS checker and listening discord bot.
startScheduler()
}
// startScheduler to start running scheduler.
func startScheduler() {
// Add job.
worker := newScheduler()
worker.add(context.Background(), feedJob, time.Duration(cfg.Interval)*time.Minute)
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-quit
// Clean everything.
worker.stop()
discord.Close()
}
// feedJob is scheduler job to notify each user.
func feedJob(ctx context.Context) {
users, err := getSubsUser()
if err != nil {
fmt.Println(err)
return
}
for _, user := range users {
// Check user's feed.
checkFeed(user)
// Sleep to prevent spamming.
time.Sleep(time.Second)
}
}
// checkFeed to check feeds update and send if there is new update.
func checkFeed(user User) {
feeds, err := getCleanFeed(user)
if err != nil {
fmt.Println(err)
return
}
if len(feeds) > 0 {
sendFeed(feeds, user)
}
}