Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
'PremiumRateService', version='v201802')
# Create a statement to select premium rates.
statement = dfp.StatementBuilder()
# Retrieve a small amount of premium rates at a time, paging
# through until all premium rates have been retrieved.
while True:
response = premium_rate_service.getPremiumRatesByStatement(
statement.ToStatement())
if 'results' in response and len(response['results']):
for premium_rate in response['results']:
# Print out some information for each premium rate.
print('Premium rate with ID "%d", premium feature "%s", and rate card '
'id "%d" was found.\n' % (premium_rate['id'],
dfp.DfpClassType(premium_rate),
premium_rate['rateCardId']))
statement.offset += statement.limit
else:
break
print '\nNumber of results found: %s' % response['totalResultSetSize']
# for both CPM and CPC rate types.
premium_rate = {
'rateCardId': rate_card_id,
'pricingMethod': 'ANY_VALUE',
'premiumFeature': ad_unit_premium_feature,
'premiumRateValues': [cpc_premium_rate_value, cpm_premium_rate_value]
}
# Add premium_rates.
premium_rates = premium_rate_service.createPremiumRates([premium_rate])
# Display results.
for premium_rate in premium_rates:
print ('A premium rate for "%s" was added to the rate card with ID'
' of "%s".\n'
% (dfp.DfpClassType(premium_rate['premiumFeature']),
premium_rate['id']))
Returns:
str a CSV writer friendly value formatted by Value.Type.
"""
if 'value' in pql_value:
field = pql_value['value']
elif 'values' in pql_value:
field = pql_value['values']
else:
field = None
if field:
if isinstance(field, list):
return ','.join(['"%s"' % str(ConvertValueForCsv(single_field))
for single_field in field])
else:
class_type = dfp.DfpClassType(pql_value)
if class_type == 'TextValue':
return field.replace('"', '""').encode('UTF8')
elif class_type == 'NumberValue':
return float(field) if '.' in field else int(field)
elif class_type == 'DateTimeValue':
return ConvertDateTimeToOffset(field)
elif class_type == 'DateValue':
return date(int(field['date']['year']),
int(field['date']['month']),
int(field['date']['day'])).isoformat()
else:
return field
else:
return '-'
# Get the creative.
query_result = creative_service.getCreativesByStatement(
statement.ToStatement())
image_creative = query_result['results'][0]
# Build the new creative, set id to None to create a copy.
image_creative['id'] = None
image_creative['name'] = 'Copy of %s' % image_creative['name']
result = creative_service.createCreatives([image_creative])[0]
print ('A creative with ID %d, name "%s", and type "%s" was created and '
'can be previewed at: %s' % (result['id'],
result['name'],
dfp.DfpClassType(result),
result['previewUrl']))
# Create a statement to select base rates.
statement = (dfp.StatementBuilder()
.Where('rateCardId = :rateCardId')
.WithBindVariable('rateCardId', rate_card_id))
# Retrieve a small amount of base rates at a time, paging
# through until all base rates have been retrieved.
while True:
response = base_rate_service.getBaseRatesByStatement(statement.ToStatement(
))
if 'results' in response and len(response['results']):
for base_rate in response['results']:
# Print out some information for each base rate.
print('Base rate with ID "%d", type "%s", and rate card ID "%d" was '
'found.\n' %
(base_rate['id'], dfp.DfpClassType(base_rate),
base_rate['rateCardId']))
statement.offset += statement.limit
else:
break
print '\nNumber of results found: %s' % response['totalResultSetSize']
}
}]
query = 'where rateCardId = :rateCardId ORDER BY id ASC'
statement = dfp.FilterStatement(query, values)
# Get base rates by statement.
while True:
response = base_rate_service.getBaseRatesByStatement(
statement.ToStatement())
if 'results' in response:
# Display results.
for base_rate in response['results']:
print ('Base rate with ID \'%s\' and type \'%s\' belonging to '
'rate card ID \'%s\' was found.' % (
base_rate['id'],
dfp.DfpClassType(base_rate),
base_rate['rateCardId']))
statement.offset += dfp.SUGGESTED_PAGE_LIMIT
else:
break
print '\nNumber of results found: %s' % response['totalResultSetSize']
Returns:
str a CSV writer friendly value formatted by Value.Type.
"""
if 'value' in pql_value:
field = pql_value['value']
elif 'values' in pql_value:
field = pql_value['values']
else:
field = None
if field:
if isinstance(field, list):
return ','.join(['"%s"' % str(ConvertValueForCsv(single_field))
for single_field in field])
else:
class_type = dfp.DfpClassType(pql_value)
if class_type == 'TextValue':
return field.replace('"', '""').encode('UTF8')
elif class_type == 'NumberValue':
return float(field) if '.' in field else int(field)
elif class_type == 'DateTimeValue':
return ConvertDateTimeToOffset(field)
elif class_type == 'DateValue':
return date(int(field['date']['year']),
int(field['date']['month']),
int(field['date']['day'])).isoformat()
else:
return field
else:
return '-'
},
{
'xsi_type': 'StringCreativeTemplateVariableValue',
'uniqueName': 'Targetwindow',
'value': '_blank'
}
]
}
# Call service to create the creative.
creative = creative_service.createCreatives([creative])[0]
# Display results.
print ('Template creative with id "%s", name "%s", and type "%s" was '
'created and can be previewed at %s.'
% (creative['id'], creative['name'], dfp.DfpClassType(creative),
creative['previewUrl']))
# Initialize appropriate service.
base_rate_service = client.GetService('BaseRateService', version='v201805')
# Create a statement to select base rates.
statement = dfp.StatementBuilder()
# Retrieve a small amount of base rates at a time, paging
# through until all base rates have been retrieved.
while True:
response = base_rate_service.getBaseRatesByStatement(statement.ToStatement(
))
if 'results' in response and len(response['results']):
for base_rate in response['results']:
# Print out some information for each base rate.
print('Base rate with ID "%d", type "%s", and rate card ID "%d" was '
'found.\n' % (base_rate['id'], dfp.DfpClassType(base_rate),
base_rate['rateCardId']))
statement.offset += statement.limit
else:
break
print '\nNumber of results found: %s' % response['totalResultSetSize']
updated_base_rates = []
for base_rate in response['results']:
base_rate['rate'] = new_rate
updated_base_rates.append(base_rate)
# Update base rates remotely.
base_rates = base_rate_service.updateBaseRates(updated_base_rates)
# Display results.
if base_rates:
for base_rate in base_rates:
print ('Base rate with ID "%s" and type "%s" belonging to '
'rate card ID "%s" was updated.' % (
base_rate['id'],
dfp.DfpClassType(base_rate),
base_rate['rateCardId']))
else:
print 'No base rates were updated.'
else:
print 'No base rates found to update.'