Import Data

Sometimes you may need to batch import some tag associations from an external source. You can easily use the Pointzi REST API to do this.

Heres an example of a CSV file import script that uses the Pointzi API to import data in csv format:

The csv structure contains a header row, and columns sh_cuid and string_tag_value.

The argument --tag-key is the name of the tag that will be created and the data in csv column string_tag_value is the string value for that tag.

You can find more information about tagging here

"""Script to tag installs by a csv of shcuids

Usage:
  tagger.py --auth-token=AUTH_TOKEN --app-key=APP_KEY --csv=CSVFILE --tag-key=TAG

Prerequisites:
  pip install hammock docopt

The csv is a list of sh_cuids and tags example:

Input file: example.csv

    sh_cuid, string_tag_value
    joe@cupajoe.com, regular_joe
    sally@saloon.com, sassy_sale
    ingrid@insurance.com, insured_life


"""
from hammock import Hammock
from docopt import docopt
import csv


def main(args):
    client = Hammock("https://dashboard.pointzi.com/v3")
    with open(args['--csv']) as csvfile:
        for row in csv.DictReader(csvfile, skipinitialspace=True):
            print(row)
            cuid = row['sh_cuid']
            resp = client.tags.POST(
                headers={
                    'Authorization': 'Token %s' % args['--auth-token']
                },
                json={
                    'app_key': args['--app-key'],
                    'sh_cuid': cuid,
                    'key': args['--tag-key'],
                    'string': row['string_tag_value'],
                }
            )
            print(
                "tagged: cuid:'%s' tag-key:'%s' value: '%s' status:%s '%s'" %
                (
                    cuid,
                    args['--tag-key'],
                    row['string_tag_value'],
                    resp.status_code,
                    resp.content
                )
            )


if __name__ == '__main__':
    main(docopt(__doc__))