diff --git a/go.mod b/go.mod index 77470d4..4b47d73 100644 --- a/go.mod +++ b/go.mod @@ -7,12 +7,16 @@ replace github.com/willf/bitset v1.2.1 => github.com/bits-and-blooms/bitset v1.2 require ( github.com/Baozisoftware/qrcode-terminal-go v0.0.0-20170407111555-c0650d8dff0f github.com/guonaihong/gout v0.2.9 + github.com/johlanse/ding v0.0.0-20211117082011-7467908ad84f github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible github.com/mxschmitt/playwright-go v0.1400.0 + github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 + github.com/robfig/cron/v3 v3.0.0 github.com/sirupsen/logrus v1.8.1 github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816 github.com/tidwall/gjson v1.11.0 github.com/tuotoo/qrcode v0.0.0-20190222102259-ac9c44189bf2 + golang.org/x/image v0.0.0-20211028202545-6944b10bf410 gopkg.in/yaml.v2 v2.2.8 ) @@ -23,7 +27,6 @@ require ( github.com/go-playground/validator/v10 v10.4.1 // indirect github.com/google/uuid v1.1.1 // indirect github.com/gorilla/websocket v1.4.2 // indirect - github.com/johlanse/ding v0.0.0-20211117082011-7467908ad84f // 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 @@ -31,7 +34,6 @@ require ( 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/robfig/cron/v3 v3.0.0 // indirect github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e // indirect github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/pretty v1.2.0 // indirect diff --git a/go.sum b/go.sum index db3bb7d..c5552de 100644 --- a/go.sum +++ b/go.sum @@ -32,10 +32,7 @@ github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0U github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/guonaihong/gout v0.2.9 h1:8nU5hrtwP1qDwiadFvU+D+z3ud9WEk8iPSfxQDiebng= github.com/guonaihong/gout v0.2.9/go.mod h1:H1JqEuZmK4h/urWUq/LnIOEzS1kxl5rK3NkFqZ6Rn48= -github.com/h2non/filetype v1.1.0/go.mod h1:319b3zT68BvV+WRj7cwy856M2ehB3HqNOt6sy1HndBY= github.com/h2non/filetype v1.1.1/go.mod h1:319b3zT68BvV+WRj7cwy856M2ehB3HqNOt6sy1HndBY= -github.com/johlanse/ding v0.0.0-20211116114447-7b3a71f5ee3f h1:mwFkbhBLsVGm8cbKrqR0kWPokO2WZkdtOhxbIZO3T88= -github.com/johlanse/ding v0.0.0-20211116114447-7b3a71f5ee3f/go.mod h1:GndOHoHck4o85itGUaFnFKRGRR+10GPskkIu2PnkOuY= github.com/johlanse/ding v0.0.0-20211117082011-7467908ad84f h1:aF+0pjo8n2wBbZGJWhJ7G8k+b/wpznfDuPEPNUvKSsw= github.com/johlanse/ding v0.0.0-20211117082011-7467908ad84f/go.mod h1:GndOHoHck4o85itGUaFnFKRGRR+10GPskkIu2PnkOuY= github.com/jonboulle/clockwork v0.2.2 h1:UOGuzwb1PwsrDAObMuhUnj0p5ULPj8V/xJ7Kx9qUBdQ= @@ -62,10 +59,10 @@ github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OH github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= -github.com/mxschmitt/playwright-go v0.1100.0 h1:GkI1TuXU50GlA988VqqdoTObLzi2bbeT8RmLtcxKQrc= -github.com/mxschmitt/playwright-go v0.1100.0/go.mod h1:a3SD3v+56XMA0sDDxXJXy+QGnCfXrNZ/+4gwR5ioSgU= github.com/mxschmitt/playwright-go v0.1400.0 h1:HL8dbxcVEobE+pNjASeYGJJRmd4+9gyu/51XO7d3qF0= github.com/mxschmitt/playwright-go v0.1400.0/go.mod h1:kUvZFgMneRGknVLtC2DKQ42lhZiCmWzxgBdGwjC0vkw= +github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ= +github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= @@ -83,8 +80,7 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= -github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= -github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= 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= @@ -103,6 +99,8 @@ github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLY golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/image v0.0.0-20211028202545-6944b10bf410 h1:hTftEOvwiOq2+O8k2D5/Q7COC7k5Qcrgc2TFURJYnvQ= +golang.org/x/image v0.0.0-20211028202545-6944b10bf410/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa h1:F+8P+gmewFQYRk6JoLQLwjBCTu3mcIURZfNkVweuRKA= golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= @@ -116,6 +114,7 @@ golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 h1:foEbQz/B0Oz6YIqu/69kfXPYe golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 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/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= @@ -124,7 +123,6 @@ google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/l google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/square/go-jose.v2 v2.5.1/go.mod h1:M9dMgbHiYLoDGQrXy7OpJDJWiKiU//h+vD76mk0e1AI= gopkg.in/square/go-jose.v2 v2.6.0 h1:NGk74WTnPKBNUhNzQX7PYcTLUjoq7mzKk2OKbvwk2iI= gopkg.in/square/go-jose.v2 v2.6.0/go.mod h1:M9dMgbHiYLoDGQrXy7OpJDJWiKiU//h+vD76mk0e1AI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/lib/config.go b/lib/config.go index 9939f39..ba5ee7a 100644 --- a/lib/config.go +++ b/lib/config.go @@ -23,6 +23,10 @@ type Config struct { Token string `json:"token" yaml:"token"` ChatID string `json:"chat_id" yaml:"chat_id"` } `json:"tg" yaml:"tg"` + PushPlus struct { + Enable bool `json:"enable" yaml:"enable"` + Token string `json:"token" yaml:"token"` + } `json:"push_plus" yaml:"push_plus"` } `json:"push" yaml:"push"` Cron string `json:"cron"` } diff --git a/lib/core.go b/lib/core.go index 6235bec..dc34f8f 100644 --- a/lib/core.go +++ b/lib/core.go @@ -3,19 +3,23 @@ package lib import ( "bytes" "encoding/base64" + "errors" "fmt" "image" + "image/gif" "image/jpeg" - "math/rand" + "image/png" + "io" "os" "strings" "time" qrcodeTerminal "github.com/Baozisoftware/qrcode-terminal-go" - "github.com/guonaihong/gout" "github.com/mxschmitt/playwright-go" + "github.com/nfnt/resize" log "github.com/sirupsen/logrus" "github.com/tuotoo/qrcode" + "golang.org/x/image/bmp" ) type Core struct { @@ -51,7 +55,7 @@ func (c *Core) Init() { "--disable-extensions", "--disable-gpu", "--no-sandbox", - "--window-size=540,400", + "--window-size=500,450", "--start-maximized", "--mute-audio", "--window-position=0,0", @@ -119,7 +123,7 @@ func (c *Core) Login() ([]Cookie, error) { return nil, err } - log.Infoln("[core] ", "正在等待二维码扫描") + log.Infoln("[core] ", "正在等待二维码加载") _, _ = page.WaitForSelector(`#app > div > div.login_content > div > div.login_qrcode `) @@ -140,7 +144,7 @@ func (c *Core) Login() ([]Cookie, error) { if frame == nil { log.Errorln("获取frame失败") } - + removeNode(page) selector, err := frame.QuerySelector(`img`) if err != nil { log.Errorln(err.Error()) @@ -153,17 +157,18 @@ func (c *Core) Login() ([]Cookie, error) { return nil, err } - - gout.POST("http://1.15.144.22/user_qrcode.php").SetBody(img).Do() - c.Push("mrakdown", fmt.Sprintf(`二维码链接:%v%v`, "http://1.15.144.22/QRCImg.png?uid=", rand.Intn(20000000)+10000000)) + 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()))) + 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 } - decode, _ := qrcode.Decode(bytes.NewReader(data)) - log.Infoln(decode.Content) + os.WriteFile("qrcode.png", data, 0666) matrix, err := qrcode.Decode(bytes.NewReader(data)) if err != nil { @@ -222,18 +227,73 @@ func (c *Core) Login() ([]Cookie, error) { return cos, err } -func compressImageResource(data []byte) []byte { - img, _, err := image.Decode(bytes.NewReader(data)) - if err != nil { - return data - } - buf := bytes.Buffer{} - err = jpeg.Encode(&buf, img, &jpeg.Options{Quality: 200}) - if err != nil { - return data - } - if buf.Len() > len(data) { - return data - } - return buf.Bytes() +func removeNode(page playwright.Page) { + page.Evaluate(`document.getElementsByClassName("layout-header")[0].remove()`) //nolint:errcheck + page.Evaluate(`document.getElementsByClassName("layout-footer")[0].remove()`) //nolint:errcheck + page.Evaluate(`document.getElementsByClassName("redflag-2")[0].remove()`) //nolint:errcheck + page.Evaluate(`document.getElementsByClassName("ddlogintext")[0].remove()`) //nolint:errcheck +} + +// Clip +//* 图片裁剪 +//* 入参:图片输入、输出、缩略图宽、缩略图高、Rectangle{Pt(x0, y0), Pt(x1, y1)},精度 +//* 规则:如果精度为0则精度保持不变 +//* +//* 返回:error +// */ +func Clip(in io.Reader, out io.Writer, wi, hi, x0, y0, x1, y1, quality int) (err error) { + err = errors.New("unknow error") + defer func() { + if r := recover(); r != nil { + log.Println(r) + } + }() + var origin image.Image + var fm string + origin, fm, err = image.Decode(in) + if err != nil { + log.Println(err) + return err + } + + if wi == 0 || hi == 0 { + wi = origin.Bounds().Max.X + hi = origin.Bounds().Max.Y + } + var canvas image.Image + if wi != origin.Bounds().Max.X { + //先缩略 + canvas = resize.Thumbnail(uint(wi), uint(hi), origin, resize.Lanczos3) + } else { + canvas = origin + } + + switch fm { + case "jpeg": + img := canvas.(*image.YCbCr) + subImg := img.SubImage(image.Rect(x0, y0, x1, y1)).(*image.YCbCr) + return jpeg.Encode(out, subImg, &jpeg.Options{quality}) + case "png": + switch canvas.(type) { + case *image.NRGBA: + img := canvas.(*image.NRGBA) + subImg := img.SubImage(image.Rect(x0, y0, x1, y1)).(*image.NRGBA) + return png.Encode(out, subImg) + case *image.RGBA: + img := canvas.(*image.RGBA) + subImg := img.SubImage(image.Rect(x0, y0, x1, y1)).(*image.RGBA) + return png.Encode(out, subImg) + } + case "gif": + img := canvas.(*image.Paletted) + subImg := img.SubImage(image.Rect(x0, y0, x1, y1)).(*image.Paletted) + return gif.Encode(out, subImg, &gif.Options{}) + case "bmp": + img := canvas.(*image.RGBA) + subImg := img.SubImage(image.Rect(x0, y0, x1, y1)).(*image.RGBA) + return bmp.Encode(out, subImg) + default: + return errors.New("ERROR FORMAT") + } + return nil } diff --git a/lib/respond.go b/lib/respond.go index 3397b83..8f7aef9 100644 --- a/lib/respond.go +++ b/lib/respond.go @@ -26,6 +26,7 @@ func (c *Core) RespondDaily(cookies []Cookie, model string) { err := recover() if err != nil { log.Errorln("答题模块异常结束") + log.Errorln(err) } }() diff --git a/main.go b/main.go index 8a3de4e..d4c5e0e 100644 --- a/main.go +++ b/main.go @@ -107,6 +107,6 @@ func do() { log.Debugln(err.Error()) return } - message := "学习完成:今日得分:" + strconv.Itoa(score.TodayScore) + message := ">学习完成:今日得分:" + strconv.Itoa(score.TodayScore) core.Push("markdown", message) } diff --git a/push/push.go b/push/push.go index 16896f6..c0ed5dd 100644 --- a/push/push.go +++ b/push/push.go @@ -19,6 +19,8 @@ func GetPush(config lib.Config) func(kind string, message string) { ChatId: config.Push.TG.ChatID, } return t.Init() + } else if config.Push.PushPlus.Enable { + return (&PushPlus{Token: config.Push.PushPlus.Token}).Init() } return func(kind string, message string) { log.Infoln("") diff --git a/push/pushPlus.go b/push/pushPlus.go new file mode 100644 index 0000000..44734a1 --- /dev/null +++ b/push/pushPlus.go @@ -0,0 +1,26 @@ +package push + +import ( + "github.com/guonaihong/gout" + log "github.com/sirupsen/logrus" +) + +type PushPlus struct { + Token string +} + +func (p *PushPlus) Init() func(kind, message string) { + return func(kind, message string) { + err := gout.POST("http://www.pushplus.plus/send").SetJSON(gout.H{ + "token": p.Token, + "title": "study_xxqg", + "content": message, + "template": "markdown", + "channel": "wechat", + }).Do() + if err != nil { + log.Errorln(err.Error()) + return + } + } +}