def resubscribe(self): cs = CreateSend( {"api_key": Configuration.instance.get_setting("campaign_monitor_api_key")} ) now = datetime.now() for client in cs.clients(): client = Client( {"api_key": Configuration.instance.get_setting("campaign_monitor_api_key")}, client.ClientID ) for list in client.lists(): subscriber = Subscriber( {"api_key": Configuration.instance.get_setting("campaign_monitor_api_key")} ) try: response = subscriber.get(list.ListID, self.email) except BadRequest: continue date = datetime.strptime(response.Date, '%Y-%m-%d %H:%M:%S') diff = now - date if response.State != "Active" and ( date > now or diff.seconds < self.max_seconds ): response = subscriber.add( list.ListID, self.email, response.Name, None, True ) self.resubscribed_lists.append(list.ListID)
def sync_cm_list(sender, instance=None, created=False, **kwargs): """On Group Add: if group name does not exist on C. M, add a list to C. M. add an entry to listmap On Group Edit: if group exists on C. M., if list.name <> group.name, update list name else: add a list on C. M. add an entry to listmap """ cl = Client(cm_client_id) lists = cl.lists() list_ids = [list.ListID for list in lists] list_names = [list.Name for list in lists] list_ids_d = dict(zip(list_names, list_ids)) list_d = dict(zip(list_ids, lists)) if created: if instance.name in list_names: list_id = list_ids_d[instance.name] else: list_id = get_or_create_cm_list(cm_client_id, instance) if list_id: # add an entry to the listmap listmap_insert(instance, list_id) else: # update try: # find the entry in the listmap list_map = ListMap.objects.get(group=instance) list_id = list_map.list_id except ListMap.DoesNotExist: if instance.name in list_names: list_id = list_ids_d[instance.name] else: # hasn't be created on C. M. yet. create one list_id = get_or_create_cm_list(cm_client_id, instance) if list_id: listmap_insert(instance, list_id) # if the list title doesn't match with the group name, update the list title if list_id and list_id in list_ids: list = list_d[list_id] if instance.name != list.Name: list = List(list_id) list.update(instance.name, "", False, "")
def list_names(client_id): my_client = Client(a_test_client_auth, client_id=client_id) results = my_client.lists() list_names_dir = {} list_names = [] for r in results: list_names_dir[r.Name] = r.ListID list_names.append(r.Name) if 'Main List' not in list_names: create_new_list(clientid=client_id) return (list_names_dir['Main List'])
def handle_noargs(self, **options): client = CSClient(auth=settings.CREDENTIALS, client_id=settings.CLIENT_ID) lists = client.lists() for list_id in lists: list = CSList(list_id) import pdb; pdb.set_trace() data = list.list_id # WTF?! List.objects.create( cm_id=data.ListID, title=data.Name, )
def handle_noargs(self, **options): CreateSend.api_key = settings.API_KEY client = CSClient(client_id=settings.CLIENT_ID) lists = client.lists() for list_id in lists: list = CSList(list_id) import pdb; pdb.set_trace() data = list.list_id # WTF?! List.objects.create( cm_id=data.ListID, title=data.Name, )
def handle_noargs(self, **options): CreateSend.api_key = settings.API_KEY client = CSClient(client_id=settings.CLIENT_ID) lists = client.lists() for list_id in lists: list = CSList(list_id) import pdb pdb.set_trace() data = list.list_id # WTF?! List.objects.create( cm_id=data.ListID, title=data.Name, )
def list_names(client_id): my_client=Client(account_auth, client_id=client_id) results=my_client.lists() list_names_dir={} list_names=[] for r in results: list_names_dir[r.Name]=r.ListID list_names.append(r.Name) print(list_names) if 'Main List' not in list_names: print("it's not there!") create_new_list(client_id=client_id, list_name="Main List") else: print("we see it!")
def handle(self, **options): client = CSClient(auth=settings.CREDENTIALS, client_id=settings.CLIENT_ID) lists = client.lists() for data in lists: # get or create the list list_obj, created = List.objects.get_or_create( cm_id=data.ListID, title=data.Name, ) cs_list = CSList(auth=settings.CREDENTIALS, list_id=data.ListID) # get all the active subscribers in this list for active_sub in cs_list.active().Results: subscriber, created = Subscriber.objects.get_or_create( list=list_obj, email_address=active_sub.EmailAddress, defaults={'state': STATE_ACTIVE}) subscriber.name = active_sub.Name subscriber.date = active_sub.Date subscriber.state = STATE_ACTIVE subscriber.save()
def handle(self, *args, **options): from tendenci.apps.user_groups.models import Group from tendenci.apps.profiles.models import Profile from tendenci.addons.campaign_monitor.models import ( ListMap, Campaign, Template, setup_custom_fields) from tendenci.addons.campaign_monitor.utils import sync_campaigns, sync_templates from createsend import (CreateSend, Client, List, Subscriber, BadRequest, Unauthorized) verbosity = 1 if 'verbosity' in options: verbosity = options['verbosity'] def subscribe_to_list(subscriber_obj, list_id, name, email, custom_data): # check if this user has already subscribed, if not, subscribe it try: subscriber = subscriber_obj.get(list_id, email) if str(subscriber.State).lower() == 'active': print name, email, ' - UPDATED' subscriber = subscriber_obj.update(email, name, custom_data, True) except BadRequest as br: print br try: email_address = subscriber_obj.add(list_id, email, name, custom_data, True) if verbosity >= 2: print "%s (%s)" % (name, email) except BadRequest as br: print name, email, ' - NOT ADDED: %s' % br api_key = getattr(settings, 'CAMPAIGNMONITOR_API_KEY', None) client_id = getattr(settings, 'CAMPAIGNMONITOR_API_CLIENT_ID', None) #CreateSend.api_key = api_key auth = {'api_key': api_key} cl = Client(auth, client_id) lists = cl.lists() list_ids = [list.ListID for list in lists] list_names = [list.Name for list in lists] list_ids_d = dict(zip(list_names, list_ids)) groups = Group.objects.filter(status=1, status_detail='active', sync_newsletters=1) listmaps = ListMap.objects.filter(group__sync_newsletters=1) syncd_groups = [listmap.group for listmap in listmaps] cm_list = List(auth) print "Starting to sync groups with campaign monitor..." print for group in groups: if group not in syncd_groups: # get the list id or create a list if not exists # campaing monitor requires the list title if group.name in list_names: list_id = list_ids_d[group.name] else: # add group to the campaign monitor list_id = cm_list.create(client_id, group.name, "", False, "") print "Added group '%s' to the C.M. list." % group.name print # insert to the listmap list_map = ListMap(group=group, list_id=list_id) list_map.save() else: list_map = ListMap.objects.filter(group=group)[0] list_id = list_map.list_id # if a previous added list is deleted on campaign monitor, add it back # TODO: we might need a setting to decide whether we want to add it back or not. a_list = List(list_id) try: list_stats = a_list.stats() # set up custom fields print "Setting up custom fields..." setup_custom_fields(a_list) #num_unsubscribed = list_stats.TotalUnsubscribes #if num_unsubscribed > 0: # # a list of all unsubscribed # unsubscribed_obj = a_list.unsubscribed('2011-5-1') # unsubscribed_emails = [res.EmailAddress for res in unsubscribed_obj.Results] # unsubscribed_names = [res.Name for res in unsubscribed_obj.Results] # unsubscribed_list = zip(unsubscribed_emails, unsubscribed_names) except Unauthorized as e: if 'Invalid ListID' in e: # this list might be deleted on campaign monitor, add it back list_id = cm_list.create(client_id, group.name, "", False, "") # update the list_map list_map.list_id = list_id list_map.save() # sync subscribers in this group print "Subscribing users to the C.M. list '%s'..." % group.name members = group.members.all() for i, member in enumerate(members, 1): # Append custom fields from the profile try: profile = member.profile except Profile.DoesNotExist: profile = None custom_data = [] if profile: fields = [ 'city', 'state', 'zipcode', 'country', 'sex', 'member_number' ] for field in fields: data = {} data['Key'] = field data['Value'] = getattr(profile, field) if not data['Value']: data['Clear'] = True custom_data.append(data) email = member.email name = member.get_full_name() subscriber_obj = Subscriber(auth, list_id, email) subscribe_to_list(subscriber_obj, list_id, name, email, custom_data) print 'Done' print 'Starting to sync campaigns with campaign monitor...' sync_campaigns() print "Done" print 'Syncing templates...' sync_templates() print "Done"
def handle(self, *args, **options): from tendenci.apps.user_groups.models import Group from tendenci.apps.profiles.models import Profile from tendenci.apps.campaign_monitor.models import (ListMap, setup_custom_fields) from tendenci.apps.campaign_monitor.utils import sync_campaigns, sync_templates from createsend import (Client, List, Subscriber, BadRequest, Unauthorized) verbosity = 1 if 'verbosity' in options: verbosity = options['verbosity'] def subscribe_to_list(subscriber_obj, list_id, name, email, custom_data): # check if this user has already subscribed, if not, subscribe it try: subscriber = subscriber_obj.get(list_id, email) if str(subscriber.State).lower() == 'active': print(name, email, ' - UPDATED') subscriber = subscriber_obj.update(email, name, custom_data, True) except BadRequest as br: print(br) try: subscriber_obj.add(list_id, email, name, custom_data, True) # Returns email_address if verbosity >=2: print("%s (%s)" % (name, email)) except BadRequest as br: print(name, email, ' - NOT ADDED: %s' % br) api_key = getattr(settings, 'CAMPAIGNMONITOR_API_KEY', None) client_id = getattr(settings, 'CAMPAIGNMONITOR_API_CLIENT_ID', None) #CreateSend.api_key = api_key auth = {'api_key': api_key} cl = Client(auth, client_id) lists = cl.lists() list_ids = [list.ListID for list in lists] list_names = [list.Name for list in lists] list_ids_d = dict(zip(list_names, list_ids)) groups = Group.objects.filter(status=1, status_detail='active', sync_newsletters=1) listmaps = ListMap.objects.filter(group__sync_newsletters=1) syncd_groups = [listmap.group for listmap in listmaps] cm_list = List(auth) print("Starting to sync groups with campaign monitor...") print() for group in groups: if group not in syncd_groups: # get the list id or create a list if not exists # campaing monitor requires the list title if group.name in list_names: list_id = list_ids_d[group.name] else: # add group to the campaign monitor list_id = cm_list.create(client_id, group.name, "", False, "") print("Added group '%s' to the C.M. list." % group.name) print() # insert to the listmap list_map = ListMap(group=group, list_id=list_id) list_map.save() else: list_map = ListMap.objects.filter(group=group)[0] list_id = list_map.list_id # if a previous added list is deleted on campaign monitor, add it back # TODO: we might need a setting to decide whether we want to add it back or not. a_list = List(auth, list_id) try: #list_stats = a_list.stats() # set up custom fields print("Setting up custom fields...") setup_custom_fields(a_list) #num_unsubscribed = list_stats.TotalUnsubscribes #if num_unsubscribed > 0: # # a list of all unsubscribed # unsubscribed_obj = a_list.unsubscribed('2011-5-1') # unsubscribed_emails = [res.EmailAddress for res in unsubscribed_obj.Results] # unsubscribed_names = [res.Name for res in unsubscribed_obj.Results] # unsubscribed_list = zip(unsubscribed_emails, unsubscribed_names) except Unauthorized as e: if 'Invalid ListID' in e: # this list might be deleted on campaign monitor, add it back list_id = cm_list.create(client_id, group.name, "", False, "") # update the list_map list_map.list_id = list_id list_map.save() # sync subscribers in this group print("Subscribing users to the C.M. list '%s'..." % group.name) members = group.members.all() for i, member in enumerate(members, 1): # Append custom fields from the profile try: profile = member.profile except Profile.DoesNotExist: profile = None custom_data = [] if profile: fields = ['city', 'state', 'zipcode', 'country', 'sex', 'member_number'] for field in fields: data = {} data['Key'] = field data['Value'] = getattr(profile, field) if not data['Value']: data['Clear'] = True custom_data.append(data) email = member.email name = member.get_full_name() subscriber_obj = Subscriber(auth, list_id, email) subscribe_to_list(subscriber_obj, list_id, name, email, custom_data) print('Done') print('Starting to sync campaigns with campaign monitor...') sync_campaigns() print("Done") print('Syncing templates...') sync_templates() print("Done")
def sync_cm_list(sender, instance=None, created=False, **kwargs): """Check if sync_newsletters. Do nothing if false. On Group Add: if group name does not exist on C. M, add a list to C. M. add an entry to listmap On Group Edit: if group exists on C. M., if list.name <> group.name, update list name else: add a list on C. M. add an entry to listmap """ cl = Client(auth, cm_client_id) lists = cl.lists() list_ids = [alist.ListID for alist in lists] list_names = [alist.Name for alist in lists] list_ids_d = dict(zip(list_names, list_ids)) list_d = dict(zip(list_ids, lists)) if created and instance.sync_newsletters: if instance.name in list_names: list_id = list_ids_d[instance.name] else: list_id = get_or_create_cm_list(cm_client_id, instance) if list_id: # add an entry to the listmap listmap_insert(instance, list_id) # custom fields setup cm_list = List(auth, list_id) setup_custom_fields(cm_list) elif instance.sync_newsletters: # update try: # find the entry in the listmap list_map = ListMap.objects.get(group=instance) list_id = list_map.list_id except ListMap.DoesNotExist: if instance.name in list_names: list_id = list_ids_d[instance.name] else: # hasn't be created on C. M. yet. create one list_id = get_or_create_cm_list(cm_client_id, instance) if list_id: listmap_insert(instance, list_id) if list_id and list_id in list_ids: alist = list_d[list_id] cm_list = List(auth, list_id) # setup custom fields setup_custom_fields(cm_list) # if the list title doesn't match with the group name, update the list title if instance.name != alist.Name: try: # trap the error for now # TODO: update only if the list title does not exist # within a client. cm_list.update(instance.name, "", False, "") except: pass
def handle(self, *args, **options): from tendenci.apps.user_groups.models import Group from tendenci.apps.profiles.models import Profile from tendenci.apps.subscribers.models import GroupSubscription as GS, SubscriberData as SD from tendenci.apps.subscribers.utils import get_subscriber_name_email from tendenci.addons.campaign_monitor.models import ListMap, Campaign, Template, setup_custom_fields from tendenci.addons.campaign_monitor.utils import sync_campaigns, sync_templates from createsend import CreateSend, Client, List, Subscriber, BadRequest, Unauthorized verbosity = 1 if "verbosity" in options: verbosity = options["verbosity"] def subscribe_to_list(subscriber_obj, list_id, name, email, custom_data): # check if this user has already subscribed, if not, subscribe it try: subscriber = subscriber_obj.get(list_id, email) if str(subscriber.State).lower() == "active": print name, email, " - UPDATED" subscriber = subscriber_obj.update(email, name, custom_data, True) except BadRequest as br: print br try: email_address = subscriber_obj.add(list_id, email, name, custom_data, True) if verbosity >= 2: print "%s (%s)" % (name, email) except BadRequest as br: print name, email, " - NOT ADDED: %s" % br api_key = getattr(settings, "CAMPAIGNMONITOR_API_KEY", None) client_id = getattr(settings, "CAMPAIGNMONITOR_API_CLIENT_ID", None) # CreateSend.api_key = api_key auth = {"api_key": api_key} cl = Client(auth, client_id) lists = cl.lists() list_ids = [list.ListID for list in lists] list_names = [list.Name for list in lists] list_ids_d = dict(zip(list_names, list_ids)) groups = Group.objects.filter(status=1, status_detail="active", sync_newsletters=1) listmaps = ListMap.objects.filter(group__sync_newsletters=1) syncd_groups = [listmap.group for listmap in listmaps] cm_list = List(auth) print "Starting to sync groups with campaign monitor..." print for group in groups: if group not in syncd_groups: # get the list id or create a list if not exists # campaing monitor requires the list title if group.name in list_names: list_id = list_ids_d[group.name] else: # add group to the campaign monitor list_id = cm_list.create(client_id, group.name, "", False, "") print "Added group '%s' to the C.M. list." % group.name print # insert to the listmap list_map = ListMap(group=group, list_id=list_id) list_map.save() else: list_map = ListMap.objects.filter(group=group)[0] list_id = list_map.list_id # if a previous added list is deleted on campaign monitor, add it back # TODO: we might need a setting to decide whether we want to add it back or not. a_list = List(list_id) try: list_stats = a_list.stats() # set up custom fields print "Setting up custom fields..." setup_custom_fields(a_list) # num_unsubscribed = list_stats.TotalUnsubscribes # if num_unsubscribed > 0: # # a list of all unsubscribed # unsubscribed_obj = a_list.unsubscribed('2011-5-1') # unsubscribed_emails = [res.EmailAddress for res in unsubscribed_obj.Results] # unsubscribed_names = [res.Name for res in unsubscribed_obj.Results] # unsubscribed_list = zip(unsubscribed_emails, unsubscribed_names) except Unauthorized as e: if "Invalid ListID" in e: # this list might be deleted on campaign monitor, add it back list_id = cm_list.create(client_id, group.name, "", False, "") # update the list_map list_map.list_id = list_id list_map.save() # sync subscribers in this group print "Subscribing users to the C.M. list '%s'..." % group.name members = group.members.all() for i, member in enumerate(members, 1): # Append custom fields from the profile try: profile = member.profile except Profile.DoesNotExist: profile = None custom_data = [] if profile: fields = ["city", "state", "zipcode", "country", "sex", "member_number"] for field in fields: data = {} data["Key"] = field data["Value"] = getattr(profile, field) if not data["Value"]: data["Clear"] = True custom_data.append(data) email = member.email name = member.get_full_name() subscriber_obj = Subscriber(auth, list_id, email) subscribe_to_list(subscriber_obj, list_id, name, email, custom_data) # sync subscribers in this group's subscription gss = GS.objects.filter(group=group) for gs in gss: if gs.subscriber: form_entry = gs.subscriber (name, email) = form_entry.get_name_email() else: gs_data = SD.objects.filter(subscription=gs) (name, email) = get_subscriber_name_email(gs_data) if email: subscriber_obj = Subscriber(auth, list_id, email) subscribe_to_list(subscriber_obj, list_id, name, email, []) print "Done" print "Starting to sync campaigns with campaign monitor..." sync_campaigns() print "Done" print "Syncing templates..." sync_templates() print "Done"
import config from createsend import Subscriber, Client, CreateSend, List account_auth = config.account_auth client_auth = config.client_auth a_test_client_auth = config.a_test_client_auth my_lists=Client(client_auth) lists_in_account=my_lists.lists() #for l in lists_in_account: # print(l) #my_client = Client(account_auth) #my_client.create("ZZZ API Test Client", "(GMT-05:00) Eastern Time (US & Canada)", "United States of America") ''' account_admin=CreateSend(account_auth) client_list=account_admin.clients() #print(client_list[0]) clients_and_ids = {} for client in client_list: clients_and_ids[client.Name] = client.ClientID #print(clients_and_ids[key]) #print(clients_and_ids) account_admin=CreateSend(account_auth)
# client_details = my_client.details() # # print(client_details.ApiKey) #print(a_test_client_auth) # #print("hello world") # # my_subscriber = Subscriber(a_test_client_auth) # # my_custom_fields = [{"Key": "Favorite Cuisine", "Value": "Pizza"},{'Key': 'Hobby', "Value": "Football"}] # # my_subscriber.add("5926ac888cffa2665144bae9127a89a5", "*****@*****.**", "Subscriber", my_custom_fields, True, "yes") # my_client = Client(account_auth, client_id="9cde80d058d8e4955a076d33b6ec2294") results = my_client.lists() list_names_ids = {} list_names = [] for r in results: list_names_ids[r.Name] = r.ListID list_names.append(r.Name) print(list_names) if 'Main List' not in list_names: print("its not there") else: print("It exists!") # # # print(type(dir(results[0]))) #