def main(client):
  # Initialize appropriate service.
  custom_targeting_service = client.GetService(
      'CustomTargetingService', version='v201508')

  # Create statement to get all targeting keys.
  targeting_key_statement = dfp.FilterStatement()

  all_keys = []

  # Get custom targeting keys by statement.
  while True:
    response = custom_targeting_service.getCustomTargetingKeysByStatement(
        targeting_key_statement.ToStatement())
    if 'results' in response:
      all_keys.extend(response['results'])
      targeting_key_statement.offset += dfp.SUGGESTED_PAGE_LIMIT
    else:
      break

  if all_keys:
    # Create map of custom targeting key id to custom targeting values.
    key_value_map = {}

    # Create statement to get all targeting values.
    query = ('WHERE customTargetingKeyId IN (%s)'
             % ', '.join([str(key['id']) for key in all_keys]))
    targeting_value_statement = dfp.FilterStatement(query)

    # Get custom targeting values by statement.
    while True:
      response = custom_targeting_service.getCustomTargetingValuesByStatement(
          targeting_value_statement.ToStatement())
      if 'results' in response:
        for key in all_keys:
          for value in response['results']:
            if key['id'] == value['customTargetingKeyId']:
              if key['id'] not in key_value_map.keys():
                key_value_map[key['id']] = []
              key_value_map[key['id']].append(value)
        targeting_value_statement.offset += dfp.SUGGESTED_PAGE_LIMIT
      else:
        break

    # Display results.
    for key in all_keys:
      print ('Custom targeting key with id \'%s\', name \'%s\', display name '
             '\'%s\', and type \'%s\' was found.'
             %(key['id'], key['name'], key['displayName'], key['type']))
      if key['id'] in key_value_map.keys():
        for value in key_value_map[key['id']]:
          print ('\tCustom targeting value with id \'%s\', name \'%s\', and '
                 'display name \'%s\' was found.'
                 % (value['id'], value['name'], value['displayName']))
  else:
    print 'No keys were found.'
dataset/ETHPy150Open googleads/googleads-python-lib/examples/dfp/v201508/custom_targeting_service/get_all_custom_targeting_keys_and_values.py/main/original