def main(): username = input('Username: '******'Error: Invalid username and/or password.') audience_id = input('Audience ID: ') response = lotame.get(f'reports/audiences/{audience_id}/publisher') status = response.status_code if status != 200: print('Error retrieving contribution report.') sys.exit() report = response.json() columns = report['reportColumns'] with open('pub_contribution_report.csv', 'w', newline='') as csvfile: report_writer = csv.writer(csvfile, delimiter=',') report_writer.writerow(columns) for publisher in report['stats']: publisher_stats = [] for column in columns: publisher_stats.append(publisher[column]) report_writer.writerow(publisher_stats)
def main(): username = input('Username: '******'Error: Invalid username and/or password.') sys.exit() hierarchy_id = input('Hierarchy ID: ') endpoint = f'hierarchies/{hierarchy_id}/nodes?depth=2' response = lotame.get(endpoint) if response.status_code != 200: print('Error: Could not find hierarchy.') lotame.cleanup() sys.exit() nodes = response.json()['nodes'] node_ids = [] ''' Get the ID of the top nodes and then check for children.''' for node in nodes: node_ids.append(node['id']) get_child_nodes(node, node_ids) for node_id in node_ids: print(node_id) lotame.cleanup()
def main(): if len(sys.argv) != 2: print(f'Usage: python {sys.argv[0]} audience_ids.txt') sys.exit() username = input('Username: '******'Error: Invalid username and/or password.') sys.exit() recency = input('Recency (in days): ') recency = str(int(recency) * 24 * 60) # Convert secs to days filename = sys.argv[1] with open(filename) as audience_ids: for audience_id in audience_ids: audience_id = audience_id.strip() info = lotame.get(f'audiences/{audience_id}').json() component = info['definition']['component'] add_recency(component, recency) info['definition']['component'] = component response = lotame.put(f'audiences/{audience_id}', info) status = response.status_code print(f'Audience {audience_id} | HTTP {status}') lotame.cleanup()
def main(): username = input('Username: '******'Error: Invalid username and/or password.') sys.exit() audience_id = input('Audience ID: ') # Get the audience info from the Lotame API response = lotame.get(f'audiences/{audience_id}') # Pull the resulting JSON from the Response object and get the desired # values from it audience_info = response.json() audience_name = audience_info['name'] definition = audience_info['definition']['component'] # Delete the ticket-granting ticket, now that the script is done with it lotame.cleanup() # If there are any nested groups of behaviors in the audience definition, # the best way to pull them out is with a recursive function, which is why # we define find_behaviors(definition, behaviors) behaviors = {} find_behaviors(definition, behaviors) print('Behaviors in ' + audience_name) for behavior_id in behaviors: print(behavior_id + '\t' + behaviors[behavior_id])
def main(): if len(sys.argv) != 2: print(f'Usage: python {sys.argv[0]} audience_ids.txt') sys.exit() username = input('Username: '******'Error: Incorrect username and/or password.') sys.exit() filename = sys.argv[1] with open(filename) as file: for audience_id in file: audience_id = audience_id.strip() info = lotame.get(f'audiences/{audience_id}').json() if info['generate_apr']: print(f'APR already active for audience {audience_id}') else: info['generate_apr'] = True response = lotame.put(f'audiences/{audience_id}', info) status = response.status_code print(f'Audience {audience_id} | HTTP {status}') lotame.cleanup()
def get_behavior_client_id(behavior_id): response = lotame.get(f'behaviors/{behavior_id}') status = response.status_code if status != 200: return None return response.json()['clientId']
def is_valid_behavior_id(behavior_id): response = lotame.get(f'behaviors/{behavior_id}') status = response.status_code if status != 200: return False return True
def get_audience_info(audience_id): response = lotame.get(f'audiences/{audience_id}') status = response.status_code if status != 200: return None return response.json()
def get_audience_info(audience_id): """Grabs audience info from the API.""" response = lotame.get(f'audiences/{audience_id}') status = response.status_code if status != 200: return None return response.json()
def get_categorized(node_id, client_id): endpoint = f'hierarchies/nodes/{node_id}/categorizedBehaviors' \ f'?client_id={client_id}' response = lotame.get(endpoint) status = response.status_code if status != 200: return None return response.json()
def get_apr_gender_percents(audience_id): response = lotame.get(f'reports/audiences/{audience_id}/profile/type/6') status = response.status_code if status != 200: return None apr_info = response.json() gender_percents = {} gender_percents['audience_name'] = apr_info['audienceName'] for affinity in apr_info['audienceAffinities']: gender = affinity['behaviorName'] percent = str(round(float(affinity['compositional30']), 2)) gender_percents[gender] = percent return gender_percents
def main(): if len(sys.argv) != 2: print(f'Usage: python {sys.argv[0]} audience_ids.txt') sys.exit() username = input('Username: '******'Error: Username and/or password invalid.') sys.exit() print('Change all audiences to...') print('1. Enrich') print('2. Extend') audience_type = 0 while audience_type not in [1, 2]: audience_type = int(input('Choose: ')) filename = sys.argv[1] with open(filename) as file: for audience_id in file: audience_id = audience_id.strip() info = lotame.get('audiences/{audience_id}').json() # Set appropriate option, or skip to next audience if already set if audience_type == 1: if info['overlapOnly']: print('Audience {audience_id} already correct.') continue info['overlapOnly'] = True # Enrich else: if not info['overlapOnly']: print('Audience {audience_id} already correct.') continue info['overlapOnly'] = False # Extend status = lotame.put('audiences/{audience_id}', info).status_code print('Audience {audience_id} | HTTP {status}') lotame.cleanup()
def main(): if len(sys.argv) == 1: print(f'Usage: python {sys.argv[0]} aliases.xlsx') sys.exit() username = input('Username: '******'Error: Invalid username or password.') sys.exit() option = 0 while option not in ['1', '2']: print('Select option:') print('1. Replace variants') print('2. Append variants') option = input('Option: ') filename = sys.argv[1] workbook = openpyxl.load_workbook(filename) sheet_names = workbook.get_sheet_names() sheet = workbook.get_sheet_by_name(sheet_names[0]) for row in range(2, sheet.max_row + 1): behavior_id = str(sheet[f'A{row}'].value) new_alias = str(sheet[f'B{row}'].value) endpoint = f'behaviors/{behavior_id}/aliases' info = lotame.get(endpoint).json() if option == '1': # Replace info['alias'] = [new_alias] else: # Append info['alias'].append(new_alias) status = lotame.put(endpoint, info).status_code print(f'Behavior {behavior_id} | HTTP {status}') lotame.cleanup()
def get_monthly_uniques(behavior_id, network, date): client_id = get_behavior_client_id(behavior_id) if not client_id: return None network = bool(network == 'y') endpoint = f'statistics/behaviors/{behavior_id}/aggregated' \ f'?client_as_group={network}&ref_date={date}' \ f'&stat_interval=FULL_MONTH&client_id={client_id}' \ '&universe_id=1' response = lotame.get(endpoint) status = response.status_code if status != 200: return None return response.json()['uniques']
def main(): if len(sys.argv) != 2: print(f'Usage: python {sys.argv[0]} campaign_ids.txt') sys.exit() username = input('Username: '******'Error: Invalid username and/or password.') sys.exit() filename = sys.argv[1] with open(filename) as campaign_file: campaign_ids = [campaign_id.strip() for campaign_id in campaign_file] for campaign_id in campaign_ids: response = lotame.get(f'campaigns/{campaign_id}') status = response.status_code if status != 200: print(f'Could not retrieve campaign {campaign_id}') continue campaign_info = response.json() campaign_info['activateStats'] = False response = lotame.put(f'campaigns/{campaign_id}', campaign_info) status = response.status_code if status == 200: print(f'Deactivated stats for campaign {campaign_id}') else: print(f'Could not deactivate stats for campaign {campaign_id}') lotame.cleanup()
def main(): # Exit if there's no argument passed in if len(sys.argv) != 2: print(f'Usage: python {sys.argv[0]} behavior_ids.txt') sys.exit() username = input('Username: '******'Error: Invalid username and/or password.') sys.exit() # Open the user-provided list of behavior IDs and go through each line filename = sys.argv[1] with open(filename) as behavior_ids: for behavior_id in behavior_ids: # Get rid of the newline character at the end of each line, since # this will confuse the API behavior_id = behavior_id.strip() # Get the behavior info from the Lotame API response = lotame.get(f'behaviors/{behavior_id}') # Pull the resulting JSON from the Response object and get the # value from the 'name' key behavior_info = response.json() behavior_name = behavior_info['name'] print(behavior_id + '\t' + behavior_name) # Delete the ticket-granting ticket, now that the script is done with it lotame.cleanup()
def is_valid_behavior(behavior_id): status = lotame.get(f'behaviors/{behavior_id}').status_code return bool(status == 200)
def campaign_exists(campaign_id): status = lotame.get(f'campaigns/{campaign_id}').status_code return bool(status == 200)