def main(client): # Initialize appropriate service. activity_group_service = client.GetService('ActivityGroupService', version='v201802') # Create a statement to select activity groups. statement = ( dfp.StatementBuilder().Where('status = :status').WithBindVariable( 'status', 'ACTIVE')) # Retrieve a small amount of activity groups at a time, paging # through until all activity groups have been retrieved. while True: response = activity_group_service.getActivityGroupsByStatement( statement.ToStatement()) if 'results' in response: for activity_group in response['results']: # Print out some information for each activity group. print( 'Activity group with ID "%d" and name "%s" was found.\n' % (activity_group['id'], activity_group['name'])) statement.offset += statement.limit else: break print '\nNumber of results found: %s' % response['totalResultSetSize']
def main(client, reconciliation_report_id): # Initialize appropriate service. reconciliation_order_report_service = client.GetService( 'ReconciliationOrderReportService', version='v201802') # Create a statement to select reconciliation order reports. statement = (dfp.StatementBuilder().Where( 'reconciliationReportId = :reconciliationReportId').WithBindVariable( 'reconciliationReportId', reconciliation_report_id)) # Retrieve a small amount of reconciliation order reports at a time, paging # through until all reconciliation order reports have been retrieved. while True: response = (reconciliation_order_report_service. getReconciliationOrderReportsByStatement( statement.ToStatement())) if 'results' in response: for reconciliation_order_report in response['results']: # Print out some information for each reconciliation order report. print( 'Reconciliation order report with ID "%d" and status "%s" was ' 'found.\n' % (reconciliation_order_report['id'], reconciliation_order_report['status'])) statement.offset += dfp.SUGGESTED_PAGE_LIMIT else: break print '\nNumber of results found: %s' % response['totalResultSetSize']
def main(client, activity_group_id, advertiser_company_id): # Initialize appropriate service. activity_group_service = client.GetService('ActivityGroupService', version='v201711') # Create statement object to select a single activity groups by ID. statement = ( dfp.StatementBuilder().Where('id = :activityGroupId').WithBindVariable( 'activityGroupId', long(activity_group_id)).Limit(1)) # Get activity groups by statement. response = activity_group_service.getActivityGroupsByStatement( statement.ToStatement()) if 'results' in response and len(response['results']): updated_activity_groups = [] for activity_group in response['results']: activity_group['companyIds'].append(advertiser_company_id) updated_activity_groups.append(activity_group) # Update the activity groups on the server. activity_groups = activity_group_service.updateActivityGroups( updated_activity_groups) for activity_group in activity_groups: print(('Activity group with ID "%s" and name "%s" was updated.') % (activity_group['id'], activity_group['name'])) else: print 'No activity groups found to update.'
def main(client, exchange_rate_id): # Initialize appropriate service. exchange_rate_service = client.GetService('ExchangeRateService', version='v201711') # Create a statement to get an exchange rate by its ID. statement = (dfp.StatementBuilder().Where('id = :id').WithBindVariable( 'id', long(exchange_rate_id)).Limit(1)) # Get rate cards by statement. response = exchange_rate_service.getExchangeRatesByStatement( statement.ToStatement()) if 'results' in response and len(response['results']): exchange_rate = response['results'][0] # Update the exchange rate value to 1.5. exchange_rate['exchangeRate'] = long(15000000000) exchange_rates = exchange_rate_service.updateExchangeRates( [exchange_rate]) if exchange_rates: for exchange_rate in exchange_rates: print( 'Exchange rate with id \'%s,\' currency code \'%s,\' ' 'direction \'%s,\' and exchange rate \'%.2f\' ' 'was updated.' % (exchange_rate['id'], exchange_rate['currencyCode'], exchange_rate['direction'], (float(exchange_rate['exchangeRate']) / 10000000000))) else: print 'No exchange rates were updated.' else: print 'No exchange rates found to update.'
def main(client): # Initialize appropriate service. reconciliation_order_report_service = (client.GetService( 'ReconciliationOrderReportService', version='v201802')) # Create a statement to select reconciliation order reports. statement = (dfp.StatementBuilder().Where('id = :id').OrderBy( 'id', ascending=True).Limit(1).WithBindVariable( 'id', RECONCILIATION_ORDER_REPORT_ID)) # Get reconciliation order reports by statement. page = (reconciliation_order_report_service. getReconciliationOrderReportsByStatement(statement.ToStatement())) report = page['results'][0] print('Reconciliation order report with ID %d will be submitted.' % report['id']) # Remove limit and offset from statement. statement.limit = None statement.offset = None # Perform action. result = (reconciliation_order_report_service. performReconciliationOrderReportAction( {'xsi_type': 'SubmitReconciliationOrderReports'}, statement.ToStatement())) if result and result['numChanges'] > 0: print('Number of reconciliation order reports submitted: %d' % result['numChanges']) else: print 'No reconciliation order reports were submitted.'
def main(client): # Initialize appropriate service. product_package_item_service = client.GetService( 'ProductPackageItemService', version='v201805') # Create a statement to select product package items. statement = dfp.StatementBuilder() # Retrieve a small amount of product package items at a time, paging # through until all product package items have been retrieved. while True: response = product_package_item_service.getProductPackageItemsByStatement( statement.ToStatement()) if 'results' in response and len(response['results']): for product_package_item in response['results']: # Print out some information for each product package item. print('Product package item with ID "%d", product id "%d", and product ' 'package id "%d" was found.\n' % (product_package_item['id'], product_package_item['productId'], product_package_item['productPackageId'])) statement.offset += statement.limit else: break print '\nNumber of results found: %s' % response['totalResultSetSize']
def main(client, parent_id): # Initialize appropriate service. inventory_service = client.GetService('InventoryService', version='v201802') # Create a query to select ad units under the parent ad unit and the parent ad # unit. statement = (dfp.StatementBuilder() .Where('parentId = :parentId or id = :parentId') .WithBindVariable('parentId', long(parent_id))) ad_units_archived = 0 # Get ad units by statement. while True: response = inventory_service.getAdUnitsByStatement( statement.ToStatement()) if 'results' in response: for ad_unit in response['results']: print ('Ad unit with ID "%s" and name "%s" will be archived.' % (ad_unit['id'], ad_unit['name'])) # Perform action. result = inventory_service.performAdUnitAction( {'xsi_type': 'ArchiveAdUnits'}, statement.ToStatement()) if result and int(result['numChanges']) > 0: ad_units_archived += int(result['numChanges']) statement.offset += statement.limit else: break # Display results. if ad_units_archived > 0: print 'Number of ad units archived: %s' % ad_units_archived else: print 'No ad units were archived.'
def main(client, activity_id): # Initialize appropriate service. activity_service = client.GetService('ActivityService', version='v201802') # Create statement object to select one activity by ID to update. statement = ( dfp.StatementBuilder().Where('id = :activityId').WithBindVariable( 'activityId', long(activity_id)).Limit(1)) # Get activities by statement. response = activity_service.getActivitiesByStatement( statement.ToStatement()) if 'results' in response and len(response['results']): updated_activities = [] for activity in response['results']: # Update the expected URL. activity['expectedURL'] = 'https://google.com' updated_activities.append(activity) # Update the activity on the server. activities = activity_service.updateActivities(updated_activities) for updated_activity in activities: print(('Activity with ID "%s" and name "%s" was updated.') % (updated_activity['id'], updated_activity['name'])) else: print 'No activities found to update.'
def main(client): # Initialize appropriate service. reconciliation_report_service = client.GetService( 'ReconciliationReportService', version='v201802') # Create a statement to select reconciliation reports. statement = dfp.StatementBuilder() # Retrieve a small amount of reconciliation reports at a time, paging # through until all reconciliation reports have been retrieved. while True: response = ( reconciliation_report_service.getReconciliationReportsByStatement( statement.ToStatement())) if 'results' in response: for reconciliation_report in response['results']: # Print out some information for each reconciliation report. print( 'Reconciliation report with ID "%d", month "%d", and year "%d" was' ' found.\n' % (reconciliation_report['id'], reconciliation_report['startDate']['month'], reconciliation_report['startDate']['year'])) statement.offset += statement.limit else: break print '\nNumber of results found: %s' % response['totalResultSetSize']
def get_custom_targeting_by_key_name(key_name): """ Gets a set of custom targeting values by key name Args: name (str): the name of the targeting key Returns: an array, or None: if the key exists, return an array of objects, where each object is info about a custom targeting value """ key_id = get_key_by_name(key_name)['id'] client = get_client() custom_targeting_service = client.GetService('CustomTargetingService', version='v201802') statement = (dfp.StatementBuilder().Where( 'customTargetingKeyId = :customTargetingKeyId').WithBindVariable( 'customTargetingKeyId', key_id)) response = custom_targeting_service.getCustomTargetingValuesByStatement( statement.ToStatement()) vals = [] while True: response = custom_targeting_service.getCustomTargetingValuesByStatement( statement.ToStatement()) if 'results' in response and len(response['results']) > 0: vals = vals + response['results'] statement.offset += statement.limit else: break return vals
def main(client): # Initialize appropriate service. creative_service = client.GetService('CreativeService', version='v201708') # Create a statement to get the image creative. statement = (dfp.StatementBuilder() .Where('id = :id') .OrderBy('id', ascending=True) .WithBindVariable('id', CREATIVE_ID)) # 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']))
def main(client, company_id): # Initialize appropriate service. company_service = client.GetService('CompanyService', version='v201802') # Create statement object to only select a single company by ID. statement = (dfp.StatementBuilder().Where('id = :id').WithBindVariable( 'id', long(company_id)).Limit(1)) # Get companies by statement. response = company_service.getCompaniesByStatement(statement.ToStatement()) if 'results' in response and len(response['results']): updated_companies = [] for company in response['results']: company['comment'] += ' Updated.' updated_companies.append(company) # Update the companies on the server. companies = company_service.updateCompanies(updated_companies) # Display results. for company in companies: print(('Company with ID "%s", name "%s", and comment "%s"' ' was updated.') % (company['id'], company['name'], company['comment'])) else: print 'No companies found to update.'
def main(client): # Initialize appropriate service. reconciliation_line_item_report_service = (client.GetService( 'ReconciliationLineItemReportService', version='v201802')) # Create a statement to select a reconciliation line item report. statement = ( dfp.StatementBuilder().Where('id = :lineItemReportId').OrderBy( 'id', ascending=True).Limit(1).WithBindVariable( 'lineItemReportId', RECONCILIATION_LINE_ITEM_REPORT_ID)) # Get reconciliation line item reports by statement. page = (reconciliation_line_item_report_service. getReconciliationLineItemReportsByStatement( statement.ToStatement())) line_item_report = page['results'][0] # Set and use a manual volume for billing. This example splits the difference # between DFP and the third party volume. line_item_report['manualVolume'] = ( line_item_report['dfpVolume'] + line_item_report['thirdPartyVolume']) / 2 line_item_report['reconciliationSource'] = 'MANUAL' result = (reconciliation_line_item_report_service. updateReconciliationLineItemReports([line_item_report])) for updated_report in result: print( 'Reconciliation line item report with ID %d for line item ID %d was ' 'updated, with manual volume %d' % (updated_report['id'], updated_report['lineItemId'], updated_report['manualVolume']))
def main(client, contact_id): # Initialize appropriate service. contact_service = client.GetService('ContactService', version='v201805') # Create statement object to select the single contact by ID. statement = (dfp.StatementBuilder() .Where('id = :id') .WithBindVariable('id', long(contact_id)) .Limit(1)) # Get contacts by statement. response = contact_service.getContactsByStatement( statement.ToStatement()) if 'results' in response: updated_contacts = [] for contact in response['results']: contact['address'] = '123 New Street, New York, NY, 10011' updated_contacts.append(contact) # Update the contact on the server. contacts = contact_service.updateContacts(updated_contacts) # Display results. for contact in contacts: print (('Contact with ID "%s", name "%s", and address "%s" ' 'was updated.') % (contact['id'], contact['name'], contact['address'])) else: print 'No contacts found to update.'
def main(client, custom_field_id): # Initialize appropriate service. custom_field_service = client.GetService( 'CustomFieldService', version='v201802') # Create statement to get a custom field. statement = (dfp.StatementBuilder() .Where('id = :customFieldId') .WithBindVariable('customFieldId', long(custom_field_id))) # Get custom field. custom_fields = custom_field_service.getCustomsFieldByStatement( statement.ToStatement()) if custom_fields: for custom_field in custom_fields: custom_field['description'] += ' Updated.' # Update the custom field on the server. custom_fields = custom_field_service.updateCustomFields(custom_fields) # Display results. for custom_field in custom_fields: print (('Custom field with ID "%s", name "%s", and ' 'description "%s" was updated.') % (custom_field['id'], custom_field['name'], custom_field['description'])) else: print 'No custom fields found to update.'
def main(client, audience_segment_id): # Initialize appropriate service. audience_segment_service = client.GetService('AudienceSegmentService', version='v201708') # Create statement object to get the specified first party audience segment. statement = (dfp.StatementBuilder().Where( 'Type = :type AND Id = :audience_segment_id').WithBindVariable( 'audience_segment_id', long(audience_segment_id)).WithBindVariable( 'type', 'FIRST_PARTY').Limit(1)) # Get audience segments by statement. response = audience_segment_service.getAudienceSegmentsByStatement( statement.ToStatement()) if 'results' in response: updated_audience_segments = [] for audience_segment in response['results']: print( 'Audience segment with id "%s" and name "%s" will be updated.' % (audience_segment['id'], audience_segment['name'])) audience_segment['membershipExpirationDays'] = '180' updated_audience_segments.append(audience_segment) audience_segments = audience_segment_service.updateAudienceSegments( updated_audience_segments) for audience_segment in audience_segments: print('Audience segment with id "%s" and name "%s" was updated' % (audience_segment['id'], audience_segment['name'])) else: print 'No audience segment found to update.'
def main(client, currency_code): # Initialize appropriate service. exchange_rate_service = client.GetService('ExchangeRateService', version='v201711') # Create a statement to select exchange rates. statement = (dfp.StatementBuilder().Where( 'currencyCode = :currencyCode').WithBindVariable( 'currencyCode', currency_code)) # Retrieve a small amount of exchange rates at a time, paging # through until all exchange rates have been retrieved. while True: response = exchange_rate_service.getExchangeRatesByStatement( statement.ToStatement()) if 'results' in response: for exchange_rate in response['results']: # Print out some information for each exchange rate. print( 'Exchange rate with ID "%d", currency code "%s", and exchange ' 'rate "%.2f" was found.\n' % (exchange_rate['id'], exchange_rate['currencyCode'], float(exchange_rate['exchangeRate'] / 1000000))) statement.offset += dfp.SUGGESTED_PAGE_LIMIT else: break print '\nNumber of results found: %s' % response['totalResultSetSize']
def main(client, rate_card_id): # Initialize appropriate service. base_rate_service = client.GetService('BaseRateService', version='v201711') # 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 += dfp.SUGGESTED_PAGE_LIMIT else: break print '\nNumber of results found: %s' % response['totalResultSetSize']
def main(client): # Initialize appropriate service. package_service = client.GetService('PackageService', version='v201708') # Create a statement to select the single package. statement = (dfp.StatementBuilder() .Where('id = :id') .OrderBy('id', ascending=True) .Limit(1) .WithBindVariable('id', PACKAGE_ID)) # Get the package. query_result = package_service.getPackagesByStatement(statement.ToStatement()) package = query_result['results'][0] print ('Package with ID %d will create proposal line items using product ' 'package with ID %d.' % (package['id'], package['productPackageId'])) # Remove limit and offset from statement. statement.limit = None statement.offset = None # Perform update packages action. result = package_service.performPackageAction( {'xsi_type': 'CreateProposalLineItemsFromPackages'}, statement.ToStatement()) if result and result['numChanges'] > 0: print ('Number of packages that proposal line items were created for: %d' % result['numChanges']) else: print 'No proposal line items were created.'
def main(client): # Initialize appropriate service. workflow_request_service = client.GetService( 'WorkflowRequestService', version='v201802') # Create a statement to select workflow requests. statement = (dfp.StatementBuilder() .Where('type = :type') .WithBindVariable('type', 'WORKFLOW_EXTERNAL_CONDITION_REQUEST')) # Retrieve a small amount of workflow requests at a time, paging # through until all workflow requests have been retrieved. while True: response = workflow_request_service.getWorkflowRequestsByStatement( statement.ToStatement()) if 'results' in response and len(response['results']): for workflow_request in response['results']: # Print out some information for each workflow request. print('Workflow request with ID "%d", entity type "%s", and entity ID ' '"%d" was found.\n' % (workflow_request['id'], workflow_request['entityType'], workflow_request['entityId'])) statement.offset += statement.limit else: break print '\nNumber of results found: %s' % response['totalResultSetSize']
def main(client, creative_set_id, companion_creative_id): # Initialize appropriate service. creative_set_service = client.GetService('CreativeSetService', version='v201805') # Create statement to select a single creative set by ID. statement = (dfp.StatementBuilder() .Where('id = :creativeSetId') .WithBindVariable('creativeSetId', long(creative_set_id))) # Get creative set. response = creative_set_service.getCreativeSetsByStatement( statement.ToStatement()) if 'results' in response and len(response['results']): updated_created_sets = [] for creative_set in response['results']: creative_set['companionCreativeIds'].append(companion_creative_id) updated_created_sets.append(creative_set) # Update the creative sets on the server. creative_sets = creative_set_service.updateCreativeSet(updated_created_sets) # Display results. for creative_set in creative_sets: print (('Creative set with ID "%s", master creative ID "%s", and ' 'companion creative IDs {%s} was updated.') % (creative_set['id'], creative_set['masterCreativeId'], ','.join(creative_set['companionCreativeIds']))) else: print 'No creative sets found to update.'
def main(client, placement_id): # Initialize appropriate service. placement_service = client.GetService('PlacementService', version='v201711') # Create query. statement = ( dfp.StatementBuilder().Where('id = :placementId').WithBindVariable( 'placementId', long(placement_id)).Limit(1)) # Get placements by statement. placements = placement_service.getPlacementsByStatement( statement.ToStatement()) for placement in placements: print( 'Placement with id "%s", name "%s", and status "%s" will be ' 'deactivated.' % (placement['id'], placement['name'], placement['status'])) # Perform action. result = placement_service.performPlacementAction( {'xsi_type': 'DeactivatePlacements'}, statement.ToStatement()) # Display results. if result and int(result['numChanges']) > 0: print 'Number of placements deactivated: %s' % result['numChanges'] else: print 'No placements were deactivated.'