forked from ThreeCatsLoveFish/MedalHelper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
93 lines (84 loc) · 1.73 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"MedalHelper/service"
"MedalHelper/service/push"
"MedalHelper/util"
"fmt"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/robfig/cron"
)
func init() {
// Init config file
util.InitConfig()
push.InitPush()
}
func usage() {
fmt.Print(`Usage: main.go [COMMAND]
COMMAND:
login Login bili account and get access key
start Execute all tasks immediately
`)
}
func initUsers() []service.User {
users := make([]service.User, 0, 1)
for _, userInfo := range util.GlobalConfig.UserList {
if len(userInfo.AccessKey) == 0 {
continue
}
banId := make([]int, 0)
if userInfo.BannedUid != "" {
banIdStr := strings.Split(userInfo.BannedUid, ",")
for _, str := range banIdStr {
id, err := strconv.ParseInt(str, 10, 64)
if err != nil {
continue
}
banId = append(banId, int(id))
}
}
users = append(users, service.NewUser(userInfo.AccessKey, userInfo.PushName, banId))
}
return users
}
func exec() {
users := initUsers()
wg := sync.WaitGroup{}
for _, user := range users {
if status := user.Init(); status {
wg.Add(1)
go user.Start(&wg)
}
}
wg.Wait()
util.Info(" 今日任务已完成")
}
func main() {
// Tool for login
args := os.Args
if len(args) > 1 {
if args[1] == "login" {
util.LoginBili()
} else if args[1] == "start" {
exec()
} else {
usage()
}
return
}
// Start main block
if len(util.GlobalConfig.Cron) == 0 {
util.Info(" 外部调用,开启任务")
exec()
} else {
c := cron.New()
c.AddFunc(util.GlobalConfig.Cron, exec)
entry := c.Entries()
timeNext := entry[0].Schedule.Next(time.Now()).Format(time.RFC3339)
util.Info(" 使用内置定时器,开启定时任务,下次执行时间为%s", timeNext)
c.Run()
}
}