- 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 抖音解析
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

@ -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
}

@ -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, //【请求】链接
Loading…
Cancel
Save