forked from yhy618618/study_xxqg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
204 lines (186 loc) · 4.32 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
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
package main
import (
"fmt"
"io"
"os"
"path"
"strconv"
"time"
rotates "github.com/lestrrat-go/file-rotatelogs"
"github.com/robfig/cron/v3"
log "github.com/sirupsen/logrus"
easy "github.com/t-tomalak/logrus-easy-formatter"
"github.com/huoxue1/study_xxqg/lib"
"github.com/huoxue1/study_xxqg/model"
"github.com/huoxue1/study_xxqg/push"
"github.com/huoxue1/study_xxqg/web"
)
var VERSION = "unknown"
func init() {
config = lib.GetConfig()
logFormatter := &easy.Formatter{
TimestampFormat: "2006-01-02 15:04:05",
LogFormat: "[%time%] [%lvl%]: %msg% \n",
}
w, err := rotates.New(path.Join("logs", "%Y-%m-%d.log"), rotates.WithRotationTime(time.Hour*24))
if err != nil {
log.Errorf("rotates init err: %v", err)
panic(err)
}
log.SetOutput(io.MultiWriter(w, os.Stdout))
log.SetFormatter(logFormatter)
level, err := log.ParseLevel(config.LogLevel)
if err != nil {
log.SetLevel(log.DebugLevel)
}
log.SetLevel(level)
}
func init() {
pid := os.Getpid()
pi := strconv.Itoa(pid)
err := os.WriteFile("pid.pid", []byte(pi), 0666)
if err != nil {
log.Errorln("pid写入失败")
return
}
}
var (
config lib.Config
)
func init() {
_, err := os.Stat(`./config/`)
if err != nil {
os.Mkdir("./config/", 0666) //nolint:errcheck
return
}
}
func main() {
if config.Web.Enable {
engine := web.RouterInit()
go func() {
err := engine.Run(fmt.Sprintf("%s:%d", config.Web.Host, config.Web.Port))
if err != nil {
return
}
}()
}
if config.StartWait > 0 {
log.Infoln(fmt.Sprintf("将等待%d秒后启动程序", config.StartWait))
time.Sleep(time.Second * time.Duration(config.StartWait))
}
if config.Cron != "" {
go func() {
defer func() {
err := recover()
if err != nil {
log.Errorln("定时任务执行出现问题")
log.Errorln(err)
}
}()
log.Infoln("已采用定时执行模式")
c := cron.New()
_, err := c.AddFunc(config.Cron, func() {
defer func() {
i := recover()
if i != nil {
log.Errorln(i)
log.Errorln("执行定时任务出现异常")
}
}()
do()
})
if err != nil {
log.Errorln(err.Error())
return
}
c.Start()
select {}
}()
}
if config.TG.Enable {
go func() {
defer func() {
err := recover()
if err != nil {
log.Errorln("TG模式执行出现问题")
log.Errorln(err)
}
}()
log.Infoln("已采用tg交互模式")
telegram := lib.Telegram{
Token: config.TG.Token,
ChatId: config.TG.ChatID,
Proxy: config.TG.Proxy,
}
telegram.Init()
select {}
}()
}
if !config.TG.Enable && config.Cron == "" {
log.Infoln("已采用普通学习模式")
do()
} else {
select {}
}
}
func do() {
defer func() {
err := recover()
if err != nil {
log.Errorln("do 方法执行错误")
log.Errorln(err)
}
}()
log.Infoln(` 刷课模式,默认为1,
1:只刷文章何视频
2:只刷文章和视频和每日答题
3:刷文章和视频和每日答题每周答题和专项答题`)
log.Infoln("检测到模式", config.Model)
getPush := push.GetPush(config)
core := lib.Core{ShowBrowser: config.ShowBrowser, Push: getPush}
defer core.Quit()
core.Init()
var user *model.User
users, _ := model.Query()
switch {
case len(users) < 1:
log.Infoln("未检测到有效用户信息,将采用登录模式")
u, err := core.L(config.Retry.Times)
if err != nil {
log.Errorln(err.Error())
return
}
user = u
case len(users) == 1:
log.Infoln("检测到1位有效用户信息,采用默认用户")
user = users[0]
log.Infoln("已选择用户: ", users[0].Nick)
default:
for i, user := range users {
log.Infoln("序号:", i+1, " ===> ", user.Nick)
}
log.Infoln("请输入对应序号选择对应账户")
var i int
_, _ = fmt.Scanln(&i)
user = users[i-1]
log.Infoln("已选择用户: ", users[i-1].Nick)
}
go core.LearnArticle(user)
go core.LearnVideo(user)
lib.WaitStudy(user, "")
if config.Model == 2 {
core.RespondDaily(user, "daily")
} else if config.Model == 3 {
core.RespondDaily(user, "daily")
core.RespondDaily(user, "weekly")
core.RespondDaily(user, "special")
}
score, err := lib.GetUserScore(user.ToCookies())
if err != nil {
log.Errorln("获取成绩失败")
log.Debugln(err.Error())
return
}
message := "学习完成:今日得分:" + strconv.Itoa(score.TodayScore)
core.Push("markdown", message)
}