def get_creative_template_by_name(template_name):
    
    dfp_client = get_client()
    # Initialize appropriate service.
    creative_template_service = dfp_client.GetService(
      'CreativeTemplateService', version='v202102')

     # Create a statement to select creative templates.
    statement = (ad_manager.StatementBuilder(version='v202102')
     .Where('name = :name')
     .WithBindVariable('name', template_name))

    response = creative_template_service.getCreativeTemplatesByStatement(
            statement.ToStatement())
   
    no_creative_template_found = False
    try:
        no_creative_template_found = True if len(response['results']) < 1 else False 
    except (AttributeError, KeyError):
        no_creative_template_found = True

    if no_creative_template_found:
        raise DFPObjectNotFound('No DFP creative template found with name {0}'.format(
            template_name))
    else:
        creative_template = response['results'][0]
        #print(creative_template)
        print('Found creative_template with name "{name}" and id {id}.'.format(name=creative_template['name'], id=creative_template['id']))
    return creative_template
예제 #2
0
def get_advertiser_id_by_name(name):
    """
  Returns a DFP company ID from company name.

  Args:
    name (str): the name of the DFP advertiser
  Returns:
    an integer: the advertiser's DFP ID
  """
    dfp_client = get_client()
    company_service = dfp_client.GetService('CompanyService',
                                            version='v201802')

    # Filter by name.
    query = 'WHERE name = :name'
    values = [
        {
            'key': 'name',
            'value': {
                'xsi_type': 'TextValue',
                'value': name
            }
        },
    ]
    statement = dfp.FilterStatement(query, values)

    response = company_service.getCompaniesByStatement(statement.ToStatement())

    # A company is required.
    no_company_found = False
    try:
        no_company_found = True if len(response['results']) < 1 else False
    except (AttributeError, KeyError):
        no_company_found = True

    if no_company_found:
        if getattr(settings, 'DFP_CREATE_ADVERTISER_IF_DOES_NOT_EXIST', False):
            advertiser = create_advertiser(name)
        else:
            raise DFPObjectNotFound(
                'No advertiser found  with name {0}'.format(name))
    elif len(response['results']) > 1:
        raise BadSettingException(
            'Multiple advertisers found with name {0}'.format(name))
    else:
        advertiser = response['results'][0]

    logger.info(
        u'Using existing advertiser with ID "{id}", name "{name}", and '
        'type "{type}".'.format(id=advertiser['id'],
                                name=advertiser['name'],
                                type=advertiser['type']))

    return advertiser['id']
예제 #3
0
def get_user_id_by_email(email_address):
    """
  Returns a DFP user ID from email address.

  Args:
    email (str): the email of the DFP user
  Returns:
    an integer: the user's DFP ID
  """
    dfp_client = get_client()
    user_service = dfp_client.GetService('UserService', version='v201802')

    # Filter by email address.
    query = 'WHERE email = :email'
    values = [
        {
            'key': 'email',
            'value': {
                'xsi_type': 'TextValue',
                'value': email_address
            }
        },
    ]
    statement = dfp.FilterStatement(query, values)

    response = user_service.getUsersByStatement(statement.ToStatement())

    # A user is required.
    no_user_found = False
    try:
        no_user_found = True if len(response['results']) < 1 else False
    except (AttributeError, KeyError):
        no_user_found = True

    if no_user_found:
        raise DFPObjectNotFound(
            'No DFP user found with email address {0}'.format(email_address))

    # Only get the first user in case there are multiple matches.
    user = response['results'][0]

    logger.info(u'Found user with email "{email}" and name {name}.'.format(
        email=user['email'], name=user['name']))

    return user['id']
예제 #4
0
def get_placement_by_name(placement_name):
    """
  Gets a placement by name from DFP.

  Args:
    placement_name (str): the name of the DFP placement
  Returns:
    a DFP placement object
  """

    dfp_client = get_client()
    placement_service = dfp_client.GetService('PlacementService',
                                              version='v201802')

    query = 'WHERE name = :name'
    values = [
        {
            'key': 'name',
            'value': {
                'xsi_type': 'TextValue',
                'value': placement_name
            }
        },
    ]
    statement = dfp.FilterStatement(query, values)
    response = placement_service.getPlacementsByStatement(
        statement.ToStatement())

    no_placement_found = False
    try:
        no_placement_found = True if len(response['results']) < 1 else False
    except (AttributeError, KeyError):
        no_placement_found = True

    if no_placement_found:
        raise DFPObjectNotFound(
            'No DFP placement found with name {0}'.format(placement_name))
    else:
        placement = response['results'][0]
        logger.info(
            u'Found placement with ID "{id}" and name "{name}".'.format(
                id=placement['id'], name=placement['name']))
    return placement
예제 #5
0
def get_ad_unit_by_name(ad_unit_name):
    """
  Gets a ad unit by name from DFP.

  Args:
    ad_unit_name (str): the name of the DFP ad unit
  Returns:
    a DFP ad unit object
  """

    dfp_client = get_client()
    ad_unit_service = dfp_client.GetService('InventoryService',
                                            version='v201811')

    query = 'WHERE name = :name'
    values = [
        {
            'key': 'name',
            'value': {
                'xsi_type': 'TextValue',
                'value': ad_unit_name
            }
        },
    ]
    statement = ad_manager.FilterStatement(query, values)
    response = ad_unit_service.getAdUnitsByStatement(statement.ToStatement())

    no_ad_unit_found = False
    try:
        no_ad_unit_found = True if len(response['results']) < 1 else False
    except (AttributeError, KeyError):
        no_ad_unit_found = True

    if no_ad_unit_found:
        raise DFPObjectNotFound(
            'No DFP ad_unit found with name {0}'.format(ad_unit_name))
    else:
        ad_unit = response['results'][0]
        logger.info(
            u'Found ad_unit with name "{name}".'.format(name=ad_unit['name']))
    return ad_unit
예제 #6
0
def create_line_item_configs(prices, order_id, placement_ids, bidder_code,
                             sizes, hb_bidder_key_id, hb_pb_key_id,
                             currency_code, HBBidderValueGetter,
                             HBPBValueGetter):
    """
  Create a line item config for each price bucket.

  Args:
    prices (array)
    order_id (int)
    placement_ids (arr)
    bidder_code (str)
    hb_bidder_key_id (int)
    hb_pb_key_id (int)
    currency_code (str)
    HBBidderValueGetter (DFPValueIdGetter)
    HBPBValueGetter (DFPValueIdGetter)
  Returns:
    an array of objects: the array of DFP line item configurations
  """

    # The DFP targeting value ID for this `hb_bidder` code.
    hb_bidder_value_id = HBBidderValueGetter.get_value_id(bidder_code)

    line_items_config = []
    root_ad_unit_id = None

    if not placement_ids:
        # Since the placement ids array is empty, it means we should target a run of network for the line item
        root_ad_unit_id = dfp.get_ad_units.get_root_ad_unit_id()

        if root_ad_unit_id is None:
            raise DFPObjectNotFound(
                'Could not find the root ad unit to target a run of network.')

    for price in prices:

        price_str = num_to_str(micro_amount_to_num(price))

        # Autogenerate the line item name.
        line_item_name = u'{bidder_code}: HB ${price}'.format(
            bidder_code=bidder_code, price=price_str)

        # The DFP targeting value ID for this `hb_pb` price value.
        hb_pb_value_id = HBPBValueGetter.get_value_id(price_str)

        config = dfp.create_line_items.create_line_item_config(
            name=line_item_name,
            order_id=order_id,
            placement_ids=placement_ids,
            cpm_micro_amount=price,
            sizes=sizes,
            hb_bidder_key_id=hb_bidder_key_id,
            hb_pb_key_id=hb_pb_key_id,
            hb_bidder_value_id=hb_bidder_value_id,
            hb_pb_value_id=hb_pb_value_id,
            currency_code=currency_code,
            root_ad_unit_id=root_ad_unit_id)

        line_items_config.append(config)

    return line_items_config