parent
a182d2c4ca
commit
1fda3da600
@ -1,5 +1,5 @@
|
||||
package go_library
|
||||
|
||||
func Version() string {
|
||||
return "v2022-07-20"
|
||||
return "v1.0.46"
|
||||
}
|
||||
|
@ -0,0 +1,37 @@
|
||||
package baidu
|
||||
|
||||
import (
|
||||
"github.com/dtapps/go-library/utils/golog"
|
||||
"github.com/dtapps/go-library/utils/gorequest"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type ConfigClient struct {
|
||||
PgsqlDb *gorm.DB // 日志数据库
|
||||
}
|
||||
|
||||
type Client struct {
|
||||
client *gorequest.App // 请求客户端
|
||||
log *golog.ApiClient // 日志服务
|
||||
config *ConfigClient // 配置
|
||||
}
|
||||
|
||||
func NewClient(config *ConfigClient) (*Client, error) {
|
||||
|
||||
var err error
|
||||
c := &Client{config: config}
|
||||
|
||||
c.client = gorequest.NewHttp()
|
||||
|
||||
if c.config.PgsqlDb != nil {
|
||||
c.log, err = golog.NewApiClient(
|
||||
golog.WithGormClient(c.config.PgsqlDb),
|
||||
golog.WithTableName(logTable),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package baidu
|
||||
|
||||
const (
|
||||
logTable = "baidu"
|
||||
)
|
@ -0,0 +1,33 @@
|
||||
package baidu
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/dtapps/go-library/utils/gorequest"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type ExpressResponse struct{}
|
||||
|
||||
type ExpressResult struct {
|
||||
Result ExpressResponse // 结果
|
||||
Body []byte // 内容
|
||||
Http gorequest.Response // 请求
|
||||
Err error // 错误
|
||||
}
|
||||
|
||||
func newExpressResult(result ExpressResponse, body []byte, http gorequest.Response, err error) *ExpressResult {
|
||||
return &ExpressResult{Result: result, Body: body, Http: http, Err: err}
|
||||
}
|
||||
|
||||
// Express ipv1
|
||||
// https://www.cloudflare.com/ips-v4
|
||||
func (c *Client) Express() *ExpressResult {
|
||||
// 参数
|
||||
params := gorequest.NewParams()
|
||||
// 请求
|
||||
request, err := c.request("https://m.baidu.com/s?word=快递查询&ts={$ts}&t_kt=0&ie=utf-8&rsv_iqid=&rsv_t=&sa=&rsv_pq=&rsv_sug4=&tj=1&inputT={$input}&sugid=&ss=", params, http.MethodPost)
|
||||
// 定义
|
||||
var response ExpressResponse
|
||||
err = json.Unmarshal(request.ResponseBody, &response)
|
||||
return newExpressResult(response, request.ResponseBody, request, err)
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package baidu
|
||||
|
||||
import "github.com/dtapps/go-library/utils/gorequest"
|
||||
|
||||
func (c *Client) request(url string, params map[string]interface{}, method string) (gorequest.Response, error) {
|
||||
|
||||
// 创建请求
|
||||
client := c.client
|
||||
|
||||
// 设置请求地址
|
||||
client.SetUri(url)
|
||||
|
||||
// 设置方式
|
||||
client.SetMethod(method)
|
||||
|
||||
// 设置格式
|
||||
client.SetContentTypeJson()
|
||||
|
||||
// 设置参数
|
||||
client.SetParams(params)
|
||||
|
||||
// 发起请求
|
||||
request, err := client.Request()
|
||||
if err != nil {
|
||||
return gorequest.Response{}, err
|
||||
}
|
||||
|
||||
// 日志
|
||||
if c.config.PgsqlDb != nil {
|
||||
go c.log.GormMiddleware(request)
|
||||
}
|
||||
|
||||
return request, err
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue