- update service/douyin

master
李光春 2 years ago
parent 07b3f293f7
commit c462f1502c

@ -184,7 +184,7 @@ func NewAnalysisResult(result AnalysisResponse, body []byte, http gorequest.Resp
} }
// Analysis 抖音解析 // Analysis 抖音解析
func (app *App) Analysis(content string) *AnalysisResult { func (c *Client) Analysis(content string) *AnalysisResult {
// 提取url // 提取url
var url string 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 { if err != nil {
return NewAnalysisResult(AnalysisResponse{}, nil, gorequest.Response{}, err) 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 var response AnalysisResponse

@ -9,34 +9,40 @@ import (
"strings" "strings"
) )
type App struct { type ConfigClient struct {
PgsqlDb *gorm.DB // pgsql数据库
}
type Client struct {
ua string // 用户代理 ua string // 用户代理
pgsql *gorm.DB // pgsql数据库
client *gorequest.App // 请求客户端 client *gorequest.App // 请求客户端
log *golog.Api // 日志服务 log *golog.Api // 日志服务
logTableName string // 日志表名 logTableName string // 日志表名
logStatus bool // 日志状态 logStatus bool // 日志状态
config *ConfigClient // 配置
} }
func NewApp(pgsql *gorm.DB) *App { func NewClient(config *ConfigClient) *Client {
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() c := &Client{config: config}
if pgsql != nil {
app.pgsql = pgsql c.client = gorequest.NewHttp()
app.logStatus = true if c.config.PgsqlDb != nil {
app.logTableName = "douyin" c.logStatus = true
app.log = golog.NewApi(&golog.ApiConfig{ c.logTableName = "douyin"
Db: pgsql, c.log = golog.NewApi(&golog.ApiConfig{
TableName: app.logTableName, 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) client.SetUri(url)
@ -48,7 +54,7 @@ func (app *App) request(url string, params map[string]interface{}, method string
client.SetContentTypeJson() client.SetContentTypeJson()
// 设置用户代理 // 设置用户代理
client.SetUserAgent(app.ua) client.SetUserAgent(c.ua)
// 设置参数 // 设置参数
client.SetParams(params) client.SetParams(params)
@ -60,14 +66,14 @@ func (app *App) request(url string, params map[string]interface{}, method string
} }
// 日志 // 日志
if app.logStatus == true { if c.logStatus == true {
go app.postgresqlLog(request) go c.postgresqlLog(request)
} }
return request, err 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) req, err := http.NewRequest("GET", url, nil)
if err != nil { if err != nil {
return "", err return "", err
@ -90,7 +96,7 @@ func (app *App) request302(url string) (string, error) {
return "", nil 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 { if strings.Index(str, "douyin.com") != -1 || strings.Index(str, "iesdouyin.com") != -1 {
return str return str
} }

@ -8,8 +8,8 @@ import (
) )
// 记录日志 // 记录日志
func (app *App) postgresqlLog(request gorequest.Response) { func (c *Client) postgresqlLog(request gorequest.Response) {
app.log.Record(golog.ApiPostgresqlLog{ c.log.Record(golog.ApiPostgresqlLog{
RequestTime: golog.TimeString{Time: request.RequestTime}, //【请求】时间 RequestTime: golog.TimeString{Time: request.RequestTime}, //【请求】时间
RequestUri: request.RequestUri, //【请求】链接 RequestUri: request.RequestUri, //【请求】链接
RequestUrl: gorequest.UriParse(request.RequestUri).Url, //【请求】链接 RequestUrl: gorequest.UriParse(request.RequestUri).Url, //【请求】链接
Loading…
Cancel
Save