feat: 实现网页浏览功能
This commit is contained in:
parent
115d83ff28
commit
d084a62815
10
README.md
10
README.md
@ -1,2 +1,12 @@
|
||||
# qq_bot
|
||||
|
||||
# build
|
||||
```shell
|
||||
go mod tidy
|
||||
go build
|
||||
```
|
||||
|
||||
# run
|
||||
```shell
|
||||
./qq_bot
|
||||
```
|
||||
|
9
go.mod
9
go.mod
@ -11,6 +11,15 @@ require (
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/chromedp/cdproto v0.0.0-20241003230502-a4a8f7c660df // indirect
|
||||
github.com/chromedp/chromedp v0.10.0 // indirect
|
||||
github.com/chromedp/sysutil v1.0.0 // indirect
|
||||
github.com/gobwas/httphead v0.1.0 // indirect
|
||||
github.com/gobwas/pool v0.2.1 // indirect
|
||||
github.com/gobwas/ws v1.4.0 // indirect
|
||||
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
|
||||
github.com/josharian/intern v1.0.0 // indirect
|
||||
github.com/mailru/easyjson v0.7.7 // indirect
|
||||
golang.org/x/image v0.21.0 // indirect
|
||||
golang.org/x/sys v0.26.0 // indirect
|
||||
)
|
||||
|
23
go.sum
23
go.sum
@ -1,21 +1,44 @@
|
||||
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
|
||||
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
|
||||
github.com/chromedp/cdproto v0.0.0-20240801214329-3f85d328b335/go.mod h1:GKljq0VrfU4D5yc+2qA6OVr8pmO/MBbPEWqWQ/oqGEs=
|
||||
github.com/chromedp/cdproto v0.0.0-20241003230502-a4a8f7c660df h1:cbtSn19AtqQha1cxmP2Qvgd3fFMz51AeAEKLJMyEUhc=
|
||||
github.com/chromedp/cdproto v0.0.0-20241003230502-a4a8f7c660df/go.mod h1:GKljq0VrfU4D5yc+2qA6OVr8pmO/MBbPEWqWQ/oqGEs=
|
||||
github.com/chromedp/chromedp v0.10.0 h1:bRclRYVpMm/UVD76+1HcRW9eV3l58rFfy7AdBvKab1E=
|
||||
github.com/chromedp/chromedp v0.10.0/go.mod h1:ei/1ncZIqXX1YnAYDkxhD4gzBgavMEUu7JCKvztdomE=
|
||||
github.com/chromedp/sysutil v1.0.0 h1:+ZxhTpfpZlmchB58ih/LBHX52ky7w2VhQVKQMucy3Ic=
|
||||
github.com/chromedp/sysutil v1.0.0/go.mod h1:kgWmDdq8fTzXYcKIBqIYvRRTnYb9aNS9moAV0xufSww=
|
||||
github.com/fogleman/gg v1.3.0 h1:/7zJX8F6AaYQc57WQCyN9cAIz+4bCJGO9B+dyW29am8=
|
||||
github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
|
||||
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
|
||||
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
|
||||
github.com/gobwas/httphead v0.1.0 h1:exrUm0f4YX0L7EBwZHuCF4GDp8aJfVeBrlLQrs6NqWU=
|
||||
github.com/gobwas/httphead v0.1.0/go.mod h1:O/RXo79gxV8G+RqlR/otEwx4Q36zl9rqC5u12GKvMCM=
|
||||
github.com/gobwas/pool v0.2.1 h1:xfeeEhW7pwmX8nuLVlqbzVc7udMDrwetjEv+TZIz1og=
|
||||
github.com/gobwas/pool v0.2.1/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw=
|
||||
github.com/gobwas/ws v1.4.0 h1:CTaoG1tojrh4ucGPcoJFiAQUAsEWekEWvLy7GsVNqGs=
|
||||
github.com/gobwas/ws v1.4.0/go.mod h1:G3gNqMNtPppf5XUz7O4shetPpcZ1VJ7zt18dlUeakrc=
|
||||
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
|
||||
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
|
||||
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
||||
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/jmoiron/sqlx v1.4.0 h1:1PLqN7S1UYp5t4SrVVnt4nUVNemrDAtxlulVe+Qgm3o=
|
||||
github.com/jmoiron/sqlx v1.4.0/go.mod h1:ZrZ7UsYB/weZdl2Bxg6jCRO9c3YHl8r3ahlKmRT4JLY=
|
||||
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
|
||||
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
|
||||
github.com/ledongthuc/pdf v0.0.0-20220302134840-0c2507a12d80/go.mod h1:imJHygn/1yfhB7XSJJKlFZKl/J+dCPAknuiaGOshXAs=
|
||||
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
|
||||
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
|
||||
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
|
||||
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
|
||||
github.com/mattn/go-sqlite3 v1.14.23 h1:gbShiuAP1W5j9UOksQ06aiiqPMxYecovVGwmTxWtuw0=
|
||||
github.com/mattn/go-sqlite3 v1.14.23/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
|
||||
github.com/orisano/pixelmatch v0.0.0-20220722002657-fb0b55479cde/go.mod h1:nZgzbfBr3hhjoZnS66nKrHmduYNpc34ny7RK4z5/HM0=
|
||||
github.com/sashabaranov/go-openai v1.30.3 h1:TEdRP3otRXX2A7vLoU+kI5XpoSo7VUUlM/rEttUqgek=
|
||||
github.com/sashabaranov/go-openai v1.30.3/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
|
||||
golang.org/x/image v0.21.0 h1:c5qV36ajHpdj4Qi0GnE0jUc/yuo33OLFaa0d+crTD5s=
|
||||
golang.org/x/image v0.21.0/go.mod h1:vUbsLavqK/W303ZroQQVKQ+Af3Yl6Uz1Ppu5J/cLz78=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
|
||||
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
|
30
handler/getweb/getweb.go
Normal file
30
handler/getweb/getweb.go
Normal file
@ -0,0 +1,30 @@
|
||||
package getweb
|
||||
|
||||
import (
|
||||
"git.lxtend.com/qqbot/handler"
|
||||
"git.lxtend.com/qqbot/model"
|
||||
"git.lxtend.com/qqbot/util"
|
||||
)
|
||||
|
||||
func init() {
|
||||
handler.RegisterHandler("getweb", getweb)
|
||||
}
|
||||
|
||||
func getweb(msg model.Message) (reply model.Reply) {
|
||||
if len(msg.Msg) <= len("getweb ") {
|
||||
return model.Reply{}
|
||||
}
|
||||
url := msg.Msg[len("getweb "):]
|
||||
if err := util.ScreenshotURL(url, "./tmp/getweb/url.png", 1920, 1080, 0, 0, 0, 0, ""); err != nil {
|
||||
return model.Reply{
|
||||
ReplyMsg: err.Error(),
|
||||
ReferOriginMsg: true,
|
||||
FromMsg: msg,
|
||||
}
|
||||
}
|
||||
return model.Reply{
|
||||
ReplyMsg: "[CQ:image,file=file:///root/qqbot/tmp/getweb/url.png]",
|
||||
ReferOriginMsg: true,
|
||||
FromMsg: msg,
|
||||
}
|
||||
}
|
@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
_ "git.lxtend.com/qqbot/handler/echo"
|
||||
_ "git.lxtend.com/qqbot/handler/getweb"
|
||||
_ "git.lxtend.com/qqbot/handler/headmaster"
|
||||
_ "git.lxtend.com/qqbot/handler/jrrp"
|
||||
_ "git.lxtend.com/qqbot/handler/scoresaber"
|
||||
|
112
util/web_page_shot.go
Normal file
112
util/web_page_shot.go
Normal file
@ -0,0 +1,112 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/chromedp/cdproto/emulation"
|
||||
"github.com/chromedp/cdproto/network"
|
||||
"github.com/chromedp/cdproto/page"
|
||||
"github.com/chromedp/chromedp"
|
||||
)
|
||||
|
||||
// ScreenshotURL 截图函数:传入网址、输出路径、宽高、四个边距和等待的元素 ID
|
||||
func ScreenshotURL(url, output string, width, height int, marginTop, marginRight, marginBottom, marginLeft int, waitClass string) error {
|
||||
// 创建一个上下文,连接到 Docker 中运行的 headless-shell 实例
|
||||
remoteAllocatorCtx, cancel := chromedp.NewRemoteAllocator(
|
||||
context.Background(), "ws://127.0.0.1:9222/json/ws",
|
||||
)
|
||||
defer cancel()
|
||||
|
||||
ctx, cancel := chromedp.NewContext(remoteAllocatorCtx)
|
||||
defer cancel()
|
||||
|
||||
// 设置超时时间,避免长时间无响应
|
||||
ctx, cancel = context.WithTimeout(ctx, 60*time.Second)
|
||||
defer cancel()
|
||||
|
||||
// 设置页面的宽高和缩放
|
||||
if err := chromedp.Run(ctx, setViewportAndUserAgent(width, height, "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:131.0) Gecko/20100101 Firefox/131.0")); err != nil {
|
||||
return fmt.Errorf("设置页面大小失败: %w", err)
|
||||
}
|
||||
|
||||
// 启用网络请求拦截
|
||||
if err := chromedp.Run(ctx, enableRequestInterception()); err != nil {
|
||||
return fmt.Errorf("启用请求拦截失败: %w", err)
|
||||
}
|
||||
|
||||
// 用于存储截图的变量
|
||||
var screenshot []byte
|
||||
|
||||
queryAction := chromedp.WaitVisible(fmt.Sprintf(".%s", waitClass), chromedp.ByQuery)
|
||||
if waitClass == "" {
|
||||
queryAction = chromedp.WaitVisible(`body`, chromedp.ByQuery)
|
||||
}
|
||||
|
||||
// 执行任务:打开网页并截图
|
||||
err := chromedp.Run(ctx,
|
||||
chromedp.Navigate(url), // 打开网页
|
||||
ignoreErrors(queryAction), // 等待指定元素
|
||||
chromedp.ActionFunc(func(ctx context.Context) error { // 自定义截图逻辑
|
||||
// 计算调整后的截图区域
|
||||
clip := &page.Viewport{
|
||||
X: float64(marginLeft),
|
||||
Y: float64(marginTop),
|
||||
Width: float64(width - marginLeft - marginRight),
|
||||
Height: float64(height - marginTop - marginBottom),
|
||||
Scale: 1.0,
|
||||
}
|
||||
var err error
|
||||
screenshot, err = page.CaptureScreenshot().WithClip(clip).Do(ctx)
|
||||
return err
|
||||
}),
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("截图失败: %w", err)
|
||||
}
|
||||
|
||||
// 保存截图到本地
|
||||
if err := os.WriteFile(output, screenshot, 0644); err != nil {
|
||||
return fmt.Errorf("保存图片失败: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func setViewportAndUserAgent(width, height int, userAgent string) chromedp.Tasks {
|
||||
return chromedp.Tasks{
|
||||
emulation.SetDeviceMetricsOverride(int64(width), int64(height), 1.0, false).
|
||||
WithScreenOrientation(&emulation.ScreenOrientation{
|
||||
Type: emulation.OrientationTypePortraitPrimary,
|
||||
Angle: 0,
|
||||
}),
|
||||
emulation.SetUserAgentOverride(userAgent),
|
||||
}
|
||||
}
|
||||
|
||||
// enableRequestInterception 启用网络请求拦截,忽略不必要的资源加载(如广告、图片等)
|
||||
func enableRequestInterception() chromedp.Tasks {
|
||||
return chromedp.Tasks{
|
||||
chromedp.ActionFunc(func(ctx context.Context) error {
|
||||
return network.Enable().Do(ctx)
|
||||
}),
|
||||
network.SetBlockedURLS([]string{
|
||||
"pagead2.googlesyndication.com",
|
||||
"optimizationguide-pa.googleapis.com",
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
// ignoreErrors 包裹 chromedp 任务,忽略执行过程中出现的错误
|
||||
func ignoreErrors(task chromedp.Action) chromedp.ActionFunc {
|
||||
return chromedp.ActionFunc(func(ctx context.Context) error {
|
||||
err := task.Do(ctx)
|
||||
if err != nil {
|
||||
log.Printf("忽略错误: %v", err)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user