示例#1
0
    def GetReport(self, strTime, accountInfo):
            try:
                if accountInfo[0] == "baidu-短语商家2140114":
                    self.password = "******"
                else:
                    self.password = "******"

                service = sms_v3_ReportService()
                service.setUsername(accountInfo[0])
                service.setPassword(self.password)
                service.setToken(accountInfo[1])
                strStartDate = strTime + 'T' + '00:00:00.000'
                strEndDate = strTime + 'T' + '23:59:59.000'
                print strStartDate, strEndDate
                request = {"reportRequestType":{"performanceData":["click","impression"], "startDate" : strStartDate, "endDate" : strEndDate, "levelOfDetails" : 11, "idOnly": 0, "reportType" : 14,"statRange":2,"attributes":None,"statIds":None,"unitOfTime":5, "device":0}}
                getProfessionalReportIdResp = service.getProfessionalReportId(request)
                getNum = 0
                while getProfessionalReportIdResp is None:
                        time.sleep(1)
                        if(getNum > 20):
                                break
                        getNum +=1
                        getProfessionalReportIdResp = service.getProfessionalReportId(request)

                if (getProfessionalReportIdResp == None):
                        print "Get Professional Report Id fail!"
                        return None
                getNum = 0
                while True :
                        print "Get the report id response \n"
                        time.sleep(5)
                        if(getNum > 24):
                                return ''
                        getNum +=1
                        reportId = {"reportId": getProfessionalReportIdResp["body"]["reportId"]};
                        getReportStateResp = service.getReportState(reportId)
                        getReportStatNum = 0
                        while getReportStateResp is None or getReportStateResp["body"] is None or getReportStateResp["body"]["isGenerated"] is None:
                            time.sleep(1)
                            if getReportStatNum > 10:
                                return None
                            getReportStatNum += 1
                            getReportStateResp = service.getReportState(reportId)

                        if (getReportStateResp["body"]["isGenerated"] == 3 ):
                                print "Get the report state success\n"
                                reportFilePath = service.getReportFileUrl(reportId)
                                reportFileNum =0
                                while reportFilePath is None or reportFilePath["body"] is None or reportFilePath["body"]["reportFilePath"] is None:
                                    if reportFileNum > 10:
                                        return 0
                                    reportFileNum += 1
                                    reportFilePath = service.getReportFileUrl(reportId)
                                url = reportFilePath["body"]["reportFilePath"]
                                f = urllib2.urlopen(url)
                                strText = f.read()
                                textLine = strText.split('\n')
                                i = 0
                                for info in textLine:
                                    if(i == 0):
                                        i = 1
                                        continue
                                    self.keywordshow.append(info.decode('gb2312'))
                                break
            except Exception, e:
                print e
                tb.print_exc()
def GetReport(startTime, endTime, startHour, endHour, accountInfo, passwdStr):
    """ 请求账户点击数据

    Args:
        startTime   : 开始时间
        endTime     : 结束时间
        startHour   :
        endHour     :
        accountInfo : 账户信息
        passwdStr   : 账户密码

    Returns:

    """
    try:
        if accountInfo[0] == "baidu-短语商家2140114":
            passwdStr = "Ab1234567890"
        else:
            passwdStr = "Nuomi20131127"

        service = sms_v3_ReportService()
        service.setUsername(accountInfo[0])
        service.setPassword(passwdStr)
        service.setToken(accountInfo[1])
        #service.setTarget(accountInfo[0])

        strStartDate = startTime + 'T' + '00:00:00.000'
        strEndDate = endTime + 'T' + '23:59:59.000'

        print '~~~~~~~~~~~  华丽的分割 ~~~~~~~~~~~~~~~~~~~'
        print accountInfo[0]            # 打印 帐户名
        print strStartDate, strEndDate  # 查询监控时间范围

        # 准备请求参数
        request = {
                "realTimeRequestTypes":{
                        "performanceData":["click", "impression"],
                        "startDate" : strStartDate,
                        "endDate" : strEndDate,
                        "levelOfDetails" : 2,
                        "reportType" : 2,
                        "statRange":2,
                        "statIds":None,
                        "unitOfTime":7,
                        "device":0
                }
        }
        getRealTimeReportResList = None     # API调用结果保存
        getNum = 0                          # API 重试调用次数
        MAX_RETRY = 2                       # 允许重试阈值,超过则报警
        SLEEP_TIME = 3                      # API调用重试时间间隔/秒

        while not IsValidRealTimeUserClickRet(getRealTimeReportResList):
            getNum += 1
            if(getNum > MAX_RETRY):
                break
            if getNum > 1 : time.sleep(SLEEP_TIME)
            # 调用API
            getRealTimeReportResList = service.getRealTimeData(request)

        print ''
        #print getRealTimeReportResList['header']
        print getRealTimeReportResList
        print ''

        if ( getNum > MAX_RETRY):
            print "Get Professional Report Id fail!\n"
            return '*'

        clickSum = 0
        time1 = startTime + " " + startHour
        time2 = endTime + " " + endHour
        flag = 0
        listInfo = getRealTimeReportResList['body']['realTimeResultTypes']
        for itemInfo in listInfo:
            if(cmp(itemInfo['date'], time1) == 0 or cmp(itemInfo['date'], time2) == 0):
                flag = 1
                clickSum += int(itemInfo['kpis'][0])

        if(flag == 1):
            return str(clickSum)
        else:
            return ''
    except Exception, e:
        print e
        tb.print_exc()