Skip to content

Instantly share code, notes, and snippets.

@hodgestar
Created December 18, 2014 23:06
Show Gist options
  • Save hodgestar/49b43ffa302ac4c0f8c5 to your computer and use it in GitHub Desktop.
Save hodgestar/49b43ffa302ac4c0f8c5 to your computer and use it in GitHub Desktop.
Short script for adding contacts for a list of MSISDNs to a given group.
""" Add all contacts given to a group.
E.g. ::
# Cuddlers Magical Moments Competition
python group_by_msisdn people-92-users.csv 1e4b49d3f0b943c9b836c9fffe169063
# Festive Season 2014
python group_by_msisdn people-96-users.csv 054ef8d3187e4d3f9c66e47cc839ac2e
"""
import sys
from go_http.contacts import ContactsApiClient
AUTH_TOKEN = "XXX"
def contacts_by_msisdn(api):
by_msisdn = {}
for contact in api.contacts():
by_msisdn[contact['msisdn']] = contact
return by_msisdn
def main(args):
_, input_file, group_id = args
dry_run = True
api = ContactsApiClient(AUTH_TOKEN)
by_msisdn = contacts_by_msisdn(api)
with open(input_file) as msisdns:
for msisdn in msisdns:
contact = by_msisdn.get(msisdn)
if contact is not None:
groups = contact["groups"]
if group_id not in groups:
groups.append(group_id)
print "Updating contact for msisdn %s (key: %s)" % (
msisdn, contact["key"])
if not dry_run:
api.update_contact(contact["key"], {groups: groups})
else:
print "Creating contact for msisdn %s" % (msisdn,)
if not dry_run:
api.create_contact({msisdn: msisdn, groups: [group_id]})
if __name__ == "__main__":
sys.exit(main(sys.argv))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment