예제 #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 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
	}

}