def testDownloadPqlResultToList(self): pql_service = mock.Mock() header = [{'labelName': 'Some random header...'}, {'labelName': 'Another header...'}] rval = [{'values': [{'value': 'Some random PQL response...', 'Value_Type': 'TextValue'}, {'value': {'date': { 'year': '1999', 'month': '04', 'day': '03'}}, 'Value_Type': 'DateValue'}, {'value': '123', 'Value_Type': 'NumberValue'}, {'value': {'date': {'year': '2012', 'month': '11', 'day': '05'}, 'hour': '12', 'minute': '12', 'second': '12', 'timeZoneID': 'PST8PDT'}, 'Value_Type': 'DateTimeValue'}]}, {'values': [{'value': 'A second row of PQL response!', 'Value_Type': 'TextValue'}, {'value': {'date': { 'year': '2009', 'month': '02', 'day': '05'}}, 'Value_Type': 'DateValue'}, {'value': '345', 'Value_Type': 'NumberValue'}, {'value': {'date': {'year': '2013', 'month': '01', 'day': '03'}, 'hour': '02', 'minute': '02', 'second': '02', 'timeZoneID': 'GMT'}, 'Value_Type': 'DateTimeValue'}]}] def VerifyExpectedCall(arg): self.assertEqual({'values': None, 'query': ('SELECT Id, Name FROM Line_Item ' 'LIMIT 500 OFFSET 0')}, arg) return [{'rows': rval, 'columnTypes': header}] pql_service._service_name = 'PublisherQueryLanguageService' pql_service.select.side_effect = VerifyExpectedCall result_set = DfpUtils.DownloadPqlResultToList( pql_service, 'SELECT Id, Name FROM Line_Item') row1 = [DfpUtils._ConvertValueForCsv(field) for field in rval[0]['values']] row2 = [DfpUtils._ConvertValueForCsv(field) for field in rval[1]['values']] self.assertEqual([[header[0]['labelName'], header[1]['labelName']], row1, row2], result_set)
def main(client, master_creative_id): # Initialize appropriate service. creative_set_service = client.GetService('CreativeSetService', version='v201306') # Create statement object to only select creative sets that have the given # master creative. values = [{ 'key': 'masterCreativeId', 'value': { 'xsi_type': 'NumberValue', 'value': master_creative_id } }] query = 'WHERE masterCreativeId = :masterCreativeId' # Get creative sets by statement. creative_sets = DfpUtils.GetAllEntitiesByStatementWithService( creative_set_service, query=query, bind_vars=values) # Display results. for creative_set in creative_sets: print (('Creative set with ID \'%s\', master creative ID \'%s\', and ' 'companion creative IDs {%s} was found.') % (creative_set['id'], creative_set['masterCreativeId'], ','.join(creative_set['companionCreativeIds']))) print print 'Number of results found: %s' % len(creative_sets)
def main(client): # Initialize appropriate service. content_metadata_key_hierarchy_service = client.GetService( 'ContentMetadataKeyHierarchyService', version='v201408') # Create statement object to select all content. statement = DfpUtils.FilterStatement() # Get content by statement. while True: response = (content_metadata_key_hierarchy_service. GetContentMetadataKeyHierarchiesByStatement( statement.ToStatement())[0]) content_metadata_key_hierarchies = response.get('results') if content_metadata_key_hierarchies: # Display results. for content_metadata_key_hierarchy in content_metadata_key_hierarchies: print( 'Content metadata key hierarchyy with id \'%s\' and name \'%s\'' ' was found.' % (content_metadata_key_hierarchy['id'], content_metadata_key_hierarchy['name'])) statement.IncreaseOffsetBy(DfpUtils.PAGE_LIMIT) else: break print '\nNumber of results found: %s' % response['totalResultSetSize']
def main(client): # Initialize appropriate service. creative_wrapper_service = client.GetService('CreativeWrapperService', version='v201403') # Create statement object to only select active creative wrappers. values = [{ 'key': 'status', 'value': { 'xsi_type': 'TextValue', 'value': 'ACTIVE' } }] query = 'WHERE status = :status' statement = DfpUtils.FilterStatement(query, values) # Get creative wrappers by statement. while True: response = creative_wrapper_service.GetCreativeWrappersByStatement( statement.ToStatement())[0] creative_wrappers = response.get('results') if creative_wrappers: # Display results. for creative_wrapper in creative_wrappers: print( 'Creative wrapper with ID \'%s\' applying to label \'%s\' was ' 'found.' % (creative_wrapper['id'], creative_wrapper['labelId'])) statement.IncreaseOffsetBy(DfpUtils.PAGE_LIMIT) else: break print '\nNumber of results found: %s' % response['totalResultSetSize']
def main(client): # Initialize appropriate service. inventory_service = client.GetService('InventoryService', version='v201403') network_service = client.GetService('NetworkService', version='v201403') root_id = network_service.GetCurrentNetwork()[0]['effectiveRootAdUnitId'] # Create a statement to select the children of the effective root ad unit. values = [{ 'key': 'id', 'value': { 'xsi_type': 'TextValue', 'value': root_id } }] query = 'WHERE parentId = :id' statement = DfpUtils.FilterStatement(query, values) # Get ad units by statement. while True: response = inventory_service.GetAdUnitsByStatement( statement.ToStatement())[0] ad_units = response.get('results') if ad_units: # Display results. for ad_unit in ad_units: print ('Ad unit with ID \'%s\' and name \'%s\' was found.' % (ad_unit['id'], ad_unit['name'])) statement.IncreaseOffsetBy(DfpUtils.PAGE_LIMIT) else: break print '\nNumber of results found: %s' % response['totalResultSetSize']
def main(client): # Initialize appropriate service. activity_service = client.GetService('ActivityService', version='v201302') total_results_counter = 0 activity_group_ids = GetAllActivityGroupIds(client) for activity_group_id in activity_group_ids: # Set the activity group ID to select from. values = [{ 'key': 'activityGroupId', 'value': { 'xsi_type': 'NumberValue', 'value': activity_group_id } }] query = 'WHERE activityGroupId = :activityGroupId' # Get activities by statement. activities = DfpUtils.GetAllEntitiesByStatementWithService( activity_service, query=query, bind_vars=values) total_results_counter += len(activities) # Display results. for activity in activities: print( 'Activity with ID \'%s\', name \'%s\', and type \'%s\' was ' 'found.' % (activity['id'], activity['name'], activity['type'])) print print 'Number of results found: %s' % total_results_counter
def main(client): # Initialize appropriate service. inventory_service = client.GetService('InventoryService', version='v201206') network_service = client.GetService('NetworkService', version='v201206') root_id = network_service.GetCurrentNetwork()[0]['effectiveRootAdUnitId'] # Create a statement to select the children of the effective root ad unit. values = [{ 'key': 'id', 'value': { 'xsi_type': 'TextValue', 'value': root_id } }] query = 'WHERE parentId = :id' # Get ad units by statement. ad_units = DfpUtils.GetAllEntitiesByStatementWithService(inventory_service, query=query, bind_vars=values) # Display results. for ad_unit in ad_units: print('Ad unit with ID \'%s\' and name \'%s\' was found.' % (ad_unit['id'], ad_unit['name'])) print print 'Number of results found: %s' % len(ad_units)
def main(client): # Initialize appropriate service. custom_targeting_service = client.GetService('CustomTargetingService', version='v201311') values = [{ 'key': 'type', 'value': { 'xsi_type': 'TextValue', 'value': 'PREDEFINED' } }] query = 'WHERE type = :type' statement = DfpUtils.FilterStatement(query, values) # Get custom targeting keys by statement. while True: response = custom_targeting_service.GetCustomTargetingKeysByStatement( statement.ToStatement())[0] keys = response.get('results') if keys: # Display results. for key in 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'])) statement.IncreaseOffsetBy(DfpUtils.PAGE_LIMIT) else: break print '\nNumber of results found: %s' % response['totalResultSetSize']
def main(client, custom_field_id): # Initialize appropriate service. custom_field_service = client.GetService( 'CustomFieldService', version='v201311') # Create statement to get a custom field. values = [{ 'key': 'customFieldId', 'value': { 'xsi_type': 'NumberValue', 'value': custom_field_id } }] query = 'WHERE id = :customFieldId' statement = DfpUtils.FilterStatement(query, values) # Get custom field. custom_fields = custom_field_service.GetCustomsFieldByStatement( statement.ToStatement())[0] 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): # Initialize appropriate service. lica_service = client.GetService('LineItemCreativeAssociationService', version='v201405') # Create a filter statement. statement = DfpUtils.FilterStatement() # Get line items by statement. while True: response = lica_service.GetLineItemCreativeAssociationsByStatement( statement.ToStatement())[0] licas = response.get('results') if licas: # Display results. for lica in licas: if 'creativeSetId' in lica: print( 'LICA with line item ID \'%s\', creative set ID \'%s\', and ' 'status \'%s\' was found.' % (lica['lineItemId'], lica['creativeSetId'], lica['status'])) else: print( 'LICA with line item ID \'%s\', creative ID \'%s\', and status' ' \'%s\' was found.' % (lica['lineItemId'], lica['creativeId'], lica['status'])) print '\nNumber of results found: %s' % response['totalResultSetSize']
def main(client): # Initialize appropriate service. creative_template_service = client.GetService('CreativeTemplateService', version='v201405') # Create statement object to only select system defined creative templates. values = [{ 'key': 'creativeTemplateType', 'value': { 'xsi_type': 'TextValue', 'value': 'SYSTEM_DEFINED' } }] query = 'WHERE type = :creativeTemplateType' statement = DfpUtils.FilterStatement(query, values) # Get creative templates by statement. while True: response = creative_template_service.GetCreativeTemplatesByStatement( statement.ToStatement())[0] creative_templates = response.get('results') if creative_templates: # Display results. for template in creative_templates: print( 'Creative template with id \'%s\', name \'%s\', and type \'%s\' ' 'was found.' % (template['id'], template['name'], template['type'])) statement.IncreaseOffsetBy(DfpUtils.PAGE_LIMIT) else: break print '\nNumber of results found: %s' % response['totalResultSetSize']
def main(client, label_id): # Initialize appropriate service. label_service = client.GetService('LabelService', version='v201411') # Create a statement to select only active labels. values = [{ 'key': 'labelId', 'value': { 'xsi_type': 'NumberValue', 'value': label_id } }] query = 'WHERE id = :labelId' statement = DfpUtils.FilterStatement(query, values) # Get labels by filter. response = label_service.GetLabelsByStatement(statement.ToStatement())[0] labels = response.get('results') if labels: # Update each local label object by changing the description. for label in labels: label['description'] = 'These labels are updated.' # Update labels remotely. labels = label_service.UpdateLabels(labels) for label in labels: print('Label with id \'%s\' and name \'%s\' was updated.' % (label['id'], label['name'])) else: print 'No labels found to update.'
def main(client): # Initialize appropriate service. creative_wrapper_service = client.GetService('CreativeWrapperService', version='v201211') # Create statement object to only select active creative wrappers. values = [{ 'key': 'status', 'value': { 'xsi_type': 'TextValue', 'value': 'ACTIVE' } }] query = 'WHERE status = :status' # Get creative_wrappers by statement. creative_wrappers = DfpUtils.GetAllEntitiesByStatementWithService( creative_wrapper_service, query=query, bind_vars=values) # Display results. for creative_wrapper in creative_wrappers: print( 'Creative wrapper with ID \'%s\' applying to label \'%s\' with ' 'status \'%s\' was found.' % (creative_wrapper['id'], creative_wrapper['labelId'], creative_wrapper['status'])) print print 'Number of results found: %s' % len(creative_wrappers)
def main(client, order_id): # Initialize appropriate service. order_service = client.GetService('OrderService', version='v201411') # Create statement object to select a single order by an ID. values = [{ 'key': 'orderId', 'value': { 'xsi_type': 'NumberValue', 'value': order_id } }] query = 'WHERE id = :orderId' statement = DfpUtils.FilterStatement(query, values) # Get orders by statement. response = order_service.GetOrdersByStatement(statement.ToStatement())[0] orders = [] if 'results' in response: orders = response['results'] # Display results. for order in orders: print('Order with id \'%s\' name \'%s\' was found.' % (order['id'], order['name'])) print '\nNumber of results found: %s' % len(orders)
def main(client, activity_group_id): # Initialize appropriate service. activity_service = client.GetService('ActivityService', version='v201306') # Create statement object to only select active activities. values = [{ 'key': 'activityGroupId', 'value': { 'xsi_type': 'NumberValue', 'value': activity_group_id } }, { 'key': 'status', 'value': { 'xsi_type': 'TextValue', 'value': 'ACTIVE' } }] query = 'WHERE activityGroupId = :activityGroupId and status = :status' # Get activities by statement. activities = DfpUtils.GetAllEntitiesByStatementWithService( activity_service, query=query, bind_vars=values) # Display results. for activity in activities: print( 'Activity with ID \'%s\', name \'%s\', and type \'%s\' was ' 'found.' % (activity['id'], activity['name'], activity['type'])) print print 'Number of results found: %s' % len(activities)
def main(client, activity_group_id, advertiser_company_id): # Initialize appropriate service. activity_group_service = client.GetService('ActivityGroupService', version='v201311') # Create statement object to select a single activity groups by ID. values = [{ 'key': 'activityGroupId', 'value': { 'xsi_type': 'NumberValue', 'value': activity_group_id } }] query = 'WHERE id = :activityGroupId' # Create a filter statement. statement = DfpUtils.FilterStatement(query, values, 1) # Get activity groups by statement. response = activity_group_service.GetActivityGroupsByStatement( statement.ToStatement())[0] activity_groups = response.get('results') if activity_groups: # Display results. for activity_group in activity_groups: activity_group['companyIds'].append(advertiser_company_id) # Update the activity groups on the server. activity_groups = activity_group_service.UpdateActivityGroups( 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, placement_id): # Initialize appropriate service. placement_service = client.GetService('PlacementService', version='v201408') # Create query. values = [{ 'key': 'placementId', 'value': { 'xsi_type': 'NumberValue', 'value': placement_id } }] query = 'WHERE id = :placementId' statement = DfpUtils.FilterStatement(query, values, 1) # Get placements by statement. placements = placement_service.GetPlacementsByStatement( statement.ToStatement())[0] for placement in placements: if not placement['targetingDescription']: placement['targetingDescription'] = 'Generic description' placement['targetingAdLocation'] = 'All images on sports pages.' placement['targetingSiteName'] = 'http://code.google.com' placement['isAdSenseTargetingEnabled'] = 'true' # Update placements remotely. placements = placement_service.UpdatePlacements(placements) for placement in placements: print ('Placement with id \'%s\', name \'%s\', and AdSense targeting ' 'enabled \'%s\' was updated.' % (placement['id'], placement['name'], placement['isAdSenseTargetingEnabled']))
def main(client): # Initialize appropriate service. contact_service = client.GetService('ContactService', version='v201403') # Create statement object to only select contacts that are uninvited. values = [{ 'key': 'status', 'value': { 'xsi_type': 'TextValue', 'value': 'UNINVITED' } }] query = 'WHERE status = :status' statement = DfpUtils.FilterStatement(query, values) # Get contacts by statement. while True: response = contact_service.GetContactsByStatement( statement.ToStatement())[0] contacts = response.get('results') if contacts: # Display results. for contact in contacts: print ('Contact with ID \'%s\' and name \'%s\' was found.' % (contact['id'], contact['name'])) statement.IncreaseOffsetBy(DfpUtils.PAGE_LIMIT) else: break print '\nNumber of results found: %s' % response['totalResultSetSize']
def main(client, user_id): # Initialize appropriate service. user_team_association_service = client.GetService( 'UserTeamAssociationService', version='v201405') # Create query. values = [{ 'key': 'userId', 'value': { 'xsi_type': 'NumberValue', 'value': user_id } }] query = 'WHERE userId = :userId' # Create a filter statement. statement = DfpUtils.FilterStatement(query, values) # Get user team associations by statement. while True: response = user_team_association_service.GetUserTeamAssociationsByStatement( statement.ToStatement())[0] user_team_associations = response.get('results') if user_team_associations: # Display results. for user_team_association in user_team_associations: print ('User team association between user with ID \'%s\' and team with' ' ID \'%s\' was found.' % (user_team_association['userId'], user_team_association['teamId'])) statement.IncreaseOffsetBy(DfpUtils.PAGE_LIMIT) else: break print '\nNumber of results found: %s' % response['totalResultSetSize']
def main(client): # Initialize appropriate service. company_service = client.GetService('CompanyService', version='v201408') # Create statement object to only select companies that are advertisers. values = [{ 'key': 'type', 'value': { 'xsi_type': 'TextValue', 'value': 'ADVERTISER' } }] query = 'WHERE type = :type' statement = DfpUtils.FilterStatement(query, values) # Get companies by statement. while True: response = company_service.GetCompaniesByStatement( statement.ToStatement())[0] companies = response.get('results') if companies: # Display results. for company in companies: print ('Company with ID \'%s\', name \'%s\', and type \'%s\' was found.' % (company['id'], company['name'], company['type'])) statement.IncreaseOffsetBy(DfpUtils.PAGE_LIMIT) else: break print '\nNumber of results found: %s' % response['totalResultSetSize']
def main(client): # Initialize appropriate service. company_service = client.GetService('CompanyService', version='v201306') # Create statement object to only select companies that are advertisers. values = [{ 'key': 'type', 'value': { 'xsi_type': 'TextValue', 'value': 'ADVERTISER' } }] query = 'WHERE type = :type' # Get companies by statement. companies = DfpUtils.GetAllEntitiesByStatementWithService(company_service, query=query, bind_vars=values) # Display results. for company in companies: print( 'Company with ID \'%s\', name \'%s\', and type \'%s\' was found.' % (company['id'], company['name'], company['type'])) print print 'Number of results found: %s' % len(companies)
def main(client): # Initialize appropriate service. custom_field_service = client.GetService('CustomFieldService', version='v201308') # Get custom fields by statement. custom_fields = DfpUtils.GetAllEntitiesByStatementWithService( custom_field_service) # Display results. for custom_field in custom_fields: if custom_field['CustomField_Type'] == 'DropDownCustomField': drop_down_custom_field_strings = [] if 'options' in custom_field: drop_down_custom_field_strings = [ custom_field_option['displayName'] for custom_field_option in custom_field['options'] ] print( 'Drop-down custom field with ID \'%s\', name \'%s\', and options ' '{%s} was found.' % (custom_field['id'], custom_field['name'], ','.join(drop_down_custom_field_strings))) else: print('Custom field with ID \'%s\' and name \'%s\' was found' % (custom_field['id'], custom_field['name'])) print print 'Number of results found: %s' % len(custom_fields)
def main(client): # Initialize appropriate service. custom_field_service = client.GetService('CustomFieldService', version='v201311') # Create statement to select only custom fields that apply to line items. values = [{ 'key': 'entityType', 'value': { 'xsi_type': 'TextValue', 'value': 'LINE_ITEM' } }] query = 'WHERE entityType = :entityType' # Get custom fields by statement. custom_fields = DfpUtils.GetAllEntitiesByStatementWithService( custom_field_service, query=query, bind_vars=values) # Display results. for custom_field in custom_fields: print('Custom field with ID \'%s\' and name \'%s\' was found.' % (custom_field['id'], custom_field['name'])) print print 'Number of results found: %s' % len(custom_fields)
def main(client, line_item_id): # Initialize appropriate service. lica_service = client.GetService( 'LineItemCreativeAssociationService', version='v201311') # Create statement object to only select LICAs for the given line item id. values = [{ 'key': 'lineItemId', 'value': { 'xsi_type': 'NumberValue', 'value': line_item_id } }] query = 'WHERE lineItemId = :lineItemId' statement = DfpUtils.FilterStatement(query, values) while True: # Get LICAs by statement. response = lica_service.GetLineItemCreativeAssociationsByStatement( statement.ToStatement())[0] licas = response.get('results') if licas: # Display results. for lica in licas: print ('LICA with line item id \'%s\', creative id \'%s\', and status ' '\'%s\' was found.' % (lica['lineItemId'], lica['creativeId'], lica['status'])) statement.IncreaseOffsetBy(DfpUtils.PAGE_LIMIT) else: break print '\nNumber of results found: %s' % response['totalResultSetSize']
def main(client, company_id): # Initialize appropriate service. company_service = client.GetService('CompanyService', version='v201405') # Create statement object to only select a single company by ID. values = [{ 'key': 'id', 'value': { 'xsi_type': 'NumberValue', 'value': company_id } }] query = 'WHERE id = :id' statement = DfpUtils.FilterStatement(query, values, 1) # Get companies by statement. response = company_service.GetCompaniesByStatement( statement.ToStatement())[0] companies = response.get('results') if companies: # Display results. for company in companies: company['comment'] += ' Updated.' # Update the companies on the server. companies = company_service.UpdateCompanies(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, order_id): # Initialize appropriate service. report_service = client.GetService('ReportService', version='v201408') # Create statement object to filter for an order. values = [{ 'key': 'id', 'value': { 'xsi_type': 'NumberValue', 'value': order_id } }] filter_statement = {'query': 'WHERE ORDER_ID = :id', 'values': values} # Create report job. report_job = { 'reportQuery': { 'dimensions': ['ORDER_ID', 'ORDER_NAME'], 'dimensionAttributes': ['ORDER_TRAFFICKER', 'ORDER_START_DATE_TIME', 'ORDER_END_DATE_TIME'], 'statement': filter_statement, 'columns': ['AD_SERVER_IMPRESSIONS', 'AD_SERVER_CLICKS', 'AD_SERVER_CTR', 'AD_SERVER_CPM_AND_CPC_REVENUE', 'AD_SERVER_WITHOUT_CPD_AVERAGE_ECPM'], 'dateRangeType': 'LAST_MONTH' } } # Run report. report_job = report_service.RunReportJob(report_job)[0] # Wait for report to complete. status = report_job['reportJobStatus'] while status != 'COMPLETED' and status != 'FAILED': print 'Report job with \'%s\' id is still running.' % report_job['id'] time.sleep(30) status = report_service.GetReportJob(report_job['id'])[0]['reportJobStatus'] if status == 'FAILED': print ('Report job with id \'%s\' failed to complete successfully.' % report_job['id']) else: # Change to your preferred export format. export_format = 'CSV_DUMP' report_file = tempfile.NamedTemporaryFile(suffix='.csv.gz', delete=False) # Download report data. DfpUtils.DownloadReportToFile( report_job['id'], export_format, report_service, report_file) report_file.close() # Display results. print 'Report job with id \'%s\' downloaded to:\n%s' % ( report_job['id'], report_file.name)
def main(client, key_id): # Initialize appropriate service. custom_targeting_service = client.GetService('CustomTargetingService', version='v201311') filter_values = [{ 'key': 'keyId', 'value': { 'xsi_type': 'NumberValue', 'value': key_id } }] query = 'WHERE customTargetingKeyId = :keyId' statement = DfpUtils.FilterStatement(query, filter_values) deleted_custom_targeting_values = 0 # Get custom targeting values. while True: response = custom_targeting_service.GetCustomTargetingValuesByStatement( statement.ToStatement())[0] values = response.get('results') if values: value_ids = [value['id'] for value in values] action = {'type': 'DeleteCustomTargetingValues'} value_query = ('WHERE customTargetingKeyId = :keyId ' 'AND id IN (%s)' % ', '.join(value_ids)) value_statement = DfpUtils.FilterStatement(value_query, filter_values) # Delete custom targeting values. result = custom_targeting_service.PerformCustomTargetingValueAction( action, value_statement.ToStatement())[0] if result and int(result['numChanges']) > 0: deleted_custom_targeting_values += int(result['numChanges']) statement.IncreaseOffsetBy(DfpUtils.PAGE_LIMIT) else: break if deleted_custom_targeting_values > 0: print('Number of custom targeting values deleted: %s' % deleted_custom_targeting_values) else: print 'No custom targeting values were deleted.'
def testGetAllEntitiesByStatement(self): """Test whether GetAllEntitiesByStatement() does what it suppose to.""" users = DfpUtils.GetAllEntitiesByStatement( client, 'User', 'ORDER BY name', server=self.__class__.SERVER, version=self.__class__.VERSION, http_proxy=HTTP_PROXY) self.assert_(isinstance(users, list))
def main(client, key_name): # Initialize appropriate service. custom_targeting_service = client.GetService('CustomTargetingService', version='v201311') values = [{ 'key': 'name', 'value': { 'xsi_type': 'TextValue', 'value': key_name } }] query = 'WHERE name = :name' statement = DfpUtils.FilterStatement(query, values) deleted_custom_targeting_keys = 0 # Get custom targeting keys. while True: response = custom_targeting_service.GetCustomTargetingKeysByStatement( statement.ToStatement())[0] keys = response.get('results') if keys: key_ids = [key['id'] for key in keys] action = {'type': 'DeleteCustomTargetingKeys'} key_query = 'WHERE id IN (%s)' % ', '.join(key_ids) key_statement = DfpUtils.FilterStatement(key_query) # Delete custom targeting keys. result = custom_targeting_service.PerformCustomTargetingKeyAction( action, key_statement.ToStatement())[0] if result and int(result['numChanges']) > 0: deleted_custom_targeting_keys += int(result['numChanges']) statement.IncreaseOffsetBy(DfpUtils.PAGE_LIMIT) else: break if deleted_custom_targeting_keys > 0: print('Number of custom targeting keys deleted: %s' % deleted_custom_targeting_keys) else: print 'No custom targeting keys were deleted.'
def main(client, line_item_id): # Initialize appropriate service. lica_service = client.GetService('LineItemCreativeAssociationService', version='v201403') # Create query. values = [{ 'key': 'lineItemId', 'value': { 'xsi_type': 'NumberValue', 'value': line_item_id } }, { 'key': 'status', 'value': { 'xsi_type': 'TextValue', 'value': 'ACTIVE' } }] query = 'WHERE lineItemId = :lineItemId AND status = :status' statement = DfpUtils.FilterStatement(query, values) num_deactivated_licas = 0 # Get LICAs by statement. while True: response = lica_service.GetLineItemCreativeAssociationsByStatement( statement.ToStatement())[0] licas = response.get('results') if licas: for lica in licas: for lica in licas: print( 'LICA with line item id \'%s\', creative id \'%s\', and status' ' \'%s\' will be deactivated.' % (lica['lineItemId'], lica['creativeId'], lica['status'])) # Perform action. result = lica_service.PerformLineItemCreativeAssociationAction( {'type': 'DeactivateLineItemCreativeAssociations'}, statement.ToStatement())[0] if result and int(result['numChanges']) > 0: num_deactivated_licas += int(result['numChanges']) statement.IncreaseOffsetBy(DfpUtils.PAGE_LIMIT) else: break # Display results. if num_deactivated_licas > 0: print 'Number of LICAs deactivated: %s' % num_deactivated_licas else: print 'No LICAs were deactivated.'
def main(client, label_id): # Initialize appropriate service. creative_wrapper_service = client.GetService('CreativeWrapperService', version='v201403') # Create a query to select the active creative wrappers for the given label. values = [{ 'key': 'labelId', 'value': { 'xsi_type': 'NumberValue', 'value': label_id } }, { 'key': 'status', 'value': { 'xsi_type': 'TextValue', 'value': 'ACTIVE' } }] query = 'WHERE status = :status AND labelId = :labelId' statement = DfpUtils.FilterStatement(query, values) creative_wrappers_deactivated = 0 # Get creative wrappers by statement. while True: response = creative_wrapper_service.GetCreativeWrappersByStatement( statement.ToStatement())[0] creative_wrappers = response.get('results') if creative_wrappers: # Display results. for creative_wrapper in creative_wrappers: print( 'Creative wrapper with ID \'%s\' applying to label \'%s\' with ' 'status \'%s\' will be deactivated.' % (creative_wrapper['id'], creative_wrapper['labelId'], creative_wrapper['status'])) # Perform action. result = creative_wrapper_service.PerformCreativeWrapperAction( {'type': 'DeactivateCreativeWrappers'}, statement.ToStatement())[0] if result and int(result['numChanges']) > 0: creative_wrappers_deactivated += int(result['numChanges']) statement.IncreaseOffsetBy(DfpUtils.PAGE_LIMIT) else: break # Display results. if creative_wrappers_deactivated > 0: print('Number of creative wrappers deactivated: %s' % creative_wrappers_deactivated) else: print 'No creative wrappers were deactivated.'