diff --git a/service/weishi/api.go b/service/weishi/api.go index 275f0c5d..e8033bcc 100644 --- a/service/weishi/api.go +++ b/service/weishi/api.go @@ -1048,12 +1048,12 @@ type AnalysisResult struct { Err error // 错误 } -func NewAnalysisResult(result AnalysisResponse, body []byte, http gorequest.Response, err error) *AnalysisResult { +func newAnalysisResult(result AnalysisResponse, body []byte, http gorequest.Response, err error) *AnalysisResult { return &AnalysisResult{Result: result, Body: body, Http: http, Err: err} } // Analysis 微视解析 -func (ws *WeiShi) Analysis(content string) *AnalysisResult { +func (c *Client) Analysis(content string) *AnalysisResult { // 提取url var url string @@ -1062,16 +1062,16 @@ func (ws *WeiShi) Analysis(content string) *AnalysisResult { } else if strings.Contains(content, "isee.weishi") { url = xurls.Relaxed.FindString(content) } else { - return NewAnalysisResult(AnalysisResponse{}, nil, gorequest.Response{}, errors.New("url为空")) + return newAnalysisResult(AnalysisResponse{}, nil, gorequest.Response{}, errors.New("url为空")) } // 内容匹配 var feedid string if strings.Contains(url, "h5.weishi") { // 重定向信息 - request302, err := ws.request302(url) + request302, err := c.request302(url) if err != nil { - return NewAnalysisResult(AnalysisResponse{}, nil, gorequest.Response{}, err) + return newAnalysisResult(AnalysisResponse{}, nil, gorequest.Response{}, err) } feedid = strings.Split(request302, "/")[3] ///share/video/6734643996347485448/?region=CN&mid=6734637731277851404&u_code=0&titleType=title&utm_source=copy_link&utm_campaign=client_share&utm_medium=android&app=aweme @@ -1081,10 +1081,10 @@ func (ws *WeiShi) Analysis(content string) *AnalysisResult { feedid = regexp.MustCompile("id=(.*?)&").FindStringSubmatch(url)[1] } - request, err := ws.request("https://h5.qzone.qq.com/webapp/json/weishi/WSH5GetPlayPage?feedid=" + feedid) + request, err := c.request("https://h5.qzone.qq.com/webapp/json/weishi/WSH5GetPlayPage?feedid=" + feedid) // 定义 var response AnalysisResponse err = json.Unmarshal(request.ResponseBody, &response) - return NewAnalysisResult(response, request.ResponseBody, request, err) + return newAnalysisResult(response, request.ResponseBody, request, err) } diff --git a/service/weishi/client.go b/service/weishi/client.go new file mode 100644 index 00000000..33d290a5 --- /dev/null +++ b/service/weishi/client.go @@ -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 +} diff --git a/service/weishi/const.go b/service/weishi/const.go new file mode 100644 index 00000000..15e197be --- /dev/null +++ b/service/weishi/const.go @@ -0,0 +1,5 @@ +package weishi + +const ( + logTable = "weishi" +) diff --git a/service/weishi/pgsql.go b/service/weishi/pgsql.go index 3d16c999..588984df 100644 --- a/service/weishi/pgsql.go +++ b/service/weishi/pgsql.go @@ -8,8 +8,8 @@ import ( ) // 记录日志 -func (ws *WeiShi) postgresqlLog(request gorequest.Response) { - ws.log.Record(golog.ApiPostgresqlLog{ +func (c *Client) postgresqlLog(request gorequest.Response) { + c.log.Record(golog.ApiPostgresqlLog{ RequestTime: golog.TimeString{Time: request.RequestTime}, //【请求】时间 RequestUri: request.RequestUri, //【请求】链接 RequestUrl: gorequest.UriParse(request.RequestUri).Url, //【请求】链接 diff --git a/service/weishi/request.go b/service/weishi/request.go new file mode 100644 index 00000000..f0ad0673 --- /dev/null +++ b/service/weishi/request.go @@ -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 +} diff --git a/service/weishi/weishi.go b/service/weishi/weishi.go deleted file mode 100644 index 14c3e5a8..00000000 --- a/service/weishi/weishi.go +++ /dev/null @@ -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 -} diff --git a/service/wikeyun/rest.power.add_card.go b/service/wikeyun/rest.power.add_card.go index 361004fe..71fbf91f 100644 --- a/service/wikeyun/rest.power.add_card.go +++ b/service/wikeyun/rest.power.add_card.go @@ -28,7 +28,7 @@ type RestPowerAddCardResult struct { Err error // 错误 } -func NewRestPowerAddCardResult(result RestPowerAddCardResponse, body []byte, http gorequest.Response, err error) *RestPowerAddCardResult { +func newRestPowerAddCardResult(result RestPowerAddCardResponse, body []byte, http gorequest.Response, err error) *RestPowerAddCardResult { return &RestPowerAddCardResult{Result: result, Body: body, Http: http, Err: err} } @@ -43,5 +43,5 @@ func (c *Client) RestPowerAddCard(notMustParams ...gorequest.Params) *RestPowerA // 定义 var response RestPowerAddCardResponse err = json.Unmarshal(request.ResponseBody, &response) - return NewRestPowerAddCardResult(response, request.ResponseBody, request, err) + return newRestPowerAddCardResult(response, request.ResponseBody, request, err) } diff --git a/service/wikeyun/rest.power.cancel.go b/service/wikeyun/rest.power.cancel.go index ae10b495..bf82d706 100644 --- a/service/wikeyun/rest.power.cancel.go +++ b/service/wikeyun/rest.power.cancel.go @@ -18,7 +18,7 @@ type RestPowerCancelResult struct { Err error // 错误 } -func NewRestPowerCancelResult(result RestPowerCancelResponse, body []byte, http gorequest.Response, err error) *RestPowerCancelResult { +func newRestPowerCancelResult(result RestPowerCancelResponse, body []byte, http gorequest.Response, err error) *RestPowerCancelResult { return &RestPowerCancelResult{Result: result, Body: body, Http: http, Err: err} } @@ -34,5 +34,5 @@ func (c *Client) RestPowerCancel(orderNumber string) *RestPowerCancelResult { // 定义 var response RestPowerCancelResponse err = json.Unmarshal(request.ResponseBody, &response) - return NewRestPowerCancelResult(response, request.ResponseBody, request, err) + return newRestPowerCancelResult(response, request.ResponseBody, request, err) } diff --git a/service/wikeyun/rest.power.del_card.go b/service/wikeyun/rest.power.del_card.go index 58a23597..4b1a37a1 100644 --- a/service/wikeyun/rest.power.del_card.go +++ b/service/wikeyun/rest.power.del_card.go @@ -19,7 +19,7 @@ type RestPowerDelCardResult struct { Err error // 错误 } -func NewRestPowerDelCardResult(result RestPowerDelCardResponse, body []byte, http gorequest.Response, err error) *RestPowerDelCardResult { +func newRestPowerDelCardResult(result RestPowerDelCardResponse, body []byte, http gorequest.Response, err error) *RestPowerDelCardResult { return &RestPowerDelCardResult{Result: result, Body: body, Http: http, Err: err} } @@ -35,5 +35,5 @@ func (c *Client) RestPowerDelCard(cardId string) *RestPowerDelCardResult { // 定义 var response RestPowerDelCardResponse err = json.Unmarshal(request.ResponseBody, &response) - return NewRestPowerDelCardResult(response, request.ResponseBody, request, err) + return newRestPowerDelCardResult(response, request.ResponseBody, request, err) } diff --git a/service/wikeyun/rest.power.push_order.go b/service/wikeyun/rest.power.push_order.go index 14cb605f..70737357 100644 --- a/service/wikeyun/rest.power.push_order.go +++ b/service/wikeyun/rest.power.push_order.go @@ -21,7 +21,7 @@ type RestPowerPushOrderResult struct { Err error // 错误 } -func NewRestPowerPushOrderResult(result RestPowerPushOrderResponse, body []byte, http gorequest.Response, err error) *RestPowerPushOrderResult { +func newRestPowerPushOrderResult(result RestPowerPushOrderResponse, body []byte, http gorequest.Response, err error) *RestPowerPushOrderResult { return &RestPowerPushOrderResult{Result: result, Body: body, Http: http, Err: err} } @@ -36,5 +36,5 @@ func (c *Client) RestPowerPushOrder(notMustParams ...gorequest.Params) *RestPowe // 定义 var response RestPowerPushOrderResponse err = json.Unmarshal(request.ResponseBody, &response) - return NewRestPowerPushOrderResult(response, request.ResponseBody, request, err) + return newRestPowerPushOrderResult(response, request.ResponseBody, request, err) } diff --git a/service/wikeyun/rest.power.query.go b/service/wikeyun/rest.power.query.go index 11723606..d73c7b13 100644 --- a/service/wikeyun/rest.power.query.go +++ b/service/wikeyun/rest.power.query.go @@ -28,7 +28,7 @@ type RestPowerQueryResult struct { Err error // 错误 } -func NewRestPowerQueryResult(result RestPowerQueryResponse, body []byte, http gorequest.Response, err error) *RestPowerQueryResult { +func newRestPowerQueryResult(result RestPowerQueryResponse, body []byte, http gorequest.Response, err error) *RestPowerQueryResult { return &RestPowerQueryResult{Result: result, Body: body, Http: http, Err: err} } @@ -44,5 +44,5 @@ func (c *Client) RestPowerQuery(orderNumber string) *RestPowerQueryResult { // 定义 var response RestPowerQueryResponse err = json.Unmarshal(request.ResponseBody, &response) - return NewRestPowerQueryResult(response, request.ResponseBody, request, err) + return newRestPowerQueryResult(response, request.ResponseBody, request, err) } diff --git a/service/wikeyun/rest.recharge.cancel.go b/service/wikeyun/rest.recharge.cancel.go index a4b24056..6a57ab96 100644 --- a/service/wikeyun/rest.recharge.cancel.go +++ b/service/wikeyun/rest.recharge.cancel.go @@ -18,7 +18,7 @@ type RestRechargeCancelResult struct { Err error // 错误 } -func NewRestRechargeCancelResult(result RestRechargeCancelResponse, body []byte, http gorequest.Response, err error) *RestRechargeCancelResult { +func newRestRechargeCancelResult(result RestRechargeCancelResponse, body []byte, http gorequest.Response, err error) *RestRechargeCancelResult { return &RestRechargeCancelResult{Result: result, Body: body, Http: http, Err: err} } @@ -34,5 +34,5 @@ func (c *Client) RestRechargeCancel(orderNumber string) *RestRechargeCancelResul // 定义 var response RestRechargeCancelResponse err = json.Unmarshal(request.ResponseBody, &response) - return NewRestRechargeCancelResult(response, request.ResponseBody, request, err) + return newRestRechargeCancelResult(response, request.ResponseBody, request, err) } diff --git a/service/wikeyun/rest.recharge.mobileInfo.go b/service/wikeyun/rest.recharge.mobileInfo.go index 578f6ad3..a93e5fb4 100644 --- a/service/wikeyun/rest.recharge.mobileInfo.go +++ b/service/wikeyun/rest.recharge.mobileInfo.go @@ -38,7 +38,7 @@ type RestRechargeMobileInfoResult struct { Err error // 错误 } -func NewRestRechargeMobileInfoResult(result RestRechargeMobileInfoResponse, body []byte, http gorequest.Response, err error) *RestRechargeMobileInfoResult { +func newRestRechargeMobileInfoResult(result RestRechargeMobileInfoResponse, body []byte, http gorequest.Response, err error) *RestRechargeMobileInfoResult { return &RestRechargeMobileInfoResult{Result: result, Body: body, Http: http, Err: err} } @@ -54,5 +54,5 @@ func (c *Client) RestRechargeMobileInfo(orderNumber string) *RestRechargeMobileI // 定义 var response RestRechargeMobileInfoResponse err = json.Unmarshal(request.ResponseBody, &response) - return NewRestRechargeMobileInfoResult(response, request.ResponseBody, request, err) + return newRestRechargeMobileInfoResult(response, request.ResponseBody, request, err) } diff --git a/service/wikeyun/rest.recharge.push_order.go b/service/wikeyun/rest.recharge.push_order.go index cf7e45f6..2a6dd100 100644 --- a/service/wikeyun/rest.recharge.push_order.go +++ b/service/wikeyun/rest.recharge.push_order.go @@ -20,7 +20,7 @@ type RestRechargePushOrderResult struct { Err error // 错误 } -func NewRestRechargePushOrderResult(result RestRechargePushOrderResponse, body []byte, http gorequest.Response, err error) *RestRechargePushOrderResult { +func newRestRechargePushOrderResult(result RestRechargePushOrderResponse, body []byte, http gorequest.Response, err error) *RestRechargePushOrderResult { return &RestRechargePushOrderResult{Result: result, Body: body, Http: http, Err: err} } @@ -35,5 +35,5 @@ func (c *Client) RestRechargePushOrder(notMustParams ...gorequest.Params) *RestR // 定义 var response RestRechargePushOrderResponse err = json.Unmarshal(request.ResponseBody, &response) - return NewRestRechargePushOrderResult(response, request.ResponseBody, request, err) + return newRestRechargePushOrderResult(response, request.ResponseBody, request, err) } diff --git a/service/wikeyun/rest.recharge.query.go b/service/wikeyun/rest.recharge.query.go index fb61aea0..601d16a0 100644 --- a/service/wikeyun/rest.recharge.query.go +++ b/service/wikeyun/rest.recharge.query.go @@ -28,7 +28,7 @@ type RestRechargeQueryResult struct { Err error // 错误 } -func NewRestRechargeQueryResult(result RestRechargeQueryResponse, body []byte, http gorequest.Response, err error) *RestRechargeQueryResult { +func newRestRechargeQueryResult(result RestRechargeQueryResponse, body []byte, http gorequest.Response, err error) *RestRechargeQueryResult { return &RestRechargeQueryResult{Result: result, Body: body, Http: http, Err: err} } @@ -44,5 +44,5 @@ func (c *Client) RestRechargeQuery(orderNumber string) *RestRechargeQueryResult // 定义 var response RestRechargeQueryResponse err = json.Unmarshal(request.ResponseBody, &response) - return NewRestRechargeQueryResult(response, request.ResponseBody, request, err) + return newRestRechargeQueryResult(response, request.ResponseBody, request, err) } diff --git a/service/wikeyun/rest.user.query.go b/service/wikeyun/rest.user.query.go index 90deda5b..8f2b0683 100644 --- a/service/wikeyun/rest.user.query.go +++ b/service/wikeyun/rest.user.query.go @@ -23,7 +23,7 @@ type RestUserQueryResult struct { Err error // 错误 } -func NewRestUserQueryResult(result RestUserQueryResponse, body []byte, http gorequest.Response, err error) *RestUserQueryResult { +func newRestUserQueryResult(result RestUserQueryResponse, body []byte, http gorequest.Response, err error) *RestUserQueryResult { return &RestUserQueryResult{Result: result, Body: body, Http: http, Err: err} } @@ -35,5 +35,5 @@ func (c *Client) RestUserQuery() *RestUserQueryResult { // 定义 var response RestUserQueryResponse err = json.Unmarshal(request.ResponseBody, &response) - return NewRestUserQueryResult(response, request.ResponseBody, request, err) + return newRestUserQueryResult(response, request.ResponseBody, request, err) }