def main():
    username = input('Username: '******'Error: Invalid username and/or password.')
        sys.exit()

    client_id = input('Client ID: ')
    audience_name = input('New Audience Name: ')

    # Put the two behavior IDs in a list for future iteration
    behavior_ids = []
    behavior_ids.append(input('First Behavior ID: '))
    behavior_ids.append(input('Second Behavior ID: '))

    behaviors = []
    first_behavior = True
    for behavior_id in behavior_ids:
        # We can't give the first behavior a relationship, because there's
        # nothing behind it that it can relate to
        if first_behavior:
            relationship = None
            first_behavior = False
        # But we can define how the second behavior relates to the first. Note
        # that AND or OR *must* be in all caps, or you'll get a 500 error
        else:
            relationship = 'AND'

        # Puts the behavior IDs into a JSON skeleton, preparing them to be
        # passed into the audience JSON
        behavior = create_behavior_definition(behavior_id, relationship)
        behaviors.append(behavior)

    audience = {
        'clientId': client_id,
        'name': audience_name,
        'overlapOnly': True,  # True for Enrich, False for Extend
        'generate_apr': False,  # Be sure to set whether APR should be enabled
        'Client': {
            'id': client_id
        },
        'definition': {
            'component': behaviors
        }
    }

    # Use the above audience JSON to create the new audience
    new_audience = lotame.post('audiences', audience).json()

    # Print out the ID of the new audience
    new_audience_id = new_audience['id']
    print('New audience created with ID ' + new_audience_id)

    # Delete the ticket-granting ticket, now that we're done with it
    lotame.cleanup()
Exemplo n.º 2
0
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()
Exemplo n.º 3
0
def main():
    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()

    filename = sys.argv[1]
    with open(filename) as behavior_file:
        behavior_ids = [behavior_id.strip() for behavior_id in behavior_file]

    info = {'behaviors': {'behavior': []}}

    for behavior_id in behavior_ids:
        behavior = {'id': behavior_id}
        info['behaviors']['behavior'].append(behavior)

    status = lotame.put('behaviors/ignored', info).status_code
    if status == 204:
        print('Successfully ignored behaviors.')
    else:
        print('Error: Could not ignore behaviors.')

    lotame.cleanup()
def main():
    if len(sys.argv) != 2:
        print(f'Usage: python {sys.argv[0]} campaigns.csv')
        sys.exit()

    username = input('Username: '******'Error: Invalid username and/or password')
        sys.exit()

    filename = sys.argv[1]
    with open(filename) as csv_file:
        reader = csv.reader(csv_file)

        #Skip header
        next(reader, None)

        for row in reader:
            campaign_id = row[0]
            interaction_id = row[1]

            if delete_interaction(campaign_id, interaction_id):
                message = f'Deleted interaction {interaction_id} from ' \
                          f'campaign {campaign_id}'
            else:
                message = f'Could not delete interaction {interaction_id} ' \
                          f'from campaign {campaign_id}'
            print(message)

    lotame.cleanup()
Exemplo n.º 5
0
def main():
    if len(sys.argv) != 2:
        print(f'Usage: python {sys.argv[0]} behaviors.xlsx')
        sys.exit()

    username = input('Username: '******'A{row}'].value)
        description = str(sheet[f'B{row}'].value)
        client_id = str(sheet[f'C{row}'].value)
        type_id = str(sheet[f'D{row}'].value)

        options = {
            'name': name,
            'description': description,
            'clientId': client_id,
            'behaviorTypeId': type_id
        }

        response = lotame.post('behaviors?use_aliases=true', options)
        print(f'{name} | {response.status_code}')

    lotame.cleanup()
Exemplo n.º 6
0
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])
Exemplo n.º 7
0
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)
Exemplo n.º 8
0
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()
Exemplo n.º 9
0
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]} audiences.csv')
        sys.exit()

    username = input('Username: '******'Error: Invalid username and/or password.')
        sys.exit()

    filename = sys.argv[1]
    with open(filename) as csv_file:
        reader = csv.reader(csv_file)

        # Skip header row
        next(reader, None)

        for row in reader:
            skip_row = False

            audience_id = row[0]
            old_behavior_id = row[1]
            new_beahvior_id = row[2]

            audience_info = get_audience_info(audience_id)

            for behavior_id in [old_behavior_id, new_beahvior_id]:
                if not is_valid_behavior_id(behavior_id):
                    print(f'Error: {behavior_id} is not a valid behavior ID')
                    skip_row = True

            if skip_row:
                continue

            component = audience_info['definition']['component']
            success = replace_behavior(component, old_behavior_id,
                                       new_beahvior_id)

            if not success:
                print(
                    f'Error: Couldn\'t find {old_behavior_id} in audience {audience_id}'
                )
                continue

            audience_info['definition']['component'] = component

            if set_audience_info(audience_id, audience_info):
                print(
                    f'Updated audience {audience_id} with behavior {new_beahvior_id}'
                )
            else:
                print(
                    f'Error: Couldn\'t update audience {audience_id} with {new_beahvior_id}'
                )

    lotame.cleanup()
Exemplo n.º 11
0
def main():
    if len(sys.argv) != 2:
        print(f'Usage: python {sys.argv[0]} pixel_ids.txt')
        sys.exit()

    username = input('Username: '******'Error: Invalid username and/or password.')
        sys.exit()

    filename = sys.argv[1]
    with open(filename) as pixel_ids:
        for pixel_id in pixel_ids:
            pixel_id = pixel_id.strip()

            status = lotame.delete(f'pixels/{pixel_id}').status_code

            if status == 204:
                print(f'Deleted/paused pixel {pixel_id}')
            else:
                print(f'Could not delete/pause pixel {pixel_id}')

    lotame.cleanup()
def main():
    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()

    filename = sys.argv[1]
    with open(filename) as behavior_file:
        behavior_ids = [behavior_id.strip() for behavior_id in behavior_file]

    valid_months = [str(month) for month in range(1, 13)]
    prompt = 'Enter a month (numeric): '
    month = get_choice(prompt, valid_months)
    year = get_choice('Enter a year: ')
    date = f'{year}{month}01'

    prompt = 'Are these network clients? (y/n) '
    network = get_choice(prompt, ['y', 'n'])

    behavior_stats = []
    print('Grabbing stats...')
    for behavior_id in behavior_ids:
        uniques = get_monthly_uniques(behavior_id, network, date)

        if not uniques:
            print(f'Error: Couldn\'t get uniques for {behavior_id}')
            continue

        behavior_stat = {'behavior_id': behavior_id, 'uniques': uniques}

        behavior_stats.append(behavior_stat)

    lotame.cleanup()

    if not behavior_stats:
        print('Couldn\'t get any stats')
        sys.exit()

    filename = f'monthly_stats_{date}.csv'
    with open(filename, 'w') as statfile:
        writer = csv.writer(statfile, delimiter='\t')
        writer.writerow(['Behavior ID', 'Monthly Uniques'])

        for behavior_stat in behavior_stats:
            behavior_id = behavior_stat['behavior_id']
            uniques = behavior_stat['uniques']

            writer.writerow([behavior_id, uniques])

    print(f'Stats written to {filename}')
def main():
    if len(sys.argv) != 2:
        print(f'Usage: python {sys.argv[0]} audiences.xlsx')
        sys.exit()

    username = input('Username: '******'Error: Invalid username and/or password')
        sys.exit()

    print('Relationship?')
    print('1. AND')
    print('2. OR')
    relationship = choice('Choose: ', ['1', '2'])

    if relationship == '1':
        relationship = 'AND'
    else:
        relationship = 'OR'

    filename = sys.argv[1]
    with open(filename) as csv_file:
        reader = csv.reader(csv_file)

        # Skip header row
        next(reader, None)

        for row in reader:
            audience_id = row.pop(0)

            audience_info = get_audience_info(audience_id)
            if not audience_info:
                print(f'Error: Could not find audience {audience_id}')
                continue

            behaviors = []
            for behavior_id in row:
                if behavior_id == '':
                    break
                if not behaviors:
                    behavior = create_behavior_definition(behavior_id, None)
                else:
                    behavior = create_behavior_definition(
                        behavior_id, relationship)
                behaviors.append(behavior)

            audience_info['definition']['component'] = behaviors
            if put_audience_info(audience_id, audience_info):
                print(f'Updated audience {audience_id}')
            else:
                print(f'Error: Could not update audience {audience_id}')

    lotame.cleanup()
Exemplo n.º 14
0
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) != 2:
        print(f'Usage: python {sys.argv[0]} nodes.xlsx')
        sys.exit()

    username = input('Username: '******'Error: Invalid username and/or password.')
        sys.exit()

    client_id = input('Hierarchy owner ID: ')

    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):
        original_id = str(sheet[f'A{row}'].value)
        duplicate_id = str(sheet[f'B{row}'].value)

        categorized = get_categorized(original_id, client_id)
        if not categorized['behavior']:
            print(f'Nothing to categorize from {original_id} to {duplicate_id}')
            continue

        del categorized['totalRows']

        for behavior in categorized['behavior']:
            del behavior['created']
            del behavior['modified']
            del behavior['categories']

        success = categorize(duplicate_id, categorized)
        if success:
            print(f'Copied from {original_id} to {duplicate_id}')
        else:
            print(f'Error copying from {original_id} to {duplicate_id}')

    lotame.cleanup()
Exemplo n.º 16
0
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()
Exemplo n.º 17
0
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()

    filename = sys.argv[1]
    with open(filename) as audience_file:
        audience_ids = [audience_id.strip() for audience_id in audience_file]

    today = datetime.today().strftime('%Y%m%d')
    outfile_name = f'audience_apr_genders_{today}.csv'

    with open(outfile_name, 'w') as outfile:
        writer = csv.writer(outfile, delimiter=',')
        writer.writerow(['Audience ID', 'Audience Name', 'Male', 'Female'])

        for audience_id in audience_ids:
            gender_percents = get_apr_gender_percents(audience_id)

            if not gender_percents:
                print(f'Error: Could not get APR for audience {audience_id}')
                continue

            audience_name = gender_percents['audience_name']
            male_percent = gender_percents['Male'] + '%'
            female_percent = gender_percents['Female'] + '%'

            row = [audience_id, audience_name, male_percent, female_percent]
            writer.writerow(row)

    print(f'Results exported to {outfile_name}')
    lotame.cleanup()
Exemplo n.º 18
0
def main():
    if len(sys.argv) != 2:
        print(f'Usage: python {sys.argv[0]} interactions.csv')
        sys.exit()

    username = input('Username: '******'Error: Invalid username and/or password')
        sys.exit()

    filename = sys.argv[1]
    with open(filename) as csv_file:
        reader = csv.reader(csv_file)

        # Skip header row
        next(reader, None)

        for row in reader:
            campaign_id = row[0]
            behavior_id = row[1]
            interaction_type_id = row[2]

            if not campaign_exists(campaign_id):
                print(f'Error: Cannot find campaign {campaign_id}')
                continue

            created = add_interaction(campaign_id, behavior_id,
                                      interaction_type_id)

            if created:
                print(f'Added {behavior_id} to campaign {campaign_id}')
            else:
                print(
                    f'Error: Could not add {behavior_id} to campaign {campaign_id}'
                )

    lotame.cleanup()
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():
    if len(sys.argv) != 2:
        print(f'Usage: python {sys.argv[0]} behaviors.xlsx')
        sys.exit()

    username = input('Username: '******'Error: Invalid username or password.')
        sys.exit()

    # Prep to open given Excel file
    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):
        # Get node and behavior for the row
        node_id = str(sheet[f'A{row}'].value)
        behavior_id = str(sheet[f'B{row}'].value)

        endpoint = 'hierarchies/nodes/' + \
            node_id + '/categorizedBehaviors?behavior_id=' + \
            behavior_id + '&unscoped=false&include_global=false'

        status = lotame.delete(endpoint).status_code

        if status == 204:
            print(f'Decategorized behavior {behavior_id} from {node_id}')
        else:
            print(
                f'Error decategorizing behavior {behavior_id} from {node_id}')

    lotame.cleanup()
Exemplo n.º 21
0
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()
Exemplo n.º 22
0
def main():
    if len(sys.argv) != 2:
        print(f'Usage: python {sys.argv[0]} audiences.csv')
        sys.exit()

    username = input('Username: '******'Error: Invalid username and/or password')
        sys.exit()

    print('Append options:')
    print('1. AND')
    print('2. OR')
    choice = ''
    while choice not in ['1', '2']:
        choice = input('Choice: ')

    if choice == '1':
        operator = 'AND'
    else:
        operator = 'OR'

    filename = sys.argv[1]
    with open(filename) as csv_file:
        reader = csv.reader(csv_file)

        # Skip header row
        next(reader)

        for row in reader:
            audience_id = row[0]
            behavior_id = row[1]

            audience_info = get_audience_info(audience_id)
            if not audience_info:
                print(f'Error: Audience {audience_id} not found')
                continue

            if not is_valid_behavior(behavior_id):
                print(f'Error: Behavior {behavior_id} not found')
                continue

            behavior = {
                'operator': operator,
                'complexAudienceBehavior': {
                    'behavior': {
                        'id': behavior_id
                    }
                }
            }

            audience_info['definition']['component'].append(behavior)
            if set_audience_info(audience_id, audience_info):
                print(f'Updated audience {audience_id}')
            else:
                print(f'Error: Could not update audience {audience_id}')

    lotame.cleanup()