Пример #1
0
def make_geo_targets(rows):
    try:
        document = AdDocument()
        for row in rows:
            geo_type = row["geoType"].lower()
            docgeo_type = None
            if geo_type == GEOType.NATIONAL.value:
                docgeo_type = AdDocument.Geotarget.NATIONAL
            elif geo_type == GEOType.STATE.value:
                docgeo_type = AdDocument.Geotarget.STATE
            elif geo_type == GEOType.DMA.value:
                docgeo_type = AdDocument.Geotarget.DMA
            elif geo_type == GEOType.CITY.value:
                docgeo_type = AdDocument.Geotarget.CITY
            elif geo_type == GEOType.ZIP.value:
                docgeo_type = AdDocument.Geotarget.ZIP
            print "geo_type  KKKKKK"
            print docgeo_type
            if docgeo_type is not None:
                geodoc = add_geo_target(docgeo_type,
                                        city_=row["city"],
                                        state_=row["state"],
                                        zip_=row["zip"],
                                        dma_=row["dma"],
                                        country_=row["country"])
                print geodoc
                if geodoc:
                    document.MergeFrom(geodoc)
        return document
    except Exception as e:
        print e
        return None
Пример #2
0
def make_creatives(rows):
    try:
        document = AdDocument()
        for row in rows:
            creative_doc = add_creative(row["api"], row["attributes"], row["mime"], row["video_duration"], row["video_protocol"])
            if creative_doc:
                document.MergeFrom(creative_doc)
        return document
    except Exception as e:
        print e
        return None
Пример #3
0
def make_target_profiles(rows):
    try:
        document = AdDocument()
        for row in rows:
            tp_doc = add_target_profile(row["targetType"], row["constraints"], row["exclude"])
            if tp_doc:
                document.MergeFrom(tp_doc)
        return document
    except Exception as e:
        print e
        return None
Пример #4
0
def add_geo_target(target_type,
                   city_=None,
                   state_=None,
                   zip_=None,
                   dma_=None,
                   country_=None):
    try:
        document = AdDocument()
        geo_target = document.geotargets.add()

        if is_valid_geo_target_type(target_type):
            geo_target.geotarget_type = target_type

        if city_:
            geo_target.city = str(city_)

        if state_:
            geo_target.state = str(state_)

        if zip_:
            geo_target.zip = str(zip_)

        if dma_:
            geo_target.dma = str(dma_)

        if country_:
            geo_target.country = str(country_)
        return document
    except Exception as e:
        print e
        return
Пример #5
0
def add_creative(api=None,attr = None, mime = None, video_duration = None, video_protocol = None):

    try:
        document = AdDocument()
        creative = document.creatives.add()
        if api is not None:
            if  isinstance(api, list):
                creative.api = ",".join(api)
            else:
                creative.api = str(api)
        if attr is not None:
            if isinstance(attr, list):
                creative.attr = ",".join(attr)
            else:
                creative.attr = str(attr)
        if mime is not None:
            if isinstance(mime, list):
                creative.mime = ",".join(mime)
            else:
                creative.mime = str(mime)
        if video_duration is not None:
            if isinstance(video_duration, list):
                creative.video_duration = ",".join(video_duration)
            else:
                creative.video_duration = str(video_duration)
        if video_protocol is not None:
            if isinstance(video_protocol, list):
                creative.video_protocol = ",".join(video_protocol)
            else:
                creative.video_protocol = (video_protocol)

        return document
    except Exception as e:
        print e
        return None
Пример #6
0
def make_geo_targets(rows):
    try:
        document = AdDocument()
        for row in rows:
            geo_type = row["geoType"].lower()
            if geo_type == GEOType.NATIONAL.value:
                country_ = add_geo_target(AdDocument.Geotarget.NATIONAL,
                                          country_=row["country"])
                if country_:
                    document.MergeFrom(country_)
            elif geo_type == GEOType.STATE.value:
                state_ = add_geo_target(AdDocument.Geotarget.STATE,
                                        state_=row["state"])
                if state_:
                    document.MergeFrom(state_)
            elif geo_type == GEOType.DMA.value:
                dma_ = add_geo_target(AdDocument.Geotarget.DMA,
                                      dma_=row["dma"])
                if dma_:
                    document.MergeFrom(dma_)
            elif geo_type == GEOType.CITY.value:
                city_ = add_geo_target(AdDocument.Geotarget.CITY,
                                       city_=row["city"])
                if city_:
                    document.MergeFrom(city_)
            elif geo_type == GEOType.ZIP.value:
                zip_ = add_geo_target(AdDocument.Geotarget.ZIP,
                                      zip_=row["zip"])
                if zip_:
                    document.MergeFrom(zip_)
        return document
    except Exception as e:
        print e
        return None
Пример #7
0
def add_target_profile(target_type, constraints, exclude=False):

    try:
        document = AdDocument()
        target_profile = document.target_profiles.add()
        if is_valid_target_profile_type(target_type):
            target_profile.target_type = target_type

        if isinstance(constraints, list):
            target_profile.constraints = ", ".join(constraints)
        else:
            target_profile.constraints = str(constraints)

        if exclude:
            target_profile.exclude = exclude

        return document
    except Exception as e:
        print e
        return
Пример #8
0
def make_ad_document(campaignId, adgroupId, conn):
    isAdgroupPresent = False
    try:
        document = AdDocument()
        rows = getInfoFromDB(adgroupId, sql_adgroup, conn)
        row = None
        if rows:
            row = rows[0]

        if row is not None:
            isAdgroupPresent = True
        if isAdgroupPresent:
            document.tenant_id = row["tenant_id"] if row["tenant_id"] else 0  #1
            document.campaign_id = long(
                campaignId) if campaignId != "0" else long(
                    row["campaignId"])  #2
            document.adgroup_id = long(
                adgroupId) if adgroupId != "0" else 0  #3
            document.adomain = "rti.com"  #4   ??
            # 5  6  TBD
            document.banner_size = row["banner_size"] if row[
                "banner_size"] else ""  #7
            document.creative_type = row["creative_type"] if row[
                "creative_type"] else ""  #8
            document.instl = bool(row["instl"]) if row["instl"] else False  #9
            document.proximity_mode = row["proximity_mode"] if row[
                "proximity_mode"] else ""  #10
            creatives = None  #11
            target_profiles = None  #12
            geo_targets = None  #13
            publishers = None  #14
            document.status = row["status_flag"] if row[
                "status_flag"] else 6  #15
            setattr(document, 'del', bool(row["del"]))  #16
            document.rti_mode = AdDocument.BATCH  #17
            document.adv_bid_rates = row["bid_rate"] if row[
                "bid_rate"] else 0.0  #18
            document.is_secure = bool(
                row["is_secure"]) if row["is_secure"] else False  #19
            document.session_id = randint(0, 1000000)  #20
            document.pub_bid_rates = 0.0  #21
            document.kpi_ctr = row["kpi_ctr"] if row["kpi_ctr"] else 0.0  #22
            document.kpi_sar = row["kpi_sar"] if row["kpi_sar"] else 0.0  #23
            #24  TBD

        if isAdgroupPresent:
            #get targetprofile info from db
            targetprofiles_rows = getInfoFromDB(adgroupId, sql_targetprofiles,
                                                conn)
            if targetprofiles_rows:
                target_profiles = make_target_profiles(targetprofiles_rows)

            if target_profiles:
                document.MergeFrom(target_profiles)

            #get geotarget info from db
            geotargets_rows = getInfoFromDB(adgroupId, sql_geotargets, conn)
            '''
            conn_marketplace = getConnectWithDB(dbconfig_marketplace, "marketplace_pool")
            if geotargets_rows:
                insert_rows = []
                for row in geotargets_rows:
                    if row["lat"] and row["lng"] and row["radius"]:
                        data = {
                                "id" : row["gtId"],
                                "tenant_id" : row["tenant_id"],
                                "study_id" : document.session_id,
                                "address1" :  row["address1"] ,
                                "address2":   row["address2"],
                                "city":      row["city"],
                                "state":      row["state"],
                                "country":     row["country"],
                                "zipcode":      row["zip"] ,
                                "latittude":      row["lat"],
                                "longitude":      row["lng"],
                                "type":      row["geoType"],
                                "adgroup":      row["adGroup_id"],
                                "dma_code":      row["dma"],
                                "radius":      row["radius"]
                                }
                        insert_rows.append((
                                      (row["gtId"]),
                                      (row["tenant_id"]),
                                      (document.session_id),
                                      (row["address1"]) ,
                                      (row["address2"]),
                                      (row["city"]),
                                      (row["state"]),
                                      (row["country"]),
                                      (row["zip"]) ,
                                      (row["lat"]),
                                      (row["lng"]),
                                      (row["geoType"]),
                                      (row["adGroup_id"]),
                                      (row["dma"]),
                                      (row["radius"])
                                    ))
                insertInfoToDB(insert_rows, conn_marketplace)
            '''
            print geotargets_rows
            if geotargets_rows:
                geo_targets = make_geo_targets(geotargets_rows)
            if geo_targets:
                document.MergeFrom(geo_targets)
            #closeDB(conn_marketplace)
            #get creative info from db
            creative_rows = getInfoFromDB(adgroupId, sql_creatives, conn)
            if creative_rows:
                creatives = make_creatives(creative_rows)
            if creatives:
                document.MergeFrom(creatives)
        print document  # for final test when sending
        return document
    except Exception as e:
        print e
        return None
Пример #9
0
def make_ad_document(campaignId, adgroupId, conn):
    isAdgroupPresent = False
    try:
        document = AdDocument()
        rows = getInfoFromDB(adgroupId, sql_adgroup, conn)
        row = None
        if rows:
            row = rows[0]

        if row is not None:
            isAdgroupPresent = True
        if isAdgroupPresent:
            document.tenant_id = row["tenant_id"] if row["tenant_id"] else 0  #1
            document.campaign_id = long(
                campaignId) if campaignId != "0" else long(
                    row["campaignId"])  #2
            document.adgroup_id = long(
                adgroupId) if adgroupId != "0" else 0  #3
            document.adomain = "rti.com"  #4   ??
            # 5  6  TBD
            document.banner_size = row["banner_size"] if row[
                "banner_size"] else ""  #7
            document.creative_type = row["creative_type"] if row[
                "creative_type"] else ""  #8
            document.instl = bool(row["instl"]) if row["instl"] else False  #9
            document.proximity_mode = row["proximity_mode"] if row[
                "proximity_mode"] else ""  #10
            creatives = None  #11
            target_profiles = None  #12
            geo_targets = None  #13
            publishers = None  #14
            document.status = row["status_flag"] if row[
                "status_flag"] else 6  #15
            setattr(document, 'del', bool(row["del"]))  #16
            print "setting rti_mode"
            document.rti_mode = AdDocument.BATCH  #17
            document.adv_bid_rates = row["bid_rate"] if row[
                "bid_rate"] else 0.0  #18
            document.is_secure = bool(
                row["is_secure"]) if row["is_secure"] else False  #19
            document.session_id = randint(0, 1000000)  #20
            document.pub_bid_rates = 0.0  #21
            document.kpi_ctr = row["kpi_ctr"] if row["kpi_ctr"] else 0.0  #22
            document.kpi_sar = row["kpi_sar"] if row["kpi_sar"] else 0.0  #23
            #24  TBD

        if isAdgroupPresent:
            #get targetprofile info from db
            print "get targetprofile info from db"
            targetprofiles_rows = getInfoFromDB(adgroupId, sql_targetprofiles,
                                                conn)
            if targetprofiles_rows:
                target_profiles = make_target_profiles(targetprofiles_rows)

            if target_profiles:
                document.MergeFrom(target_profiles)

            #get geotarget info from db
            print "get geotarget info from db"
            geotargets_rows = getInfoFromDB(adgroupId, sql_geotargets, conn)
            print "got geotarget info from db"

            print "connecting to marketplace db"
            conn_marketplace = getConnectWithDB(dbconfig_marketplace,
                                                "marketplace_pool")
            print "connected to marketplace db"
            if geotargets_rows:
                arrLen = len(geotargets_rows)
                i = 0
                data = str()
                for row in geotargets_rows:
                    print i, " vs ", arrLen
                    i += 1
                    #                    data += '(' + str(row["gtId"]) + ',' + str(row["tenant_id"]) +  ',' + str(document.session_id) + ',' + str(row["address1"]) + ',' + str(row["address2"]) + ',' + str(row["city"]) + ',' + str(row["state"]) + ',' + str(row["country"]) + ',' + str(row["zip"]) + ',' + str(row["lat"]) + ',' + str(row["lng"]) + ',' + str(row["geoType"]) + ',' + str(row["adGroup_id"]) + ',' + str(row["dma"]) + ',' + str(row["radius"]) + '),'
                    if row["lat"] and row["lng"] and row["radius"]:
                        insertInfoToDB(row, document.session_id,
                                       conn_marketplace)
                print data
                geo_targets = make_geo_targets(geotargets_rows)
                if geo_targets:
                    document.MergeFrom(geo_targets)
            closeDB(conn_marketplace)
            #get creative info from db
            print "get creative info from db"
            creative_rows = getInfoFromDB(adgroupId, sql_creatives, conn)
            if creative_rows:
                creatives = make_creatives(creative_rows)
            if creatives:
                document.MergeFrom(creatives)
        print document  # for test
        return document
    except Exception as e:
        print e
        return None