parent
7c0cc2fdbc
commit
2f1b913704
@ -0,0 +1,74 @@
|
|||||||
|
package weishi
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"go.dtapp.net/library/utils/golog"
|
||||||
|
"go.dtapp.net/library/utils/gorequest"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ConfigClient struct {
|
||||||
|
PgsqlDb *gorm.DB // pgsql数据库
|
||||||
|
}
|
||||||
|
|
||||||
|
type Client struct {
|
||||||
|
ua string // 用户代理
|
||||||
|
client *gorequest.App // 请求客户端
|
||||||
|
log *golog.ApiClient // 日志服务
|
||||||
|
logStatus bool // 日志状态
|
||||||
|
config *ConfigClient // 配置
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewClient(config *ConfigClient) (*Client, error) {
|
||||||
|
|
||||||
|
var err error
|
||||||
|
c := &Client{config: config}
|
||||||
|
|
||||||
|
c.ua = "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1"
|
||||||
|
|
||||||
|
c.client = gorequest.NewHttp()
|
||||||
|
if c.config.PgsqlDb != nil {
|
||||||
|
c.logStatus = true
|
||||||
|
c.log, err = golog.NewApiClient(&golog.ConfigApiClient{
|
||||||
|
Db: c.config.PgsqlDb,
|
||||||
|
TableName: logTable,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return c, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) urlJudge(str string) string {
|
||||||
|
if strings.Index(str, "weishi.qq.com") != -1 {
|
||||||
|
return str
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) request302(url string) (string, error) {
|
||||||
|
req, err := http.NewRequest("GET", url, nil)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
client := new(http.Client)
|
||||||
|
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
|
||||||
|
return errors.New("redirect")
|
||||||
|
}
|
||||||
|
|
||||||
|
response, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
if response.StatusCode == http.StatusFound {
|
||||||
|
location, err := response.Location()
|
||||||
|
return location.String(), err
|
||||||
|
} else {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return "", nil
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
package weishi
|
||||||
|
|
||||||
|
const (
|
||||||
|
logTable = "weishi"
|
||||||
|
)
|
@ -0,0 +1,28 @@
|
|||||||
|
package weishi
|
||||||
|
|
||||||
|
import "go.dtapp.net/library/utils/gorequest"
|
||||||
|
|
||||||
|
func (c *Client) request(url string) (gorequest.Response, error) {
|
||||||
|
|
||||||
|
// 创建请求
|
||||||
|
client := c.client
|
||||||
|
|
||||||
|
// 设置请求地址
|
||||||
|
client.SetUri(url)
|
||||||
|
|
||||||
|
// 设置用户代理
|
||||||
|
client.SetUserAgent(c.ua)
|
||||||
|
|
||||||
|
// 发起请求
|
||||||
|
request, err := client.Get()
|
||||||
|
if err != nil {
|
||||||
|
return gorequest.Response{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 日志
|
||||||
|
if c.logStatus == true {
|
||||||
|
go c.postgresqlLog(request)
|
||||||
|
}
|
||||||
|
|
||||||
|
return request, err
|
||||||
|
}
|
@ -1,89 +0,0 @@
|
|||||||
package weishi
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"go.dtapp.net/library/utils/golog"
|
|
||||||
"go.dtapp.net/library/utils/gorequest"
|
|
||||||
"gorm.io/gorm"
|
|
||||||
"net/http"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
type WeiShi struct {
|
|
||||||
ua string // 用户代理
|
|
||||||
pgsql *gorm.DB // pgsql数据库
|
|
||||||
client *gorequest.App // 请求客户端
|
|
||||||
log *golog.Api // 日志服务
|
|
||||||
logTableName string // 日志表名
|
|
||||||
logStatus bool // 日志状态
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewWeiShi(pgsql *gorm.DB) *WeiShi {
|
|
||||||
ws := &WeiShi{ua: "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1"}
|
|
||||||
ws.client = gorequest.NewHttp()
|
|
||||||
if pgsql != nil {
|
|
||||||
ws.pgsql = pgsql
|
|
||||||
ws.logStatus = true
|
|
||||||
ws.logTableName = "weishi"
|
|
||||||
ws.log = golog.NewApi(&golog.ApiConfig{
|
|
||||||
Db: pgsql,
|
|
||||||
TableName: ws.logTableName,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
return ws
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ws *WeiShi) request(url string) (resp gorequest.Response, err error) {
|
|
||||||
|
|
||||||
// 创建请求
|
|
||||||
client := ws.client
|
|
||||||
|
|
||||||
// 设置请求地址
|
|
||||||
client.SetUri(url)
|
|
||||||
|
|
||||||
// 设置用户代理
|
|
||||||
client.SetUserAgent(ws.ua)
|
|
||||||
|
|
||||||
// 发起请求
|
|
||||||
request, err := client.Get()
|
|
||||||
if err != nil {
|
|
||||||
return gorequest.Response{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 日志
|
|
||||||
if ws.logStatus == true {
|
|
||||||
go ws.postgresqlLog(request)
|
|
||||||
}
|
|
||||||
|
|
||||||
return request, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ws *WeiShi) urlJudge(str string) string {
|
|
||||||
if strings.Index(str, "weishi.qq.com") != -1 {
|
|
||||||
return str
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ws *WeiShi) request302(url string) (string, error) {
|
|
||||||
req, err := http.NewRequest("GET", url, nil)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
client := new(http.Client)
|
|
||||||
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
|
|
||||||
return errors.New("redirect")
|
|
||||||
}
|
|
||||||
|
|
||||||
response, err := client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
if response.StatusCode == http.StatusFound {
|
|
||||||
location, err := response.Location()
|
|
||||||
return location.String(), err
|
|
||||||
} else {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return "", nil
|
|
||||||
}
|
|
Loading…
Reference in new issue