jiantao 4 years ago
parent
commit
091ece388e
4 changed files with 27 additions and 13 deletions
  1. 5 0
      ads_handler.go
  2. 14 11
      adslib/addata/xiaomi.go
  3. 3 1
      adslib/device/device.go
  4. 5 1
      adslib/redis_data/redis_data.go

+ 5 - 0
ads_handler.go

@@ -170,6 +170,11 @@ func adsHandler(c *gin.Context) {
170 170
 		device.SetIpReqTime(ip, time.Now().Unix())
171 171
 		device.SetAdsTagLog(advertiser, request.ReqSource, "DS_FREQ_1", cityCode)
172 172
 	}
173
+
174
+	if adslib.GetConf().TestMode {
175
+		needControl = false
176
+	}
177
+
173 178
 	//### 检查是否是ip黑名单
174 179
 	isIpBlack, err := ads_checker.CheckBlackIp(ip)
175 180
 	if err != nil {

+ 14 - 11
adslib/addata/xiaomi.go

@@ -66,10 +66,10 @@ type AdData struct {
66 66
 	TargetAddition []AdAction `json:"target_addition,omitempty"`
67 67
 	Target         string     `json:"target,omitempty"`
68 68
 	ImageUrl       string     `json:"image_url,omitempty"`
69
-	Duration       int64      `json:"duration"`
69
+	Duration       int64      `json:"duration,omitempty"`
70 70
 	VideoUrl       string     `json:"video_url,omitempty"`
71 71
 	JsOrderId      int64      `json:"js_order_id,omitempty"`
72
-	UserAgent      string     `json:"user_agent, omitempty"`
72
+	UserAgent      string     `json:"user_agent,omitempty"`
73 73
 	DpReport       string     `json:"dp_report,omitempty"`
74 74
 	Dp             string     `json:"dp,omitempty"`
75 75
 	OrderName      string     `json:"order_name,omitempty"`
@@ -112,19 +112,17 @@ func GetAdsInfos(dsp *utils.DspParam, advertiser string) (*AdData, error) {
112 112
 	}
113 113
 
114 114
 	client := &http.Client{}
115
-	fmt.Printf("req url: %s\n", reqUrl)
115
+	log.WithField("request_id", dsp.RequestId).Infof("req url: %s", reqUrl)
116 116
 	req, err := http.NewRequest("GET", reqUrl, nil)
117 117
 	if err != nil {
118 118
 		return nil, err
119 119
 	}
120 120
 
121
-	req.Header.Set(",authority", "m.video.xiaomi.com")
122
-	req.Header.Set(",method", "GET")
123
-	req.Header.Set(",path", reqUrl[26:])
124
-	req.Header.Set(",scheme", "https")
121
+	req.Header.Set("authority", "m.video.xiaomi.com")
122
+	req.Header.Set("method", "GET")
123
+	req.Header.Set("path", reqUrl[26:])
124
+	req.Header.Set("scheme", "https")
125 125
 	req.Header.Set("content-type", "application/json")
126
-	req.Header.Set("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3")
127
-	req.Header.Set("accept-encoding", "gzip, deflate, br")
128 126
 	req.Header.Set("accept-language", "zh-CN,zh;q=0.9")
129 127
 	req.Header.Set("cache-control", "max-age=0")
130 128
 	req.Header.Set("upgrade-insecure-requests", "1")
@@ -132,17 +130,22 @@ func GetAdsInfos(dsp *utils.DspParam, advertiser string) (*AdData, error) {
132 130
 
133 131
 	resp, err := client.Do(req)
134 132
 	if err != nil {
133
+		log.WithField("request_id", dsp.RequestId).Errorf("http failed: %s", err)
135 134
 		return nil, err
136 135
 	}
137 136
 	defer resp.Body.Close()
138 137
 	body, err := ioutil.ReadAll(resp.Body)
139
-	fmt.Printf("rsp body: %s\n", body)
140 138
 	if err != nil {
139
+		log.WithField("request_id", dsp.RequestId).Errorf("read http body failed: %s", err)
141 140
 		return nil, err
142 141
 	}
142
+
143
+	log.WithField("request_id", dsp.RequestId).Infof("rsp: %s", string(body))
144
+
143 145
 	rsp := XiaoMiAdInfoRsp{}
144
-	err = json.Unmarshal(body, &rsp)
146
+	err = json.Unmarshal([]byte(string(body)), &rsp)
145 147
 	if err != nil {
148
+		log.WithField("request_id", dsp.RequestId).Errorf("unmarshal http rsp failed: %s, rsp: %s", err, string(body))
146 149
 		return nil, err
147 150
 	}
148 151
 

+ 3 - 1
adslib/device/device.go

@@ -110,9 +110,11 @@ func GetMiDeviceConf(ip string, randNum int, specialDeviceNum int) (*DeviceConf,
110 110
 	}
111 111
 
112 112
 	rspBytes, _ := redis.Bytes(rsp, err)
113
+	rspStr := strings.ReplaceAll(string(rspBytes), "u", "")
114
+	rspStr = strings.ReplaceAll(string(rspStr), "'", "\"")
113 115
 
114 116
 	d := DeviceConf{}
115
-	err = json.Unmarshal(rspBytes, &d)
117
+	err = json.Unmarshal([]byte(rspStr), &d)
116 118
 	if err != nil {
117 119
 		return nil, redisKey, err
118 120
 	}

+ 5 - 1
adslib/redis_data/redis_data.go

@@ -8,6 +8,7 @@ import (
8 8
 	"miads/adslib"
9 9
 	"miads/adslib/ads_redis"
10 10
 	"strconv"
11
+	"strings"
11 12
 	"time"
12 13
 )
13 14
 
@@ -54,9 +55,12 @@ func GetChannelFlag(reqSource string, business string) (ChannelFlag, error) {
54 55
 	}
55 56
 
56 57
 	rspBytes, _ := redis.Bytes(rsp, err)
58
+	rspStr := strings.ReplaceAll(string(rspBytes), "'", "\"")
59
+	rspStr = strings.ReplaceAll(rspStr, "L", "")
60
+	log.Info(rspStr)
57 61
 
58 62
 	channelFlag := ChannelFlag{}
59
-	err = json.Unmarshal(rspBytes, &channelFlag)
63
+	err = json.Unmarshal([]byte(rspStr), &channelFlag)
60 64
 	if err != nil {
61 65
 		return ChannelFlag{}, err
62 66
 	}