jiantaoli 4 yıl önce
ebeveyn
işleme
f19a8e7650
3 değiştirilmiş dosya ile 240 ekleme ve 22 silme
  1. 5 0
      .idea/dictionaries/jiantaoli.xml
  2. 31 22
      ads_handler.go
  3. 204 0
      adslib/addata/xiaomi_extra.go

+ 5 - 0
.idea/dictionaries/jiantaoli.xml

@@ -1,8 +1,13 @@
1 1
 <component name="ProjectDictionaryState">
2 2
   <dictionary name="jiantaoli">
3 3
     <words>
4
+      <w>admd</w>
5
+      <w>androidid</w>
6
+      <w>immd</w>
7
+      <w>ipdd</w>
4 8
       <w>kuxin</w>
5 9
       <w>zhiku</w>
10
+      <w>ziyou</w>
6 11
     </words>
7 12
   </dictionary>
8 13
 </component>

+ 31 - 22
ads_handler.go

@@ -89,7 +89,7 @@ func adsHandler(c *gin.Context) {
89 89
 			Imei string
90 90
 			Model string
91 91
 			NetworkType int `json:"network_type"`
92
-			Provice string
92
+			Province string
93 93
 			City string
94 94
 			ScreeSize string `json:"screen_size"`
95 95
 			Ua string
@@ -102,7 +102,7 @@ func adsHandler(c *gin.Context) {
102 102
 			Imei:         request.Imei,
103 103
 			Model:        request.Model,
104 104
 			NetworkType:  request.NetworkType,
105
-			Provice:      ipInfo.Province,
105
+			Province:      ipInfo.Province,
106 106
 			City:         ipInfo.City,
107 107
 			ScreeSize:    request.ScreenSize,
108 108
 			Ua:           request.UaClient,
@@ -485,27 +485,36 @@ func adsHandler(c *gin.Context) {
485 485
 			if customAdData != nil {
486 486
 				shortMessage = "ads_tt_thirds"
487 487
 			}
488
-			if customAdData != nil && customAdData.OrderName  == "mh_dsp" && xiaomiResponseFlag==0 {
489
-				//redis_device.set_ads_tag_log(advertiser,req_source,'ADS_ZIYOU_MH',dsp_city_code)  # 广告获取成功
490
-				//#如果是外部dsp,走这个逻辑
491
-				//from thirds import xiaomi_extra
492
-				//extra_item,ads_item = yield xiaomi_extra.get_ads_infos(dsp_info)
493
-				//short_message = "ads_vast_response"
488
+
489
+			if customAdData != nil && (shortMessage ==  "ads_vast_response" || shortMessage == "ads_tt_thirds") {
490
+				//response_data_report = {}
491
+				//response_data_report['ip'] = ads_ip
492
+				//response_data_report['short_message'] =  short_message
493
+				//response_data_report['req_source'] = req_source
494
+				//response_data_report['response_vast'] = json.dumps(extra_item)
495
+				//response_data_report['order_name']=order_name
496
+				//response_data_report['advertiser']=advertiser
497
+				//log.send_to_graylog_tornado(gelf_data=response_data_report)
498
+
499
+				adDataBytes,  err :=
500
+				grayLogData := struct {
501
+					Ip string
502
+					ShortMessage string `json:"short_message"`
503
+					ReqSource string `json:"req_source"`
504
+					ResponseVast string `json:"response_vast"`
505
+					OrderName string `json:"order_name"`
506
+					Advertiser string `json:"advertiser"`
507
+				}{
508
+					Ip:           ip,
509
+					ShortMessage: "ads_api_log",
510
+					ReqSource:    "zhiku",
511
+					ResponseVast: ""
512
+					OrderName: customAdData.OrderName,
513
+					Advertiser:  advertiser,
514
+				}
515
+
516
+				graylog.Log(grayLogData)
494 517
 			}
495
-			//if extra_item and extra_item['order_name']=="xiaodu_dsp" and xiaomi_response_flag==0:
496
-			//redis_device.set_ads_tag_log(advertiser,req_source,'ADS_ZIYOU_XIAODU',dsp_city_code)  # 广告获取成功
497
-			//from thirds import xiaodu
498
-			//extra_item,_ = yield xiaodu.get_ads_infos(dsp_info)
499
-			//short_message = "ads_xiaodu_response"
500
-			//if extra_item is not None and short_message in ['ads_xiaodu_response','ads_vast_response','ads_tt_thirds']:
501
-			//response_data_report = {}
502
-			//response_data_report['ip'] = ads_ip
503
-			//response_data_report['short_message'] =  short_message
504
-			//response_data_report['req_source'] = req_source
505
-			//response_data_report['response_vast'] = json.dumps(extra_item)
506
-			//response_data_report['order_name']=order_name
507
-			//response_data_report['advertiser']=advertiser
508
-			//log.send_to_graylog_tornado(gelf_data=response_data_report)
509 518
 			//if short_message=="ads_tt_thirds":
510 519
 			//from thirds import thirds_mix_xiaomi
511 520
 			//extra_item = thirds_mix_xiaomi.mix_last_infos(extra_item,response)

+ 204 - 0
adslib/addata/xiaomi_extra.go

@@ -0,0 +1,204 @@
1
+package addata
2
+
3
+import (
4
+	"encoding/json"
5
+	"fmt"
6
+	"io/ioutil"
7
+	"miads/adslib/graylog"
8
+	"miads/adslib/utils"
9
+	"net/http"
10
+	"net/url"
11
+	"strconv"
12
+)
13
+
14
+// 获取请求的信息
15
+func getRequestInfo(dsp utils.DspParam) (string,error) {
16
+	ipdd := combineIpdd(dsp)
17
+	url := fmt.Sprintf("http://delivery.maihehd.com/d/vast/3.0?pos=147&ip=%s&ipdd=%s", dsp.Ip, ipdd)
18
+
19
+	client := &http.Client{}
20
+	fmt.Printf("req url: %s\n", url)
21
+	req, err := http.NewRequest("GET",  url, nil)
22
+	if err != nil {
23
+		return "", err
24
+	}
25
+
26
+	resp, err := client.Do(req)
27
+	if err != nil {
28
+		return "", err
29
+	}
30
+	defer resp.Body.Close()
31
+	body, err := ioutil.ReadAll(resp.Body)
32
+	fmt.Printf("rsp body: %s\n", body)
33
+	if err != nil {
34
+		return "", err
35
+	}
36
+
37
+	graylog.LogApi("ads_mi_extra_res", string(body), "", "")
38
+	return string(body), nil
39
+}
40
+
41
+// 组装ipdd
42
+func combineIpdd(dsp utils.DspParam) string {
43
+	admd5 := utils.Md5(dsp.Androidid)
44
+	ipdd := url.Values{}
45
+	ipdd.Set("device_type", strconv.Itoa(0))
46
+	ipdd.Set("os_version", dsp.OsVersion)
47
+	ipdd.Set("os", strconv.Itoa(1))
48
+	ipdd.Set("imei", dsp.OriginImei)
49
+	ipdd.Set("immd5", dsp.RealMd5Imei)
50
+	ipdd.Set("androidid", dsp.Androidid)
51
+	ipdd.Set("admd5", admd5)
52
+	ipdd.Set("mac", dsp.Mac)
53
+	ipdd.Set("device_name", dsp.Model)
54
+	ipdd.Set("sr", dsp.ScreenSize)
55
+
56
+	return ipdd.Encode()
57
+}
58
+
59
+// 将xml转为array
60
+func GetXiaomiExtraAdsInfo(dsp utils.DspParam) {
61
+	advertiser := "vast"
62
+	xmlResult = getRequestInfo(dsp)
63
+try:
64
+	soup = BeautifulSoup(xml_result, 'xml')
65
+except:
66
+	raise gen.Return(({},[]))
67
+	// 获取视频总时长duration
68
+try:
69
+	duration_arr = str(soup.Duration.text).split(":")
70
+except:
71
+	raise gen.Return(({},[]))
72
+	video_all_time = int(duration_arr[0])*3600+int(duration_arr[1])*60+int(duration_arr[2])
73
+	### 获取点击率
74
+	click_rate = common.my_int(soup.ClickRate.text)
75
+	ads_ua = dsp_info.ua
76
+	### 获取md5_imei
77
+	if dsp_info.replace_flag==0:
78
+	md5_imei = hashlib.md5(dsp_info.imei).hexdigest()
79
+	else:
80
+	md5_imei = dsp_info.imei
81
+	track_arr = soup.find_all("Tracking")
82
+	action_infos = []
83
+	percent_duration = int(video_all_time/4.0)
84
+	click_rand_duration = random.randint(5,10)
85
+	first_flag = 0
86
+	second_flag = 0
87
+	third_flag = 0
88
+	start_urls = []
89
+	complete_urls = []
90
+	firstQuartile_urls = []
91
+	midpoint_urls = []
92
+	thirdQuartile_urls = []
93
+	for track_info in track_arr:
94
+	url = track_info.text
95
+	url = url.replace('__IMEI__',md5_imei)
96
+	event_name = track_info['event']
97
+	if event_name == "start":
98
+	start_urls.append(url)
99
+	if event_name == "complete":
100
+	complete_urls.append(url)
101
+	if event_name == "firstQuartile":
102
+	first_flag = 1
103
+	firstQuartile_urls.append(url)
104
+	if event_name == "midpoint":
105
+	second_flag = 1
106
+	midpoint_urls.append(url)
107
+	if event_name == "thirdQuartile":
108
+	third_flag = 1
109
+	thirdQuartile_urls.append(url)
110
+	### 排序
111
+	if len(start_urls):
112
+	action_event_dict={}
113
+	action_event_dict['type']="start"
114
+	action_event_dict['duration']=percent_duration
115
+	action_event_dict['urls']=start_urls
116
+	action_event_dict['weight']=100
117
+	action_infos.append(action_event_dict)
118
+	if len(firstQuartile_urls):
119
+	action_event_dict={}
120
+	action_event_dict['type']="firstQuartile"
121
+	action_event_dict['duration']=percent_duration
122
+	action_event_dict['urls']=firstQuartile_urls
123
+	action_event_dict['weight']=90
124
+	action_event_dict['timer_flag']=1
125
+	action_infos.append(action_event_dict)
126
+	if len(midpoint_urls):
127
+	action_event_dict={}
128
+	action_event_dict['type']="midpoint"
129
+	action_event_dict['duration']=percent_duration
130
+	action_event_dict['urls']=midpoint_urls
131
+	action_event_dict['weight']=80
132
+	action_event_dict['timer_flag']=2
133
+	action_infos.append(action_event_dict)
134
+	if len(thirdQuartile_urls):
135
+	action_event_dict={}
136
+	action_event_dict['type']="thirdQuartile"
137
+	action_event_dict['duration']=percent_duration
138
+	action_event_dict['urls']=thirdQuartile_urls
139
+	action_event_dict['weight']=70
140
+	action_event_dict['timer_flag']=3
141
+	action_infos.append(action_event_dict)
142
+	if len(complete_urls):
143
+	action_event_dict={}
144
+	action_event_dict['type']="complete"
145
+	action_event_dict['duration']=percent_duration
146
+	action_event_dict['urls']=complete_urls
147
+	action_event_dict['weight']=60
148
+	action_infos.append(action_event_dict)
149
+	action_infos = sorted(action_infos, key=itemgetter('weight'), reverse=True)
150
+	### 修正每个action的duration
151
+	for action_info in action_infos:
152
+	### 控制view的时间
153
+	if action_info['type']=="start" and first_flag==1:
154
+	action_info['duration']=percent_duration
155
+	elif action_info['type']=="start" and first_flag==0:
156
+	action_info['duration']=video_all_time
157
+	### 控制第一个timer时间
158
+	if action_info['type']=="firstQuartile" and action_info['timer_flag']==1 and second_flag==1:
159
+	action_info['duration']=percent_duration
160
+	elif action_info['type']=="firstQuartile" and action_info['timer_flag']==1 and second_flag==0:
161
+	action_info['duration']=video_all_time-percent_duration
162
+	### 控制第二个timer时间
163
+	if action_info['type']=='midpoint' and action_info['timer_flag']==2 and third_flag==1:
164
+	action_info['duration']=percent_duration
165
+	elif action_info['type']=='midpoint' and action_info['timer_flag']==2 and third_flag==0:
166
+	action_info['duration']=video_all_time-percent_duration*2
167
+	click_url = soup.find('ClickThrough').text
168
+	click_arr = soup.find_all('ClickTracking')
169
+	click_urls = []
170
+	for click_info in click_arr:
171
+	url = click_info.text
172
+	click = url.replace('__IMEI__',md5_imei)
173
+	click_urls.append(click)
174
+	click_event_dict = {}
175
+	has_click = 0
176
+	if click:
177
+	click_event_dict['type'] = "CLICK"
178
+	click_event_dict['duration']=click_rand_duration
179
+	click_event_dict['urls'] = click_urls
180
+	### 先组装click是否可以下发
181
+	click_info_dict = combine_ads_action_infos(click_urls,advertiser,dsp_info,"CLICK",1,click_rate,click_rand_duration)
182
+	if len(click_info_dict['urls']):
183
+	has_click=1
184
+	last_action_infos = []
185
+	sum_duration = 0
186
+	for action_info in action_infos:
187
+	action_info_dict = combine_ads_action_infos(action_info['urls'],advertiser,dsp_info,action_info['type'],0,click_rate,action_info['duration'])
188
+	sum_duration+=action_info['duration']
189
+	last_action_infos.append(action_info_dict)
190
+	if has_click and sum_duration>=click_rand_duration:
191
+	last_action_infos.append(click_info_dict)
192
+	redis_tools.set_ads_distribute_action_num(advertiser,'CLICK')
193
+	break
194
+	if len(last_action_infos):
195
+	redis_tools.set_ads_request_num(advertiser)
196
+	ads_item = {}
197
+	ads_item['target_addition'] = last_action_infos
198
+	ads_item['target'] = click_url
199
+	ads_item['image_url'] = ""
200
+	ads_item['duration'] = video_all_time
201
+	ads_item['video_url'] = ""
202
+	ads_item['user_agent'] = ads_ua
203
+	raise gen.Return((ads_item,[]))
204
+}