pdd
This commit is contained in:
137
platformapi/pddapi/pdd.go
Normal file
137
platformapi/pddapi/pdd.go
Normal file
@@ -0,0 +1,137 @@
|
||||
package pddapi
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"git.rosy.net.cn/baseapi"
|
||||
"git.rosy.net.cn/baseapi/platformapi"
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"net/http"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
prodURL = "https://gw-api.pinduoduo.com/api/router"
|
||||
sigKey = "sign"
|
||||
)
|
||||
|
||||
type API struct {
|
||||
platformapi.APICookie
|
||||
|
||||
appKey string
|
||||
appSecret string
|
||||
client *http.Client
|
||||
config *platformapi.APIConfig
|
||||
}
|
||||
|
||||
func New(appKey, appSecret string, config ...*platformapi.APIConfig) *API {
|
||||
curConfig := platformapi.DefAPIConfig
|
||||
if len(config) > 0 {
|
||||
curConfig = *config[0]
|
||||
}
|
||||
return &API{
|
||||
appKey: appKey,
|
||||
appSecret: appSecret,
|
||||
client: &http.Client{Timeout: curConfig.ClientTimeout},
|
||||
config: &curConfig,
|
||||
}
|
||||
}
|
||||
|
||||
func (a *API) signParam(params map[string]interface{}) (sig string) {
|
||||
var valueList []string
|
||||
for k, v := range params {
|
||||
if k != sigKey {
|
||||
if str := fmt.Sprint(v); str != "" {
|
||||
valueList = append(valueList, fmt.Sprintf("%s%s", k, str))
|
||||
}
|
||||
}
|
||||
}
|
||||
sort.Sort(sort.StringSlice(valueList))
|
||||
valueList = append(valueList, fmt.Sprintf("%s", a.appSecret))
|
||||
var valueList2 = make([]string, len(valueList)+1)
|
||||
at := copy(valueList2, valueList[:0])
|
||||
at += copy(valueList2[at:], []string{a.appSecret})
|
||||
copy(valueList2[at:], valueList[0:])
|
||||
sig = strings.Join(valueList2, "")
|
||||
binSig := md5.Sum([]byte(sig))
|
||||
sig = fmt.Sprintf("%X", binSig)
|
||||
return sig
|
||||
}
|
||||
|
||||
func (a *API) AccessAPI(action string, isPost bool, bizParams map[string]interface{}) (retVal map[string]interface{}, err error) {
|
||||
params := make(map[string]interface{})
|
||||
params["client_id"] = a.appKey
|
||||
params["type"] = action
|
||||
params["timestamp"] = time.Now().Unix()
|
||||
params = utils.MergeMaps(params, bizParams)
|
||||
signStr := a.signParam(params)
|
||||
params[sigKey] = signStr
|
||||
//fullURL := utils.GenerateGetURL(prodURL, action, nil)
|
||||
err = platformapi.AccessPlatformAPIWithRetry(a.client,
|
||||
func() *http.Request {
|
||||
var request *http.Request
|
||||
if !isPost {
|
||||
request, _ = http.NewRequest(http.MethodGet, utils.GenerateGetURL(prodURL, "", params), nil)
|
||||
} else {
|
||||
request, _ = http.NewRequest(http.MethodPost, prodURL, strings.NewReader(utils.Map2URLValues(params).Encode()))
|
||||
}
|
||||
return request
|
||||
},
|
||||
a.config,
|
||||
func(response *http.Response, bodyStr string, jsonResult1 map[string]interface{}) (errLevel string, err error) {
|
||||
if jsonResult1 == nil {
|
||||
return platformapi.ErrLevelRecoverableErr, fmt.Errorf("mapData is nil")
|
||||
}
|
||||
if err == nil {
|
||||
if jsonResult1["error_response"] != nil {
|
||||
errLevel = platformapi.ErrLevelGeneralFail
|
||||
err = utils.NewErrorCode(jsonResult1["error_response"].(map[string]interface{})["sub_msg"].(string), utils.Int64ToStr(utils.MustInterface2Int64(jsonResult1["error_response"].(map[string]interface{})["sub_code"])))
|
||||
baseapi.SugarLogger.Debugf("pdd AccessAPI failed, jsonResult1:%s", utils.Format4Output(jsonResult1, true))
|
||||
}
|
||||
retVal = jsonResult1
|
||||
}
|
||||
return errLevel, err
|
||||
})
|
||||
return retVal, err
|
||||
}
|
||||
|
||||
func (a *API) AccessStorePage(fullURL string, bizParams map[string]interface{}, isPost bool) (retVal map[string]interface{}, err error) {
|
||||
if a.GetCookieCount() == 0 {
|
||||
return nil, fmt.Errorf("需要设置Store Cookie才能使用此方法")
|
||||
}
|
||||
data, _ := json.Marshal(bizParams)
|
||||
err = platformapi.AccessPlatformAPIWithRetry(a.client,
|
||||
func() *http.Request {
|
||||
var request *http.Request
|
||||
if isPost {
|
||||
request, _ = http.NewRequest(http.MethodPost, fullURL, strings.NewReader(string(data)))
|
||||
request.Header.Set("Content-Type", "application/json;charset=utf-8")
|
||||
} else {
|
||||
request, _ = http.NewRequest(http.MethodGet, utils.GenerateGetURL(fullURL, "", bizParams), nil)
|
||||
}
|
||||
a.FillRequestCookies(request)
|
||||
return request
|
||||
},
|
||||
a.config,
|
||||
func(response *http.Response, bodyStr string, jsonResult1 map[string]interface{}) (errLevel string, err error) {
|
||||
if jsonResult1 == nil {
|
||||
return platformapi.ErrLevelRecoverableErr, fmt.Errorf("mapData is nil")
|
||||
}
|
||||
if strings.Contains(bodyStr, "阿里妈妈") || strings.Contains(bodyStr, "联系客服") {
|
||||
return platformapi.ErrLevelRecoverableErr, fmt.Errorf("cookie可能过期了!")
|
||||
}
|
||||
if err == nil {
|
||||
if jsonResult1["error_response"] != nil {
|
||||
errLevel = platformapi.ErrLevelGeneralFail
|
||||
err = utils.NewErrorCode(jsonResult1["error_response"].(map[string]interface{})["zh_desc"].(string), jsonResult1["error_response"].(map[string]interface{})["code"].(string))
|
||||
baseapi.SugarLogger.Debugf("pdd AccessAPI failed, jsonResult1:%s", utils.Format4Output(jsonResult1, true))
|
||||
}
|
||||
retVal = jsonResult1
|
||||
}
|
||||
return errLevel, err
|
||||
})
|
||||
return retVal, err
|
||||
}
|
||||
22
platformapi/pddapi/pdd_test.go
Normal file
22
platformapi/pddapi/pdd_test.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package pddapi
|
||||
|
||||
import (
|
||||
"git.rosy.net.cn/baseapi"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
var (
|
||||
api *API
|
||||
sugarLogger *zap.SugaredLogger
|
||||
)
|
||||
|
||||
func init() {
|
||||
logger, _ := zap.NewDevelopment()
|
||||
sugarLogger = logger.Sugar()
|
||||
baseapi.Init(sugarLogger)
|
||||
|
||||
api = New("f9d95aaf5856485eabf39588f69a86de", "fa40c1fe356eebc1376ace1d2380ed44e553c602")
|
||||
api.SetCookieWithStr(`
|
||||
t=948ba2dc83b4b34e02a9d87212e574a8; cna=A9cHGZw1jjgCAd3tly+uuYIG; account-path-guide-s1=true; 1172080007-payment-time=true; xlly_s=1; cookie2=1a1a1b2e0fdeaea7c73f238a708b2bd5; v=0; _tb_token_=7e385ebeee37e; alimamapwag=TW96aWxsYS81LjAgKFdpbmRvd3MgTlQgNi4xOyBXaW42NDsgeDY0KSBBcHBsZVdlYktpdC81MzcuMzYgKEtIVE1MLCBsaWtlIEdlY2tvKSBDaHJvbWUvOTAuMC40NDMwLjg1IFNhZmFyaS81MzcuMzY%3D; cookie32=5887051fc70ab30be7a2655652e82dc6; alimamapw=RkAFUwICU1JQXAA%2BBw0GVlcDAAYGBwIEXVYBAgZUBA9RUQNTAABQUQZXAQY%3D; cookie31=MTE3MjA4MDAwNyxxcTM2NTE3NjI4NixmZW5nLnNoaUByb3N5Lm5ldC5jbixUQg%3D%3D; login=Vq8l%2BKCLz3%2F65A%3D%3D; isg=BCEhHCMG5XUeJUmGx-aU4ApDMO07zpXAI2znAoP2HSiH6kG8yx6lkE8oSB7sOS34; l=eB_opK6mjKn7XdlbBOfanurza77OSIRYYuPzaNbMiOCPOw1B5qD5W615yR86C3GVh6DMR3-XtET6BeYBqQAonxvTabLUOWMmn; tfstk=cqS5BpaGtbcW084E4TwqzMaD6QtdwgM6dzOlFNn8Yx8HAC1cwcR6wP_n9mL9h
|
||||
`)
|
||||
}
|
||||
15
platformapi/pddapi/union.go
Normal file
15
platformapi/pddapi/union.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package pddapi
|
||||
|
||||
const (
|
||||
cthMediaID = 9180924964
|
||||
)
|
||||
|
||||
//创建推广位
|
||||
func (a *API) GoodsOPidGenerate(name string) (err error) {
|
||||
_, err = a.AccessAPI("pdd.ddk.goods.pid.generate", false, map[string]interface{}{
|
||||
"number": 1,
|
||||
"p_id_name_list": []string{"\"" + name + "\""},
|
||||
"media_id": cthMediaID,
|
||||
})
|
||||
return err
|
||||
}
|
||||
13
platformapi/pddapi/union_test.go
Normal file
13
platformapi/pddapi/union_test.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package pddapi
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGoodsOPidGenerate(t *testing.T) {
|
||||
err := api.GoodsOPidGenerate("冲天猴儿")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
//t.Log(utils.Format4Output(result, false))
|
||||
}
|
||||
@@ -6,7 +6,8 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
JxAdzoneID = 111339100149
|
||||
JxAdzoneID = 111339100149
|
||||
JxAdzoneName = "京西推广位"
|
||||
|
||||
TbElmActTypeBDH = 1
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user