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