Пример #1
0
func PremiummadRequest(results chan models.JsonEntity, adspace models.Adspace, params map[string]string) {

	weight := adspace.Weight
	beginTime := time.Now().UnixNano()

	var buffer bytes.Buffer
	buffer.WriteString("http://ae.madserving.com/adcall/bidrequest?")
	buffer.WriteString("adspaceid=" + params["adspaceid"])
	buffer.WriteString("&adtype=" + params["adtype"])
	buffer.WriteString("&width=" + params["width"])
	buffer.WriteString("&height=" + params["height"])
	buffer.WriteString("&pkgname=" + params["pkgname"])
	buffer.WriteString("&conn=" + params["conn"])
	buffer.WriteString("&carrier=" + params["carrier"])
	buffer.WriteString("&os=" + params["os"])
	buffer.WriteString("&device=" + params["device"])
	buffer.WriteString("&bid=" + params["bid"])
	buffer.WriteString("&appname=" + params["appname"])
	buffer.WriteString("&apitype=4") //始终是4
	buffer.WriteString("&pcat=" + params["pcat"])
	buffer.WriteString("&osv=" + params["osv"])
	buffer.WriteString("&imei=" + params["imei"])
	buffer.WriteString("&wma=" + params["wma"])
	buffer.WriteString("&aid=" + params["aid"])
	buffer.WriteString("&aaid=" + params["aaid"])
	buffer.WriteString("&idfa=" + params["idfa"])
	buffer.WriteString("&oid=" + params["oid"])
	buffer.WriteString("&uid=" + params["uid"])
	buffer.WriteString("&ua=" + params["ua"])
	buffer.WriteString("&ip=" + params["ip"])
	buffer.WriteString("&pid=" + params["pid"])
	buffer.WriteString("&density=" + params["density"])
	buffer.WriteString("&media=" + params["media"])
	buffer.WriteString("&lon=" + params["lon"])
	buffer.WriteString("&lat=" + params["lat"])

	u := buffer.String()
	u = strings.Replace(u, " ", "", -1) //替换空格,goreq无法解析空格

	log.Debug(u)

	resp, err := goreq.Request{Timeout: time.Duration(adspace.Timeout) * time.Millisecond, Method: "GET", Uri: u}.Do()

	if err != nil {
		log.Error(err)
		results <- models.JsonEntity{Weight: weight, Returncode: system.CODE_502, TimeSpending: (time.Now().UnixNano() - beginTime) / 1000000}
		return
	}

	body := resp.Body

	defer body.Close()

	if resp.StatusCode != 200 {
		log.Warn("response status code : ", resp.StatusCode)
		results <- models.JsonEntity{Weight: weight, Returncode: system.CODE_201, TimeSpending: (time.Now().UnixNano() - beginTime) / 1000000}
		return
	}

	b, err := ioutil.ReadAll(body)

	if err != nil {
		log.Error(err)
		results <- models.JsonEntity{Weight: weight, Returncode: system.CODE_502, TimeSpending: (time.Now().UnixNano() - beginTime) / 1000000}
		return
	}

	var data map[string]interface{}
	if err := json.Unmarshal(b, &data); err == nil {

		log.Debug(string(b))

		//无广告
		if data[adspace.DemandAdspaceKey] == nil {
			results <- models.JsonEntity{Weight: weight, Returncode: system.CODE_201, TimeSpending: (time.Now().UnixNano() - beginTime) / 1000000}
			return
		}

		if ad, ok := data[adspace.DemandAdspaceKey].(map[string]interface{}); ok {
			//无广告,这个类型是float64
			if float64(ad["returncode"].(float64)) != 200 {
				results <- models.JsonEntity{Weight: weight, Returncode: system.CODE_201, TimeSpending: (time.Now().UnixNano() - beginTime) / 1000000}
				return
			}

			jsonEntity := models.JsonEntity{}

			jsonEntity.Returncode = system.CODE_200
			jsonEntity.Adspaceid = adspace.DemandAdspaceKey
			width, _ := strconv.Atoi(params["width"])
			jsonEntity.Width = width
			height, _ := strconv.Atoi(params["height"])
			jsonEntity.Height = height
			adtype, _ := strconv.Atoi(params["adtype"])
			jsonEntity.Adtype = adtype

			jsonEntity.Imgurl = string(ad["imgurl"].(string))
			jsonEntity.Clkkurl = string(ad["clickurl"].(string))
			jsonEntity.Title = string(ad["displaytitle"].(string))
			jsonEntity.Descrtiption = string(ad["displaytext"].(string))

			if trackings, ok := ad["thclkurl"].([]interface{}); ok {
				var clktrackings []string
				for _, track := range trackings {
					if t, ok := track.(string); ok {
						clktrackings = append(clktrackings, t)
					}
				}
				jsonEntity.Clktracking = clktrackings
			}
			if trackings, ok := ad["imgtracking"].([]interface{}); ok {
				var imptrackings []string
				for _, track := range trackings {
					if t, ok := track.(string); ok {
						imptrackings = append(imptrackings, t)
					}
				}
				jsonEntity.Imptracking = imptrackings
			}

			jsonEntity.Weight = weight
			jsonEntity.TimeSpending = (time.Now().UnixNano() - beginTime) / 1000000
			results <- jsonEntity
			return

		} else {
			results <- models.JsonEntity{Weight: weight, Returncode: system.CODE_201, TimeSpending: (time.Now().UnixNano() - beginTime) / 1000000}
			return
		}

	} else {
		log.Error(err, string(b))
		results <- models.JsonEntity{Weight: weight, Returncode: system.CODE_502, TimeSpending: (time.Now().UnixNano() - beginTime) / 1000000}
		return
	}

}
Пример #2
0
func BaiduRequest(results chan models.JsonEntity, adspace models.Adspace, params map[string]string) {

	weight := adspace.Weight
	beginTime := time.Now().UnixNano()

	mobadsRequest := &api.MobadsRequest{}

	mobadsRequest.RequestId = proto.String(params["reqid"])

	mobadsRequest.ApiVersion = &api.Version{Major: proto.Uint32(5), Minor: proto.Uint32(0), Micro: proto.Uint32(0)}

	mobadsRequest.App = &api.App{AppId: proto.String(adspace.DemandSecretKey), AppVersion: &api.Version{Major: proto.Uint32(5), Minor: proto.Uint32(0), Micro: proto.Uint32(0)}}

	//adslot
	width, _ := strconv.Atoi(params["width"])
	height, _ := strconv.Atoi(params["height"])
	mobadsRequest.Adslot = &api.AdSlot{AdslotId: proto.String(adspace.DemandAdspaceKey), AdslotSize: &api.Size{Width: proto.Uint32(uint32(width)), Height: proto.Uint32(uint32(height))}}

	//Network
	ip := params["ip"] //ip必填
	if ip == "" {
		log.Warn("baidu:ip is null")
		results <- models.JsonEntity{Weight: weight, Returncode: system.CODE_502, TimeSpending: (time.Now().UnixNano() - beginTime) / 1000000}
		return
	}

	var connectionType api.Network_ConnectionType
	conn := params["conn"]
	switch conn {
	case "1":
		connectionType = api.Network_WIFI
	case "2":
		connectionType = api.Network_CELL_2G
	case "3":
		connectionType = api.Network_CELL_3G
	case "4":
		connectionType = api.Network_CELL_4G
	default:
		connectionType = api.Network_CELL_UNKNOWN
	}

	var operatorType api.Network_OperatorType
	carrier := params["carrier"]
	switch carrier {
	case "1":
		operatorType = api.Network_CHINA_MOBILE
	case "2":
		operatorType = api.Network_CHINA_UNICOM
	case "3":
		operatorType = api.Network_CHINA_TELECOM
	default:
		operatorType = api.Network_UNKNOWN_OPERATOR
	}

	mobadsRequest.Network = &api.Network{Ipv4: &ip, ConnectionType: &connectionType, OperatorType: &operatorType}

	var osType api.Device_OsType
	var udid api.UdId

	os := params["os"]
	switch os {
	case system.OS_ANDROID:
		osType = api.Device_ANDROID
		aid := params["aid"]
		if matched, _ := regexp.MatchString("[0-9A-Za-z]{16}", aid); !matched {
			log.Warn("baidu:aid is not correct,", aid)
			results <- models.JsonEntity{Weight: weight, Returncode: system.CODE_302, TimeSpending: (time.Now().UnixNano() - beginTime) / 1000000}
			return
		}
		udid.AndroidId = proto.String(aid)

		imei := params["imei"]
		wma := params["wma"]
		if matched, _ := regexp.MatchString("[0-9a-fA-F]{14,15}", imei); matched {
			udid.Imei = proto.String(imei)
		} else if matched, _ := regexp.MatchString("[0-9a-fA-F]{2}:[0-9a-fA-F]{2}:[0-9a-fA-F]{2}:[0-9a-fA-F]{2}:[0-9a-fA-F]{2}:[0-9a-fA-F]{2}", wma); matched {
			udid.Mac = proto.String(wma)
		} else {
			log.Warn("baidu:imei or wma is null, ", imei, wma)
			results <- models.JsonEntity{Weight: weight, Returncode: system.CODE_302, TimeSpending: (time.Now().UnixNano() - beginTime) / 1000000}
			return
		}

	case system.OS_IOS:
		osType = api.Device_IOS

		idfa := params["idfa"]
		wma := params["wma"]
		if matched, _ := regexp.MatchString("[0-9a-fA-F]{14,15}", idfa); matched {
			udid.Idfa = proto.String(idfa)
		} else if matched, _ := regexp.MatchString("[0-9a-fA-F]{2}:[0-9a-fA-F]{2}:[0-9a-fA-F]{2}:[0-9a-fA-F]{2}:[0-9a-fA-F]{2}:[0-9a-fA-F]{2}", wma); matched {
			udid.Mac = proto.String(wma)
		} else {
			log.Warn("baidu:idfa or wma is null, ", idfa, wma)
			results <- models.JsonEntity{Weight: weight, Returncode: system.CODE_302, TimeSpending: (time.Now().UnixNano() - beginTime) / 1000000}
			return
		}
	default:
		log.Warn("baidu:os is not correct, ", os)
		results <- models.JsonEntity{Weight: weight, Returncode: system.CODE_302, TimeSpending: (time.Now().UnixNano() - beginTime) / 1000000}
		return
	}

	//osv
	var osVersion api.Version
	osv := strings.Split(params["osv"], ".")
	if len(osv) < 1 {
		log.Warn("baidu:osv is not correct, ", osv)
		results <- models.JsonEntity{Weight: weight, Returncode: system.CODE_302, TimeSpending: (time.Now().UnixNano() - beginTime) / 1000000}
		return
	}
	if len(osv) == 1 {
		major, _ := strconv.Atoi(osv[0])
		osVersion = api.Version{Major: proto.Uint32(uint32(major))}
	} else if len(osv) == 2 {
		major, _ := strconv.Atoi(osv[0])
		minor, _ := strconv.Atoi(osv[1])
		osVersion = api.Version{Major: proto.Uint32(uint32(major)), Minor: proto.Uint32(uint32(minor))}
	} else if len(osv) == 3 {
		major, _ := strconv.Atoi(osv[0])
		minor, _ := strconv.Atoi(osv[1])
		micro, _ := strconv.Atoi(osv[2])
		osVersion = api.Version{Major: proto.Uint32(uint32(major)), Minor: proto.Uint32(uint32(minor)), Micro: proto.Uint32(uint32(micro))}
	}

	//Vendor Model
	var vendor []byte
	var model []byte
	device := strings.Split(params["device"], " ")
	if len(device) < 1 {
		log.Warn("baidu:device is not correct, ", device)
		results <- models.JsonEntity{Weight: weight, Returncode: system.CODE_302, TimeSpending: (time.Now().UnixNano() - beginTime) / 1000000}
		return
	}
	if len(device) == 1 {
		vendor = []byte(device[0])
		model = []byte("others")
	} else if len(device) == 2 {
		vendor = []byte(device[0])
		model = []byte(device[1])
	}

	deviceType := api.Device_PHONE
	mobadsRequest.Device = &api.Device{DeviceType: &deviceType, OsType: &osType, Udid: &udid, OsVersion: &osVersion, Vendor: vendor, Model: model, ScreenSize: &api.Size{Width: proto.Uint32(uint32(width)), Height: proto.Uint32(uint32(height))}}

	var err error

	content, err := proto.Marshal(mobadsRequest)

	if err != nil {
		log.Warn("baidu:", err)
		results <- models.JsonEntity{Weight: weight, Returncode: system.CODE_501, TimeSpending: (time.Now().UnixNano() - beginTime) / 1000000}
		return
	}

	log.Debug(mobadsRequest.String())

	//---------------request----------------//

	resp, err := goreq.Request{Timeout: time.Duration(adspace.Timeout) * time.Millisecond, Method: "POST", Uri: "http://mobads.baidu.com/api_5", Body: content}.Do()

	if err != nil {
		log.Error("baidu:", err)
		results <- models.JsonEntity{Weight: weight, Returncode: system.CODE_501, TimeSpending: (time.Now().UnixNano() - beginTime) / 1000000}
		return
	}

	body, err := ioutil.ReadAll(resp.Body)
	defer resp.Body.Close()

	if err != nil {
		results <- models.JsonEntity{Weight: weight, Returncode: system.CODE_502, TimeSpending: (time.Now().UnixNano() - beginTime) / 1000000}
		return
	}

	if resp.StatusCode != 200 {
		log.Warn("baidu:StatusCode ", resp.StatusCode)
		results <- models.JsonEntity{Weight: weight, Returncode: system.CODE_201, TimeSpending: (time.Now().UnixNano() - beginTime) / 1000000}
		return
	}

	mobadsResponse := &api.MobadsResponse{}

	err = proto.Unmarshal(body, mobadsResponse)

	if err != nil {
		log.Error(err)
		results <- models.JsonEntity{Weight: weight, Returncode: system.CODE_502, TimeSpending: (time.Now().UnixNano() - beginTime) / 1000000}
		return
	}

	log.Debug(mobadsResponse.String())

	//无广告
	errorCode := mobadsResponse.ErrorCode

	if errorCode != nil && *errorCode != 0 {
		results <- models.JsonEntity{Weight: weight, Returncode: system.CODE_201, TimeSpending: (time.Now().UnixNano() - beginTime) / 1000000}
		return
	}
	if len(mobadsResponse.Ads) < 1 || mobadsResponse.Ads[0].MaterialMeta == nil {
		results <- models.JsonEntity{Weight: weight, Returncode: system.CODE_201, TimeSpending: (time.Now().UnixNano() - beginTime) / 1000000}
		return
	}

	//有广告
	materialMeta := mobadsResponse.Ads[0].MaterialMeta

	jsonEntity := models.JsonEntity{}

	jsonEntity.Returncode = system.CODE_200
	jsonEntity.Adspaceid = adspace.DemandAdspaceKey
	w, _ := strconv.Atoi(params["width"])
	jsonEntity.Width = w
	h, _ := strconv.Atoi(params["height"])
	jsonEntity.Height = h
	adtype, _ := strconv.Atoi(params["adtype"])
	jsonEntity.Adtype = adtype

	jsonEntity.Clkkurl = *materialMeta.ClickUrl

	if len(materialMeta.ImageSrc) >= 1 {
		jsonEntity.Imgurl = materialMeta.ImageSrc[0]
	}
	if materialMeta.Title != nil {
		jsonEntity.Title = string(materialMeta.Title)
	}
	if len(materialMeta.Description) >= 1 {
		jsonEntity.Descrtiption = string(materialMeta.Description[0])
	}
	jsonEntity.Imptracking = materialMeta.WinNoticeUrl

	jsonEntity.Weight = weight
	jsonEntity.TimeSpending = (time.Now().UnixNano() - beginTime) / 1000000
	results <- jsonEntity
	return

}
Пример #3
0
func TencentRequest(results chan models.JsonEntity, adspace models.Adspace, params map[string]string) {

	weight := adspace.Weight
	beginTime := time.Now().UnixNano()

	url := "http://mi.gdt.qq.com/gdt_mview.fcg?adposcount=1&count=1&charset=utf8&datafmt=json"
	url += "&posid=" + adspace.DemandAdspaceKey
	url += "&posw=" + params["width"]
	url += "&posh=" + params["height"]

	ext := make(map[string]interface{})
	ext_req := make(map[string]interface{})

	ext_req["apiver"] = "1.6"
	ext_req["appid"] = adspace.DemandSecretKey

	switch params["os"] {
	case system.OS_ANDROID:
		ext_req["c_os"] = "android"
		ext_req["muidtype"] = 1
		if params["imei"] == "" {
			results <- models.JsonEntity{Weight: weight, Returncode: system.CODE_302, TimeSpending: (time.Now().UnixNano() - beginTime) / 1000000}
			return
		}
		ext_req["muid"] = utils.Md5(params["imei"])
	case system.OS_IOS:
		ext_req["c_os"] = "ios"
		ext_req["muidtype"] = 2
		if params["idfa"] == "" {
			results <- models.JsonEntity{Weight: weight, Returncode: system.CODE_302, TimeSpending: (time.Now().UnixNano() - beginTime) / 1000000}
			return
		}
		ext_req["muid"] = utils.Md5(params["idfa"])
	default:
		results <- models.JsonEntity{Weight: weight, Returncode: system.CODE_302, TimeSpending: (time.Now().UnixNano() - beginTime) / 1000000}
		return
	}

	switch params["conn"] {
	case "1":
		ext_req["conn"] = 1
	case "2":
		ext_req["conn"] = 2
	case "3":
		ext_req["conn"] = 3
	case "4":
		ext_req["conn"] = 4
	default:
		ext_req["conn"] = 0
	}
	switch params["carrier"] {
	case "1":
		ext_req["carrier"] = 1
	case "2":
		ext_req["carrier"] = 2
	case "3":
		ext_req["carrier"] = 3
	default:
		ext_req["carrier"] = 0
	}

	ext_req["c_w"] = params["width"]
	ext_req["c_h"] = params["height"]

	if params["device"] == "" {
		results <- models.JsonEntity{Weight: weight, Returncode: system.CODE_302, TimeSpending: (time.Now().UnixNano() - beginTime) / 1000000}
		return
	}
	ext_req["c_device"] = params["device"]

	if params["pkgname"] == "" {
		results <- models.JsonEntity{Weight: weight, Returncode: system.CODE_302, TimeSpending: (time.Now().UnixNano() - beginTime) / 1000000}
		return
	}
	ext_req["c_pkgname"] = params["pkgname"]

	switch params["adtype"] {
	case "2":
		ext_req["postype"] = 1
	case "5":
		ext_req["postype"] = 2
	case "6":
		ext_req["postype"] = 4
	default:
		results <- models.JsonEntity{Weight: weight, Returncode: system.CODE_302, TimeSpending: (time.Now().UnixNano() - beginTime) / 1000000}
		return
	}

	ext["req"] = ext_req

	b, err := json.Marshal(ext)

	if err != nil {
		results <- models.JsonEntity{Weight: weight, Returncode: system.CODE_502, TimeSpending: (time.Now().UnixNano() - beginTime) / 1000000}
		return
	}

	url += string(b)

	//---------------request----------------//

	resp, err := goreq.Request{Timeout: time.Duration(adspace.Timeout) * time.Millisecond, Method: "GET", Uri: url}.Do()

	if err != nil {
		log.Error(err)
		results <- models.JsonEntity{Weight: weight, Returncode: system.CODE_501, TimeSpending: (time.Now().UnixNano() - beginTime) / 1000000}
		return
	}

	body, err := ioutil.ReadAll(resp.Body)
	defer resp.Body.Close()

	if err != nil {
		log.Error(err)
		results <- models.JsonEntity{Weight: weight, Returncode: system.CODE_502, TimeSpending: (time.Now().UnixNano() - beginTime) / 1000000}
		return
	}

	if resp.StatusCode != 200 {
		results <- models.JsonEntity{Weight: weight, Returncode: system.CODE_201, TimeSpending: (time.Now().UnixNano() - beginTime) / 1000000}
		return
	}

	var data map[string]interface{}

	err = json.Unmarshal(body, &data)

	if err != nil {
		results <- models.JsonEntity{Weight: weight, Returncode: system.CODE_502, TimeSpending: (time.Now().UnixNano() - beginTime) / 1000000}
		return
	}

	ret := data["ret"]

	if ret == nil || ret != 0 {
		results <- models.JsonEntity{Weight: weight, Returncode: system.CODE_201, TimeSpending: (time.Now().UnixNano() - beginTime) / 1000000}
		return
	}

	if lists, ok := data[adspace.DemandAdspaceKey].(map[string][]interface{}); ok {
		//有广告
		list := lists["list"]

		if len(list) < 1 {
			results <- models.JsonEntity{Weight: weight, Returncode: system.CODE_201, TimeSpending: (time.Now().UnixNano() - beginTime) / 1000000}
			return
		}

		if ad, ok := list[0].(map[string]interface{}); ok {

			jsonEntity := models.JsonEntity{}

			jsonEntity.Returncode = system.CODE_200
			jsonEntity.Adspaceid = adspace.DemandAdspaceKey
			width, _ := strconv.Atoi(params["width"])
			jsonEntity.Width = width
			height, _ := strconv.Atoi(params["height"])
			jsonEntity.Height = height
			adtype, _ := strconv.Atoi(params["adtype"])
			jsonEntity.Adtype = adtype

			jsonEntity.Imgurl = string(ad["imgurl"].(string))
			jsonEntity.Clkkurl = string(ad["clickurl"].(string))
			jsonEntity.Title = string(ad["displaytitle"].(string))
			jsonEntity.Descrtiption = string(ad["displaytext"].(string))

			if trackings, ok := ad["thclkurl"].([]interface{}); ok {
				var clktrackings []string
				for _, track := range trackings {
					if t, ok := track.(string); ok {
						clktrackings = append(clktrackings, t)
					}
				}
				jsonEntity.Clktracking = clktrackings
			}
			if trackings, ok := ad["imgtracking"].([]interface{}); ok {
				var imptrackings []string
				for _, track := range trackings {
					if t, ok := track.(string); ok {
						imptrackings = append(imptrackings, t)
					}
				}
				jsonEntity.Imptracking = imptrackings
			}

			jsonEntity.Weight = weight
			jsonEntity.TimeSpending = (time.Now().UnixNano() - beginTime) / 1000000
			results <- jsonEntity
			return
		}

	} else {
		results <- models.JsonEntity{Weight: weight, Returncode: system.CODE_201, TimeSpending: (time.Now().UnixNano() - beginTime) / 1000000}
		return
	}

}