xiaomi_extra.go 6.3 KB

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