def _get_mailchimp_class() -> mailchimp3.MailChimp: """Returns the mailchimp api class. This is separated into a separate function to facilitate testing. NOTE: No other functionalities should be added to this function. Returns: Mailchimp. A mailchimp class instance with the API key and username initialized. """ # The return value ignore pragma is required for this. This is # because adding a Union[] type annotation to handle both None and # mailchimp3.MailChimp causes errors where the return value is called # (for eg: client.lists), since NoneType does not have an attribute lists. if not feconf.MAILCHIMP_API_KEY: logging.exception('Mailchimp API key is not available.') return None # type: ignore[return-value] if not feconf.MAILCHIMP_USERNAME: logging.exception('Mailchimp username is not set.') return None # The following is a class initialized in the library with the API key and # username and hence cannot be tested directly. The mailchimp functions are # tested with a mock class. return mailchimp3.MailChimp( # pragma: no cover mc_api=feconf.MAILCHIMP_API_KEY, mc_user=feconf.MAILCHIMP_USERNAME)
def _get_mailchimp_class(): """Returns the mailchimp api class. This is separated into a separate function to facilitate testing. NOTE: No other functionalities should be added to this function. Returns: Mailchimp. A mailchimp class instance with the API key and username initialized. Raises: Exception. Mailchimp API key is not available. Exception. Mailchimp username is not set. """ if not feconf.MAILCHIMP_API_KEY: raise Exception('Mailchimp API key is not available.') if not feconf.MAILCHIMP_USERNAME: raise Exception('Mailchimp username is not set.') # The following is a class initialized in the library with the API key and # username and hence cannot be tested directly. The mailchimp functions are # tested with a mock class. return mailchimp3.MailChimp( # pragma: no cover mc_api=feconf.MAILCHIMP_API_KEY, mc_user=feconf.MAILCHIMP_USERNAME)
def main(): with open('.mailchimp.json') as json_data_file: data = json.load(json_data_file) with open(data['csv_file']) as csvfile: connections = csv.DictReader(csvfile) for connection in connections: list_id = data['list_id'] client = mailchimp3.MailChimp(data['secret'], data['user']) try: client.lists.members.create( list_id, { 'email_address': connection['Email Address'], 'status': 'subscribed', 'merge_fields': { 'FNAME': connection['First Name'], 'LNAME': connection['Last Name'] } }) print(connection['First Name'], connection['Last Name'], connection['Email Address']) except Exception as e: pass os.remove(data['csv_file']) return
def get_data_in_batches( self, resource: str, batch_size: int = 10000 ) -> List[Dict[str, Any]]: client = mailchimp3.MailChimp(mc_user=self.conn.user, mc_api=self.conn.api_key) mc_resource = getattr(client, resource) keepgoing = True offset = 0 while keepgoing: data = mc_resource.all(count=batch_size, offset=offset)[resource] if data: yield data offset += batch_size else: keepgoing = False
import mailchimp3 import hashlib import json CREDENTIALS_FILE = "mailchimpCredentials.json" CREDENTIALS = json.load(open(CREDENTIALS_FILE)) API_KEY = CREDENTIALS["API_KEY"] LIST_ID = CREDENTIALS["LIST_ID"] DEFAULT_TAG = 'אין עיר' NUDNIK_TAG = "נודניק" api = mailchimp3.MailChimp(API_KEY) def getHashedEmail(email): return hashlib.md5(email.encode('utf-8').lower()).hexdigest() # only tag is tel aviv # add recurring tag if you want 'nudnik', it'll send a mail three days after def createOrUpdateMember(email, firstName, lastName, tags=[DEFAULT_TAG], status='subscribed'): api.lists.members.create_or_update(LIST_ID, subscriber_hash=getHashedEmail(email), data={'email_address': email, 'status': status, 'tags': tags, 'status_if_new': status, 'merge_fields': {'FNAME': firstName, 'LNAME': lastName, }});
def get_mailchimp_client(cls, api, user): """Returns a mailchimp3.MailChimp client""" return mailchimp3.MailChimp(mc_api=api, mc_user=user)
def get_client(self): return mailchimp3.MailChimp(mc_api=self.api_key)