study_xxqg/main.go

284 lines
5.8 KiB
Go
Raw Normal View History

2021-11-12 07:46:33 +00:00
package main
import (
2022-05-27 13:37:16 +00:00
"flag"
"fmt"
"io"
2021-11-12 07:46:33 +00:00
"os"
"path"
"strconv"
2021-11-12 07:46:33 +00:00
"time"
rotates "github.com/lestrrat-go/file-rotatelogs"
"github.com/robfig/cron/v3"
2021-11-12 07:46:33 +00:00
log "github.com/sirupsen/logrus"
easy "github.com/t-tomalak/logrus-easy-formatter"
2022-07-04 03:27:57 +00:00
//"github.com/huoxue1/study_xxqg/gui"
2021-11-12 07:46:33 +00:00
"github.com/huoxue1/study_xxqg/lib"
"github.com/huoxue1/study_xxqg/model"
"github.com/huoxue1/study_xxqg/push"
2022-05-27 13:37:16 +00:00
"github.com/huoxue1/study_xxqg/utils/update"
2022-04-20 13:31:46 +00:00
"github.com/huoxue1/study_xxqg/web"
2021-11-12 07:46:33 +00:00
)
2022-05-27 13:37:16 +00:00
var (
u bool
2022-07-04 03:27:57 +00:00
i bool
2022-05-27 13:37:16 +00:00
)
2022-02-11 10:13:50 +00:00
var VERSION = "unknown"
2021-11-12 07:46:33 +00:00
func init() {
2022-05-27 13:37:16 +00:00
flag.BoolVar(&u, "u", false, "update the study_xxqg")
2022-07-04 03:27:57 +00:00
flag.BoolVar(&i, "init", false, "init the app")
2022-05-27 13:37:16 +00:00
flag.Parse()
2021-11-12 07:46:33 +00:00
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))
2021-11-12 07:46:33 +00:00
log.SetFormatter(logFormatter)
level, err := log.ParseLevel(config.LogLevel)
2022-05-02 12:30:49 +00:00
if err != nil {
log.SetLevel(log.DebugLevel)
}
2021-11-12 07:46:33 +00:00
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
}
}
2021-11-12 07:46:33 +00:00
var (
config lib.Config
)
func init() {
_, err := os.Stat(`./config/`)
if err != nil {
2022-05-02 12:30:49 +00:00
os.Mkdir("./config/", 0666) //nolint:errcheck
2021-11-12 07:46:33 +00:00
return
}
}
func main() {
2022-07-04 03:27:57 +00:00
if i {
core := &lib.Core{}
core.Init()
core.Quit()
return
}
2022-05-27 13:37:16 +00:00
go update.CheckUpdate(VERSION)
if u {
update.SelfUpdate("", VERSION)
log.Infoln("请重启应用")
os.Exit(1)
}
2022-04-20 13:31:46 +00:00
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
}
}()
}
2022-04-14 13:18:36 +00:00
if config.StartWait > 0 {
log.Infoln(fmt.Sprintf("将等待%d秒后启动程序", config.StartWait))
time.Sleep(time.Second * time.Duration(config.StartWait))
}
2022-05-02 12:30:49 +00:00
if config.Cron != "" {
go func() {
defer func() {
2022-05-02 12:30:49 +00:00
err := recover()
if err != nil {
log.Errorln("定时任务执行出现问题")
log.Errorln(err)
}
}()
2022-05-02 12:30:49 +00:00
log.Infoln("已采用定时执行模式")
c := cron.New()
_, err := c.AddFunc(config.Cron, func() {
defer func() {
i := recover()
if i != nil {
log.Errorln(i)
log.Errorln("执行定时任务出现异常")
}
}()
do("cron")
2022-05-02 12:30:49 +00:00
})
if err != nil {
log.Errorln(err.Error())
return
}
c.Start()
}()
}
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()
}()
}
if !config.TG.Enable && config.Cron == "" {
2021-11-27 08:24:24 +00:00
log.Infoln("已采用普通学习模式")
do("normal")
} else {
2022-07-04 03:27:57 +00:00
//gui.InitWindow()
select {}
}
}
func do(m string) {
2022-07-24 07:56:12 +00:00
2022-04-25 10:39:54 +00:00
defer func() {
err := recover()
if err != nil {
log.Errorln("do 方法执行错误")
log.Errorln(err)
}
}()
2021-11-27 08:24:24 +00:00
log.Infoln(` 刷课模式默认为1
2021-11-12 07:46:33 +00:00
1只刷文章何视频
2只刷文章和视频和每日答题
3刷文章和视频和每日答题每周答题和专项答题`)
log.Infoln("检测到模式", config.Model)
2021-11-27 08:24:24 +00:00
getPush := push.GetPush(config)
core := &lib.Core{ShowBrowser: config.ShowBrowser, Push: getPush}
2021-11-12 07:46:33 +00:00
defer core.Quit()
core.Init()
var user *model.User
users, _ := model.Query()
study := func(core2 *lib.Core, u *model.User) {
go core2.LearnArticle(u)
go core2.LearnVideo(u)
lib.WaitStudy(u, "")
if config.Model == 2 {
core2.RespondDaily(u, "daily")
} else if config.Model == 3 {
core2.RespondDaily(u, "daily")
core2.RespondDaily(u, "weekly")
core2.RespondDaily(u, "special")
}
score, err := lib.GetUserScore(u.ToCookies())
if err != nil {
log.Errorln("获取成绩失败")
log.Debugln(err.Error())
return
}
message := u.Nick + " 学习完成:今日得分:" + strconv.Itoa(score.TodayScore)
2022-07-24 07:56:12 +00:00
score, _ = lib.GetUserScore(user.ToCookies())
content := lib.FormatScore(score)
err = push.PushMessage(user.Nick+"学习情况", content, "score", user.PushId)
if err != nil {
log.Errorln(err.Error())
err = nil
}
core2.Push("markdown", message)
core2.Push("flush", "")
}
// 用户小于1时自动登录
if len(users) < 1 {
log.Infoln("未检测到有效用户信息,将采用登录模式")
u, err := core.L(config.Retry.Times)
2022-04-25 10:39:54 +00:00
if err != nil {
log.Errorln(err.Error())
return
}
user = u
} else {
// 如果为定时模式则直接循环所以用户依次运行
if m == "cron" {
for _, u := range users {
study(core, u)
}
if len(users) < 1 {
user, err := core.L(config.Retry.Times)
if err != nil {
core.Push("msg", "登录超时")
return
}
study(core, user)
}
return
}
for i, user := range users {
log.Infoln("序号:", i+1, " ===> ", user.Nick)
}
log.Infoln("请输入对应序号选择对应账户输入0添加用户")
inputChan := make(chan int, 1)
go func(c chan int) {
var i int
_, _ = fmt.Scanln(&i)
c <- i
}(inputChan)
var i int
select {
case i = <-inputChan:
log.Infoln("已获取到输入")
case <-time.After(time.Minute):
log.Errorln("获取输入超时,默认选择第一个用户")
if len(users) < 1 {
return
} else {
i = 1
}
}
if i == 0 {
u, err := core.L(config.Retry.Times)
if err != nil {
log.Errorln(err.Error())
return
}
user = u
} else {
user = users[i-1]
log.Infoln("已选择用户: ", users[i-1].Nick)
}
2021-11-12 07:46:33 +00:00
}
study(core, user)
core.Push("flush", "")
2021-11-12 07:46:33 +00:00
}