diff --git a/lib/core.go b/lib/core.go index 4433b42..13d88a6 100644 --- a/lib/core.go +++ b/lib/core.go @@ -106,6 +106,10 @@ func (c *Core) Quit() { } } +func (c *Core) IsQuit() bool { + return !c.browser.IsConnected() +} + func (c *Core) Login() ([]Cookie, error) { defer func() { i := recover() @@ -161,6 +165,8 @@ func (c *Core) Login() ([]Cookie, error) { c.Push("image", base64.StdEncoding.EncodeToString(buffer.Bytes())) os.WriteFile("screen.png", buffer.Bytes(), 0666) matrix := GetPaymentStr(bytes.NewReader(buffer.Bytes())) + c.Push("text", "https://techxuexi.js.org/jump/techxuexi-20211023.html?"+matrix.GetText()) + c.Push("text", matrix.GetText()) qrcodeTerminal.New2(qrcodeTerminal.ConsoleColors.BrightBlack, qrcodeTerminal.ConsoleColors.BrightWhite, qrcodeTerminal.QRCodeRecoveryLevels.Low).Get(matrix.GetRawBytes()).Print() _, err = page.WaitForNavigation(playwright.PageWaitForNavigationOptions{ diff --git a/lib/respond.go b/lib/respond.go index cd07ea8..bb3a5f6 100644 --- a/lib/respond.go +++ b/lib/respond.go @@ -26,11 +26,14 @@ func (c *Core) RespondDaily(cookies []Cookie, model string) { err := recover() if err != nil { log.Errorln("答题模块异常结束或答题已完成") + c.Push("text", "答题模块异常退出或答题已完成") time.Sleep(5 * time.Second) log.Errorln(err) } }() - + if c.IsQuit() { + return + } // 获取用户成绩 score, err := GetUserScore(cookies) if err != nil { @@ -78,6 +81,7 @@ func (c *Core) RespondDaily(cookies []Cookie, model string) { return } + c.Push("text", "已加载每日答题模块") } case "weekly": { @@ -93,6 +97,7 @@ func (c *Core) RespondDaily(cookies []Cookie, model string) { return } + c.Push("text", "已加载每周答题模块") } case "special": { @@ -108,12 +113,16 @@ func (c *Core) RespondDaily(cookies []Cookie, model string) { return } + c.Push("text", "已加载专项答题模块") } } time.Sleep(5 * time.Second) getAnswerPage(page, model) for true { + if c.IsQuit() { + return + } // 查看是否存在答题按钮,若按钮可用则重新提交答题 btn, err := page.QuerySelector(`#app > div > div.layout-body > div > div.detail-body > div.action-row > button`) if err != nil { diff --git a/lib/study.go b/lib/study.go index 44d3655..a60a2d7 100644 --- a/lib/study.go +++ b/lib/study.go @@ -91,8 +91,12 @@ func (c *Core) LearnArticle(cookies []Cookie) { err := recover() if err != nil { log.Errorln("文章学习模块异常结束") + log.Errorln(err) } }() + if c.IsQuit() { + return + } score, err := GetUserScore(cookies) if err != nil { @@ -127,10 +131,14 @@ func (c *Core) LearnArticle(cookies []Cookie) { log.Errorln("页面跳转失败") } log.Infoln("正在学习文章:" + links[n].Title) + c.Push("text", "正在学习文章:"+links[n].Title) log.Infoln("文章发布时间:" + links[n].PublishTime) log.Infoln("文章学习链接:" + links[n].Url) learnTime := 50 + rand.Intn(5) + 10 for i := 0; i < learnTime; i++ { + if c.IsQuit() { + return + } fmt.Printf("\r[%v] [INFO]: 正在进行阅读学习中,剩余%d篇,本篇剩余时间%d秒", time.Now().Format("2006-01-02 15:04:05"), score.Content["article"].MaxScore-score.Content["article"].CurrentScore, learnTime-i) if rand.Float32() > 0.5 { @@ -165,8 +173,12 @@ func (c *Core) LearnVideo(cookies []Cookie) { err := recover() if err != nil { log.Errorln("视频学习模块异常结束") + log.Errorln(err) } }() + if c.IsQuit() { + return + } score, err := GetUserScore(cookies) if err != nil { log.Errorln(err.Error()) @@ -215,10 +227,14 @@ func (c *Core) LearnVideo(cookies []Cookie) { log.Errorln("页面跳转失败") } log.Infoln("正在观看视频:" + links[n].Title) + c.Push("text", "正在观看视频:"+links[n].Title) log.Infoln("视频发布时间:" + links[n].PublishTime) log.Infoln("视频学习链接:" + links[n].Url) learnTime := 50 + rand.Intn(5) + 10 for i := 0; i < learnTime; i++ { + if c.IsQuit() { + return + } fmt.Printf("\r[%v] [INFO]: 正在进行视频学习中,剩余%d个,当前剩余时间%d秒", time.Now().Format("2006-01-02 15:04:05"), score.Content["video"].MaxScore-score.Content["video"].CurrentScore, learnTime-i) if rand.Float32() > 0.5 { diff --git a/lib/tg.go b/lib/tg.go index f882f4e..a6f0786 100644 --- a/lib/tg.go +++ b/lib/tg.go @@ -10,11 +10,13 @@ import ( "sync" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" + "github.com/google/uuid" log "github.com/sirupsen/logrus" ) var ( handles sync.Map + datas sync.Map ) func init() { @@ -22,6 +24,7 @@ func init() { newPlugin("/get_users", getAllUser) newPlugin("/study", study) newPlugin("/get_scores", getScores) + newPlugin("/quit", quit) } //Telegram @@ -101,6 +104,7 @@ func (t *Telegram) Init() { tgbotapi.BotCommand{Command: "get_users", Description: "获取所有cookie有效的用户"}, tgbotapi.BotCommand{Command: "study", Description: "对一个账户进行学习"}, tgbotapi.BotCommand{Command: "get_scores", Description: "获取用户成绩"}, + tgbotapi.BotCommand{Command: "quit", Description: "退出所有正在学习的实例"}, )) if err != nil { return @@ -223,6 +227,11 @@ func study(bot *Telegram, args []string) { } }, } + + u := uuid.New().String() + bot.SendMsg("已创建运行实例:" + u) + datas.Store(u, &core) + defer datas.Delete(u) core.Init() defer core.Quit() core.LearnArticle(cookies) @@ -231,6 +240,8 @@ func study(bot *Telegram, args []string) { core.RespondDaily(cookies, "daily") core.RespondDaily(cookies, "weekly") core.RespondDaily(cookies, "special") + score, _ := GetUserScore(cookies) + bot.SendMsg(fmt.Sprintf("当前学习总积分:%v,今日得分:%v", score.TotalScore, score.TodayScore)) } func getScores(bot *Telegram, args []string) { @@ -247,7 +258,18 @@ func getScores(bot *Telegram, args []string) { if err != nil { message += err.Error() + "\n" } - message += PrintScore(score) + "\n" + message += foramet_score(score) + "\n" } bot.SendMsg(message) } + +func quit(bot *Telegram, args []string) { + if len(args) < 1 { + datas.Range(func(key, value interface{}) bool { + bot.SendMsg("已退出运行实例" + key.(string)) + core := value.(*Core) + core.Quit() + return true + }) + } +}