123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- package addata
- import (
- "encoding/json"
- "fmt"
- "io/ioutil"
- "miads/adslib/graylog"
- "miads/adslib/utils"
- "net/http"
- "net/url"
- "strconv"
- )
- // 获取请求的信息
- func getRequestInfo(dsp utils.DspParam) (string,error) {
- ipdd := combineIpdd(dsp)
- url := fmt.Sprintf("http://delivery.maihehd.com/d/vast/3.0?pos=147&ip=%s&ipdd=%s", dsp.Ip, ipdd)
- client := &http.Client{}
- fmt.Printf("req url: %s\n", url)
- req, err := http.NewRequest("GET", url, nil)
- if err != nil {
- return "", err
- }
- resp, err := client.Do(req)
- if err != nil {
- return "", err
- }
- defer resp.Body.Close()
- body, err := ioutil.ReadAll(resp.Body)
- fmt.Printf("rsp body: %s\n", body)
- if err != nil {
- return "", err
- }
- graylog.LogApi("ads_mi_extra_res", string(body), "", "")
- return string(body), nil
- }
- // 组装ipdd
- func combineIpdd(dsp utils.DspParam) string {
- admd5 := utils.Md5(dsp.Androidid)
- ipdd := url.Values{}
- ipdd.Set("device_type", strconv.Itoa(0))
- ipdd.Set("os_version", dsp.OsVersion)
- ipdd.Set("os", strconv.Itoa(1))
- ipdd.Set("imei", dsp.OriginImei)
- ipdd.Set("immd5", dsp.RealMd5Imei)
- ipdd.Set("androidid", dsp.Androidid)
- ipdd.Set("admd5", admd5)
- ipdd.Set("mac", dsp.Mac)
- ipdd.Set("device_name", dsp.Model)
- ipdd.Set("sr", dsp.ScreenSize)
- return ipdd.Encode()
- }
- // 将xml转为array
- func GetXiaomiExtraAdsInfo(dsp utils.DspParam) {
- advertiser := "vast"
- xmlResult = getRequestInfo(dsp)
- try:
- soup = BeautifulSoup(xml_result, 'xml')
- except:
- raise gen.Return(({},[]))
- // 获取视频总时长duration
- try:
- duration_arr = str(soup.Duration.text).split(":")
- except:
- raise gen.Return(({},[]))
- video_all_time = int(duration_arr[0])*3600+int(duration_arr[1])*60+int(duration_arr[2])
- ### 获取点击率
- click_rate = common.my_int(soup.ClickRate.text)
- ads_ua = dsp_info.ua
- ### 获取md5_imei
- if dsp_info.replace_flag==0:
- md5_imei = hashlib.md5(dsp_info.imei).hexdigest()
- else:
- md5_imei = dsp_info.imei
- track_arr = soup.find_all("Tracking")
- action_infos = []
- percent_duration = int(video_all_time/4.0)
- click_rand_duration = random.randint(5,10)
- first_flag = 0
- second_flag = 0
- third_flag = 0
- start_urls = []
- complete_urls = []
- firstQuartile_urls = []
- midpoint_urls = []
- thirdQuartile_urls = []
- for track_info in track_arr:
- url = track_info.text
- url = url.replace('__IMEI__',md5_imei)
- event_name = track_info['event']
- if event_name == "start":
- start_urls.append(url)
- if event_name == "complete":
- complete_urls.append(url)
- if event_name == "firstQuartile":
- first_flag = 1
- firstQuartile_urls.append(url)
- if event_name == "midpoint":
- second_flag = 1
- midpoint_urls.append(url)
- if event_name == "thirdQuartile":
- third_flag = 1
- thirdQuartile_urls.append(url)
- ### 排序
- if len(start_urls):
- action_event_dict={}
- action_event_dict['type']="start"
- action_event_dict['duration']=percent_duration
- action_event_dict['urls']=start_urls
- action_event_dict['weight']=100
- action_infos.append(action_event_dict)
- if len(firstQuartile_urls):
- action_event_dict={}
- action_event_dict['type']="firstQuartile"
- action_event_dict['duration']=percent_duration
- action_event_dict['urls']=firstQuartile_urls
- action_event_dict['weight']=90
- action_event_dict['timer_flag']=1
- action_infos.append(action_event_dict)
- if len(midpoint_urls):
- action_event_dict={}
- action_event_dict['type']="midpoint"
- action_event_dict['duration']=percent_duration
- action_event_dict['urls']=midpoint_urls
- action_event_dict['weight']=80
- action_event_dict['timer_flag']=2
- action_infos.append(action_event_dict)
- if len(thirdQuartile_urls):
- action_event_dict={}
- action_event_dict['type']="thirdQuartile"
- action_event_dict['duration']=percent_duration
- action_event_dict['urls']=thirdQuartile_urls
- action_event_dict['weight']=70
- action_event_dict['timer_flag']=3
- action_infos.append(action_event_dict)
- if len(complete_urls):
- action_event_dict={}
- action_event_dict['type']="complete"
- action_event_dict['duration']=percent_duration
- action_event_dict['urls']=complete_urls
- action_event_dict['weight']=60
- action_infos.append(action_event_dict)
- action_infos = sorted(action_infos, key=itemgetter('weight'), reverse=True)
- ### 修正每个action的duration
- for action_info in action_infos:
- ### 控制view的时间
- if action_info['type']=="start" and first_flag==1:
- action_info['duration']=percent_duration
- elif action_info['type']=="start" and first_flag==0:
- action_info['duration']=video_all_time
- ### 控制第一个timer时间
- if action_info['type']=="firstQuartile" and action_info['timer_flag']==1 and second_flag==1:
- action_info['duration']=percent_duration
- elif action_info['type']=="firstQuartile" and action_info['timer_flag']==1 and second_flag==0:
- action_info['duration']=video_all_time-percent_duration
- ### 控制第二个timer时间
- if action_info['type']=='midpoint' and action_info['timer_flag']==2 and third_flag==1:
- action_info['duration']=percent_duration
- elif action_info['type']=='midpoint' and action_info['timer_flag']==2 and third_flag==0:
- action_info['duration']=video_all_time-percent_duration*2
- click_url = soup.find('ClickThrough').text
- click_arr = soup.find_all('ClickTracking')
- click_urls = []
- for click_info in click_arr:
- url = click_info.text
- click = url.replace('__IMEI__',md5_imei)
- click_urls.append(click)
- click_event_dict = {}
- has_click = 0
- if click:
- click_event_dict['type'] = "CLICK"
- click_event_dict['duration']=click_rand_duration
- click_event_dict['urls'] = click_urls
- ### 先组装click是否可以下发
- click_info_dict = combine_ads_action_infos(click_urls,advertiser,dsp_info,"CLICK",1,click_rate,click_rand_duration)
- if len(click_info_dict['urls']):
- has_click=1
- last_action_infos = []
- sum_duration = 0
- for action_info in action_infos:
- action_info_dict = combine_ads_action_infos(action_info['urls'],advertiser,dsp_info,action_info['type'],0,click_rate,action_info['duration'])
- sum_duration+=action_info['duration']
- last_action_infos.append(action_info_dict)
- if has_click and sum_duration>=click_rand_duration:
- last_action_infos.append(click_info_dict)
- redis_tools.set_ads_distribute_action_num(advertiser,'CLICK')
- break
- if len(last_action_infos):
- redis_tools.set_ads_request_num(advertiser)
- ads_item = {}
- ads_item['target_addition'] = last_action_infos
- ads_item['target'] = click_url
- ads_item['image_url'] = ""
- ads_item['duration'] = video_all_time
- ads_item['video_url'] = ""
- ads_item['user_agent'] = ads_ua
- raise gen.Return((ads_item,[]))
- }
|