feat(workers): 添加httpdog工作器以处理HTTP状态码查询
This commit is contained in:
parent
93ebaeea5a
commit
70c96cbfaa
1 changed files with 116 additions and 0 deletions
116
workers/httpdog.go
Normal file
116
workers/httpdog.go
Normal file
|
@ -0,0 +1,116 @@
|
|||
package workers
|
||||
|
||||
import (
|
||||
"compress/gzip"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
"github.com/andybalholm/brotli"
|
||||
|
||||
"github.com/parnurzeal/gorequest"
|
||||
)
|
||||
|
||||
func init() {
|
||||
RegisterWorkerFactory("httpdog", func(parms []string, uid, gid, role, mid, rawMsg string) Worker {
|
||||
return &HttpDog{
|
||||
StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
type HttpDog struct {
|
||||
*StdAns
|
||||
}
|
||||
|
||||
func (a *HttpDog) GetMsg() string {
|
||||
if len(a.Parms) < 2 {
|
||||
return "您可以使用 !httpdog 状态码 进行查找,状态码支持100-599"
|
||||
}
|
||||
baseUrl := "https://http.dog/"
|
||||
picUrl := baseUrl + a.Parms[1] + ".jpg"
|
||||
// 使用 gorequest 发起 HEAD 请求检查资源是否存在
|
||||
request := gorequest.New()
|
||||
resp, _, errs := request.Head(picUrl).End()
|
||||
if len(errs) > 0 {
|
||||
return "请求错误: " + fmt.Sprint(errs)
|
||||
}
|
||||
|
||||
// 判断资源是否存在
|
||||
if resp.StatusCode != 200 {
|
||||
return fmt.Sprintf("资源不存在,状态码: %d", resp.StatusCode)
|
||||
}
|
||||
respContent := "[CQ:image,file=" + picUrl + "]"
|
||||
docUrl := "https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Status/" + a.Parms[1]
|
||||
res, _, errs := request.Get(docUrl).
|
||||
Set("Host", "developer.mozilla.org").
|
||||
Set("User-Agent", "Apifox/1.0.0 (https://apifox.com)").
|
||||
Set("Accept", "*/*").
|
||||
Set("Content-Type", "text/plain").
|
||||
End()
|
||||
if len(errs) > 0 {
|
||||
log.Println("请求错误: " + fmt.Sprint(errs))
|
||||
}
|
||||
if res != nil {
|
||||
defer res.Body.Close()
|
||||
}
|
||||
if res != nil && res.StatusCode == 200 {
|
||||
// 解压缩响应体
|
||||
var reader io.ReadCloser
|
||||
switch res.Header.Get("Content-Encoding") {
|
||||
case "br":
|
||||
reader = io.NopCloser(brotli.NewReader(res.Body))
|
||||
case "gzip":
|
||||
var err error
|
||||
reader, err = gzip.NewReader(res.Body)
|
||||
if err != nil {
|
||||
fmt.Println("gzip 解压错误:", err)
|
||||
return respContent
|
||||
}
|
||||
defer reader.Close()
|
||||
default:
|
||||
reader = res.Body
|
||||
}
|
||||
|
||||
// 使用 goquery 解析 HTML
|
||||
doc, err := goquery.NewDocumentFromReader(reader)
|
||||
if err != nil {
|
||||
fmt.Println("HTML 解析错误:", err)
|
||||
return respContent
|
||||
}
|
||||
|
||||
// 查找 <meta name="description"> 标签并获取 content 属性的值
|
||||
description, exists := doc.Find(`meta[name="description"]`).Attr("content")
|
||||
if exists {
|
||||
// fmt.Println("Meta Description Content:", description)
|
||||
respContent += description
|
||||
} else {
|
||||
fmt.Println("没有找到 meta description 标签")
|
||||
// 将解析后的 HTML 保存到文件
|
||||
// file, err := os.Create("output.html")
|
||||
// if err != nil {
|
||||
// fmt.Println("文件创建错误:", err)
|
||||
// return respContent
|
||||
// }
|
||||
// defer file.Close()
|
||||
|
||||
// // 输出 HTML 内容到文件
|
||||
// html, err := doc.Html()
|
||||
// if err != nil {
|
||||
// fmt.Println("获取 HTML 错误:", err)
|
||||
// return respContent
|
||||
// }
|
||||
// _, err = file.WriteString(html)
|
||||
// if err != nil {
|
||||
// fmt.Println("写入文件错误:", err)
|
||||
// return respContent
|
||||
// }
|
||||
|
||||
// fmt.Println("HTML 内容已保存到 output.html 文件")
|
||||
}
|
||||
}
|
||||
|
||||
return respContent
|
||||
|
||||
}
|
Loading…
Reference in a new issue