Import Data

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

Heres an example of a CSV file import script that uses the Contextual 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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"""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.contextu.al/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__))

Last update: 2023-04-12