From c462f1502c1338fea2bdba9067c5cbdb22fa9598 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=85=89=E6=98=A5?= Date: Sat, 18 Jun 2022 09:55:20 +0800 Subject: [PATCH] - update service/douyin --- service/douyin/api.go | 6 ++-- service/douyin/{app.go => client.go} | 46 ++++++++++++++++------------ service/douyin/{pgsql.go => log.go} | 4 +-- 3 files changed, 31 insertions(+), 25 deletions(-) rename service/douyin/{app.go => client.go} (61%) rename service/douyin/{pgsql.go => log.go} (93%) diff --git a/service/douyin/api.go b/service/douyin/api.go index 6dffd173..f21a7027 100644 --- a/service/douyin/api.go +++ b/service/douyin/api.go @@ -184,7 +184,7 @@ func NewAnalysisResult(result AnalysisResponse, body []byte, http gorequest.Resp } // Analysis 抖音解析 -func (app *App) Analysis(content string) *AnalysisResult { +func (c *Client) Analysis(content string) *AnalysisResult { // 提取url var url string @@ -197,7 +197,7 @@ func (app *App) Analysis(content string) *AnalysisResult { } // 重定向信息 - request302, err := app.request302(url) + request302, err := c.request302(url) if err != nil { return NewAnalysisResult(AnalysisResponse{}, nil, gorequest.Response{}, err) } @@ -209,7 +209,7 @@ func (app *App) Analysis(content string) *AnalysisResult { } // 请求 - request, err := app.request("https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids="+itemIds[0], map[string]interface{}{}, http.MethodGet) + request, err := c.request("https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids="+itemIds[0], map[string]interface{}{}, http.MethodGet) // 定义 var response AnalysisResponse diff --git a/service/douyin/app.go b/service/douyin/client.go similarity index 61% rename from service/douyin/app.go rename to service/douyin/client.go index 69666757..a3df0dfb 100644 --- a/service/douyin/app.go +++ b/service/douyin/client.go @@ -9,34 +9,40 @@ import ( "strings" ) -type App struct { +type ConfigClient struct { + PgsqlDb *gorm.DB // pgsql数据库 +} + +type Client struct { ua string // 用户代理 - pgsql *gorm.DB // pgsql数据库 client *gorequest.App // 请求客户端 log *golog.Api // 日志服务 logTableName string // 日志表名 logStatus bool // 日志状态 + config *ConfigClient // 配置 } -func NewApp(pgsql *gorm.DB) *App { - app := &App{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"} - app.client = gorequest.NewHttp() - if pgsql != nil { - app.pgsql = pgsql - app.logStatus = true - app.logTableName = "douyin" - app.log = golog.NewApi(&golog.ApiConfig{ - Db: pgsql, - TableName: app.logTableName, +func NewClient(config *ConfigClient) *Client { + + c := &Client{config: config} + + c.client = gorequest.NewHttp() + if c.config.PgsqlDb != nil { + c.logStatus = true + c.logTableName = "douyin" + c.log = golog.NewApi(&golog.ApiConfig{ + Db: c.config.PgsqlDb, + TableName: c.logTableName, }) } - return app + + return c } -func (app *App) request(url string, params map[string]interface{}, method string) (resp gorequest.Response, err error) { +func (c *Client) request(url string, params map[string]interface{}, method string) (resp gorequest.Response, err error) { // 创建请求 - client := app.client + client := c.client // 设置请求地址 client.SetUri(url) @@ -48,7 +54,7 @@ func (app *App) request(url string, params map[string]interface{}, method string client.SetContentTypeJson() // 设置用户代理 - client.SetUserAgent(app.ua) + client.SetUserAgent(c.ua) // 设置参数 client.SetParams(params) @@ -60,14 +66,14 @@ func (app *App) request(url string, params map[string]interface{}, method string } // 日志 - if app.logStatus == true { - go app.postgresqlLog(request) + if c.logStatus == true { + go c.postgresqlLog(request) } return request, err } -func (app *App) request302(url string) (string, error) { +func (c *Client) request302(url string) (string, error) { req, err := http.NewRequest("GET", url, nil) if err != nil { return "", err @@ -90,7 +96,7 @@ func (app *App) request302(url string) (string, error) { return "", nil } -func (app *App) urlJudge(str string) string { +func (c *Client) urlJudge(str string) string { if strings.Index(str, "douyin.com") != -1 || strings.Index(str, "iesdouyin.com") != -1 { return str } diff --git a/service/douyin/pgsql.go b/service/douyin/log.go similarity index 93% rename from service/douyin/pgsql.go rename to service/douyin/log.go index 57f12fa3..1dea21fd 100644 --- a/service/douyin/pgsql.go +++ b/service/douyin/log.go @@ -8,8 +8,8 @@ import ( ) // 记录日志 -func (app *App) postgresqlLog(request gorequest.Response) { - app.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, //【请求】链接