fix: 修复若干bug

修复了控制台二维码错误的bug
修复了tg推送不能使用的bug
修复了当不能获取到提示信息直接异常结束的bug
优化日志输出
This commit is contained in:
johlanse 2021-11-24 22:05:37 +08:00
parent 04f02569e8
commit 7f0de82139
12 changed files with 148 additions and 74 deletions

View File

@ -53,7 +53,9 @@ push:
enable: true
token: ""
# 设置是否定时执行学习程序格式为cron格式
# "9 19 * * *" 每天19点9分执行一次
# "* 10 * * *” 每天早上十点执行一次
cron: ""
```
> 当前控制台二维码扫描可能出现二维码已过期的情况,请直接浏览扫码或者采用[pushplus](http://www.pushplus.plus/) 推送扫码

6
go.mod
View File

@ -25,22 +25,28 @@ require (
github.com/go-playground/locales v0.13.0 // indirect
github.com/go-playground/universal-translator v0.17.0 // indirect
github.com/go-playground/validator/v10 v10.4.1 // indirect
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible // indirect
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.4.0-beta.0 // indirect
github.com/google/uuid v1.1.1 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/jonboulle/clockwork v0.2.2 // indirect
github.com/leodido/go-urn v1.2.0 // indirect
github.com/lestrrat-go/strftime v1.0.5 // indirect
github.com/makiuchi-d/gozxing v0.1.1 // indirect
github.com/maruel/rs v0.0.0-20150922171536-2c81c4312fe4 // indirect
github.com/mattn/go-colorable v0.1.11 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e // indirect
github.com/technoweenie/multipartstreamer v1.0.1 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/willf/bitset v1.2.1 // indirect
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa // indirect
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/protobuf v1.26.0 // indirect
gopkg.in/square/go-jose.v2 v2.6.0 // indirect
)

12
go.sum
View File

@ -19,6 +19,10 @@ github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD87
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE=
github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4=
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible h1:2cauKuaELYAEARXRkq2LrJ0yDDv1rW7+wrTEdVL3uaU=
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible/go.mod h1:qf9acutJ8cwBUhm1bqgz6Bei9/C/c93FPDljKWwsOgM=
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.4.0-beta.0 h1:mbEDV1g6RBzKd4sFjOWuyZdxItw4CWu5Kq4KaBAJbHM=
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.4.0-beta.0/go.mod h1:5+h9c5l1Z/+Pi+5boa1Fmr4Q+FImsXYnifor92ljaVs=
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
github.com/golang/protobuf v1.5.0 h1:LUVKkCeviFUMKqHa4tXIIij/lbhnMbP7Fn5wKdKkRh4=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
@ -48,6 +52,8 @@ github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible h1:Y6sqxHMyB1D2YSzWkL
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible/go.mod h1:ZQnN8lSECaebrkQytbHj4xNgtg8CR7RYXnPok8e0EHA=
github.com/lestrrat-go/strftime v1.0.5 h1:A7H3tT8DhTz8u65w+JRpiBxM4dINQhUXAZnhBa2xeOE=
github.com/lestrrat-go/strftime v1.0.5/go.mod h1:E1nN3pCbtMSu1yjSVeyuRFVm/U0xoR76fd03sz+Qz4g=
github.com/makiuchi-d/gozxing v0.1.1 h1:xxqijhoedi+/lZlhINteGbywIrewVdVv2wl9r5O9S1I=
github.com/makiuchi-d/gozxing v0.1.1/go.mod h1:eRIHbOjX7QWxLIDJoQuMLhuXg9LAuw6znsUtRkNw9DU=
github.com/maruel/rs v0.0.0-20150922171536-2c81c4312fe4 h1:u9jwvcKbQpghIXgNl/EOL8hzhAFXh4ePrEP493W3tNA=
github.com/maruel/rs v0.0.0-20150922171536-2c81c4312fe4/go.mod h1:kcRFpEzolcEklV6rD7W95mG49/sbdX/PlFmd7ni3RvA=
github.com/mattn/go-colorable v0.1.11 h1:nQ+aFkoE2TMGc0b68U2OKSexC+eq46+XwZzWXHRmPYs=
@ -84,6 +90,8 @@ github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5Cc
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816 h1:J6v8awz+me+xeb/cUTotKgceAYouhIB3pjzgRd6IlGk=
github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816/go.mod h1:tzym/CEb5jnFI+Q0k4Qq3+LvRF4gO3E2pxS8fHP8jcA=
github.com/technoweenie/multipartstreamer v1.0.1 h1:XRztA5MXiR1TIRHxH2uNxXxaIkKQDeX7m2XsSOlQEnM=
github.com/technoweenie/multipartstreamer v1.0.1/go.mod h1:jNVxdtShOxzAsukZwTSw6MDx5eUJoiEBsSvzDU9uzog=
github.com/tidwall/gjson v1.11.0 h1:C16pk7tQNiH6VlCrtIXL1w8GaOsi1X3W8KDkE1BuYd4=
github.com/tidwall/gjson v1.11.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
@ -115,9 +123,13 @@ golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=

9
lib/answer.go Normal file
View File

@ -0,0 +1,9 @@
package lib
type T struct {
Question string `json:"question"`
Answer string `json:"answer"`
WrongAnswer string `json:"wrongAnswer"`
Option string `json:"option"`
Datetime interface{} `json:"datetime"`
}

View File

@ -34,5 +34,7 @@ push:
enable: true
token: ""
# 设置是否定时执行学习程序格式为cron格式
# "9 19 * * *" 每天19点9分执行一次
# "* 10 * * *” 每天早上十点执行一次
cron: ""

View File

@ -11,14 +11,14 @@ import (
"image/png"
"io"
"os"
"strings"
"time"
qrcodeTerminal "github.com/Baozisoftware/qrcode-terminal-go"
"github.com/makiuchi-d/gozxing"
"github.com/makiuchi-d/gozxing/qrcode"
"github.com/mxschmitt/playwright-go"
"github.com/nfnt/resize"
log "github.com/sirupsen/logrus"
"github.com/tuotoo/qrcode"
"golang.org/x/image/bmp"
)
@ -145,38 +145,17 @@ func (c *Core) Login() ([]Cookie, error) {
log.Errorln("获取frame失败")
}
removeNode(page)
selector, err := frame.QuerySelector(`img`)
if err != nil {
log.Errorln(err.Error())
return nil, err
}
img, err := selector.GetAttribute(`src`)
if err != nil {
log.Errorln(err.Error())
return nil, err
}
screen, _ := page.Screenshot()
var result []byte
buffer := bytes.NewBuffer(result)
_ = Clip(bytes.NewReader(screen), buffer, 0, 0, 525, 35, 755, 255, 0)
c.Push("markdown", fmt.Sprintf("![screenshot](%v) \n>点开查看登录二维码\n>请在五分钟内完成扫码", "data:image/png;base64,"+base64.StdEncoding.EncodeToString(buffer.Bytes())))
c.Push("image", base64.StdEncoding.EncodeToString(buffer.Bytes()))
os.WriteFile("screen.png", buffer.Bytes(), 0666)
img = strings.ReplaceAll(img, "data:image/png;base64,", "")
data, err := base64.StdEncoding.DecodeString(img)
if err != nil {
return nil, err
}
os.WriteFile("qrcode.png", data, 0666)
matrix, err := qrcode.Decode(bytes.NewReader(data))
if err != nil {
return nil, err
}
qrcodeTerminal.New().Get(matrix.Content).Print()
matrix := GetPaymentStr(bytes.NewReader(buffer.Bytes()))
qrcodeTerminal.New2(qrcodeTerminal.ConsoleColors.BrightBlack, qrcodeTerminal.ConsoleColors.BrightWhite, qrcodeTerminal.QRCodeRecoveryLevels.Low).Get(matrix.GetRawBytes()).Print()
_, err = page.WaitForNavigation(playwright.PageWaitForNavigationOptions{
Timeout: playwright.Float(30 * 1000 * 5),
URL: nil,
@ -297,3 +276,20 @@ func Clip(in io.Reader, out io.Writer, wi, hi, x0, y0, x1, y1, quality int) (err
}
return nil
}
func GetPaymentStr(fi io.Reader) (paymentCodeUrl *gozxing.Result) {
img, _, err := image.Decode(fi)
if err != nil {
fmt.Println(err)
}
// prepare BinaryBitmap
bmp, _ := gozxing.NewBinaryBitmapFromImage(img)
// decode image
qrReader := qrcode.NewQRCodeReader()
result, err := qrReader.Decode(bmp, nil)
if err != nil {
fmt.Println(err)
}
return result
}

View File

@ -26,6 +26,7 @@ func (c *Core) RespondDaily(cookies []Cookie, model string) {
err := recover()
if err != nil {
log.Errorln("答题模块异常结束")
time.Sleep(5 * time.Second)
log.Errorln(err)
}
}()
@ -214,7 +215,6 @@ func (c *Core) RespondDaily(cookies []Cookie, model string) {
content, err := page.Content()
if err != nil {
log.Errorln("获取网页全体内容失败" + err.Error())
return
}
err = openTips.Click()
@ -240,18 +240,23 @@ func (c *Core) RespondDaily(cookies []Cookie, model string) {
options, err := getOptions(page)
if err != nil {
log.Errorln("获取选项失败" + err.Error())
return
}
log.Infoln("获取到选项答案:", options)
log.Infoln("[多选题选项]", options)
var answer []string
for _, option := range options {
for _, tip := range tips {
if strings.Contains(option, tip) {
answer = append(answer, option)
if len(tips) == 0 {
log.Warnln("检测到未成功获取提示信息将选择ABCD")
answer = append(answer, options...)
} else {
for _, option := range options {
for _, tip := range tips {
if strings.Contains(option, tip) {
answer = append(answer, option)
}
}
}
log.Infoln("根据提示分别选择了", answer)
}
err = radioCheck(page, answer)
if err != nil {
@ -265,15 +270,21 @@ func (c *Core) RespondDaily(cookies []Cookie, model string) {
return
}
log.Infoln("获取到选项答案:", options)
log.Infoln("[多选题选项]", options)
var answer []string
for _, option := range options {
for _, tip := range tips {
if strings.Contains(option, tip) {
answer = append(answer, option)
if len(tips) == 0 {
log.Warnln("未能获取到提示信息将自动选择A")
answer = append(answer, options[0])
} else {
for _, option := range options {
for _, tip := range tips {
if strings.Contains(option, tip) {
answer = append(answer, option)
}
}
}
}
log.Infoln("根据提示分别选择了", answer)
err = radioCheck(page, answer)
if err != nil {
return

View File

@ -130,8 +130,9 @@ func (c *Core) LearnArticle(cookies []Cookie) {
log.Infoln("文章发布时间:" + links[n].PublishTime)
log.Infoln("文章学习链接:" + links[n].Url)
learnTime := 50 + rand.Intn(5) + 10
log.Infoln(fmt.Sprintf("正在进行阅读学习中,剩余%d篇本篇剩余时间%d秒", score.Content["article"].MaxScore-score.Content["article"].CurrentScore, learnTime))
for i := 0; i < learnTime; i++ {
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 {
go func() {
_, err = page.Evaluate(fmt.Sprintf(`let h = document.body.scrollHeight/120*%d;document.documentElement.scrollTop=h;`, i))
@ -142,6 +143,7 @@ func (c *Core) LearnArticle(cookies []Cookie) {
}
time.Sleep(1 * time.Second)
}
fmt.Println()
if score.Content["article"].CurrentScore >= score.Content["article"].MaxScore {
log.Infoln("检测到本次阅读学习分数已满,退出学习")
@ -223,8 +225,9 @@ func (c *Core) LearnVideo(cookies []Cookie) {
log.Infoln("视频发布时间:" + links[n].PublishTime)
log.Infoln("视频学习链接:" + links[n].Url)
learnTime := 50 + rand.Intn(5) + 10
log.Infoln(fmt.Sprintf("正在进行视频学习中,剩余%d个当前剩余时间%d秒", score.Content["video"].MaxScore-score.Content["video"].CurrentScore, learnTime))
for i := 0; i < learnTime; i++ {
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 {
go func() {
_, err := page.Evaluate(fmt.Sprintf(`let h = document.body.scrollHeight/120*%d;document.documentElement.scrollTop=h;`, i))
@ -235,7 +238,7 @@ func (c *Core) LearnVideo(cookies []Cookie) {
}
time.Sleep(1 * time.Second)
}
fmt.Println()
if score.Content["video"].CurrentScore >= score.Content["video"].MaxScore || score.Content["video_time"].CurrentScore >= score.Content["video_time"].MaxScore {
log.Infoln("检测到本次视频学习分数已满,退出学习")
break

12
main.go
View File

@ -33,7 +33,6 @@ func init() {
level, err := log.ParseLevel(config.LogLevel)
log.SetLevel(level)
log.Info(config)
}
var (
@ -52,12 +51,21 @@ func main() {
if config.Cron != "" {
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 {}
}
do()
}
@ -107,6 +115,6 @@ func do() {
log.Debugln(err.Error())
return
}
message := ">学习完成:今日得分:" + strconv.Itoa(score.TodayScore)
message := "学习完成:今日得分:" + strconv.Itoa(score.TodayScore)
core.Push("markdown", message)
}

View File

@ -12,14 +12,17 @@ func GetPush(config lib.Config) func(kind string, message string) {
Secret: config.Push.Ding.Secret,
Token: config.Push.Ding.AccessToken,
}
log.Infoln("已配置钉钉推送")
return ding.Send()
} else if config.Push.TG.Enable {
t := &Telegram{
Token: config.Push.TG.Token,
ChatId: config.Push.TG.ChatID,
}
log.Infoln("已配置telegram推送")
return t.Init()
} else if config.Push.PushPlus.Enable {
log.Infoln("已配置pushplus推送")
return (&PushPlus{Token: config.Push.PushPlus.Token}).Init()
}
return func(kind string, message string) {

View File

@ -11,6 +11,9 @@ type PushPlus struct {
func (p *PushPlus) Init() func(kind, message string) {
return func(kind, message string) {
if kind == "image" {
return
}
err := gout.POST("http://www.pushplus.plus/send").SetJSON(gout.H{
"token": p.Token,
"title": "study_xxqg",

View File

@ -1,56 +1,75 @@
package push
import (
"fmt"
"encoding/base64"
"net/http"
"net/url"
"strconv"
"github.com/guonaihong/gout"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
)
//Telegram
// @Description:
//
type Telegram struct {
Token string
ChatId string
}
//TGMsg
// @Description:
//
type TGMsg struct {
ChatID string `json:"chat_id"`
Text string `json:"text"`
ParseMode string `json:"parse_mode"`
}
//Init
/**
* @Description:
* @receiver t
* @return func(kind string, message string)
*/
func (t *Telegram) Init() func(kind string, message string) {
uri, err := url.Parse("http://127.0.0.1:7890")
bot, err := tgbotapi.NewBotAPIWithClient(t.Token, tgbotapi.APIEndpoint, &http.Client{Transport: &http.Transport{
// 设置代理
Proxy: http.ProxyURL(uri),
}})
if err != nil {
log.Errorln("telegram token鉴权失败")
return func(kind string, message string) {}
}
chatId, err := strconv.ParseInt(t.ChatId, 10, 64)
if err != nil {
return func(kind string, message string) {}
}
return func(kind string, message string) {
var resp []byte
if kind == "markdown" {
data := TGMsg{
ChatID: t.ChatId,
Text: message,
ParseMode: "MarkdownV2",
}
log.Infoln(data)
err := gout.GET(fmt.Sprintf("https://api.telegram.org/bot%v/sendMessage", t.Token)).BindBody(&resp).SetQuery(data).SetProxy("http://127.0.0.1:7890").Do()
if kind == "image" {
bytes, _ := base64.StdEncoding.DecodeString(message)
photo := tgbotapi.NewPhoto(chatId, tgbotapi.FileBytes{
Name: "123",
Bytes: bytes,
})
_, err := bot.Send(photo)
if err != nil {
log.Errorln("发送图片信息失败")
log.Errorln(err.Error())
return
}
log.Infoln("向tg推送消息成功")
} else if kind == "html" {
data := TGMsg{
ChatID: t.ChatId,
Text: message,
ParseMode: "HTML",
}
log.Infoln(data)
err := gout.POST(fmt.Sprintf("https://api.telegram.org/bot%v/sendMessage", t.Token)).BindBody(&resp).SetProxy("http://127.0.0.1:7890").SetJSON(gout.H{
"chat_id": t.ChatId,
"text": message,
"parse_mode": "HTML",
}).Do()
if err != nil {
return
}
log.Infoln("向tg推送消息成功")
}
log.Infoln(gjson.GetBytes(resp, "@this|@pretty").String())
mess := tgbotapi.NewMessage(chatId, message)
mess.ParseMode = tgbotapi.ModeMarkdownV2
_, err := bot.Send(mess)
if err != nil {
log.Errorln("发送消息失败")
log.Errorln(err.Error())
return
}
}
}