From a4c600674ebd13ebd34b879f93a025af3b9ea6d2 Mon Sep 17 00:00:00 2001 From: Rob Denton Date: Wed, 10 Aug 2016 14:12:52 -0700 Subject: [PATCH 01/26] Add CID, posts * Add ability to keep campaign ID a secret * Add POST examples for sending test email and sending campaign TODO: * Add ability to replicate campaigns --- .gitignore | 1 + README.md | 2 +- python/README.md | 2 ++ python/config.py | 12 ++++++++++++ python/requirements.txt | 2 ++ python/send_email.py | 31 +++++++++++++++++++++++++++++++ python/test_email.py | 29 +++++++++++++++++++++++++++++ 7 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 python/requirements.txt create mode 100644 python/send_email.py create mode 100644 python/test_email.py diff --git a/.gitignore b/.gitignore index b14f1e6..42c0e2d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ APIKEY +CID *.pyc diff --git a/README.md b/README.md index 2a74b4e..e4dec62 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ folder, and each of those folders will have a README.md file describing what the ## General Instructions -Each of the examples will expect to find an API key in a file called 'APIKEY' here in the root folder. +Each of the examples will expect to find an API key in a file called 'APIKEY' here in the root folder. Python POST examples will expect your to find a campaign ID in a file called 'CID' here in the root folder. ## Contributions diff --git a/python/README.md b/python/README.md index 3822abd..d961c9f 100644 --- a/python/README.md +++ b/python/README.md @@ -3,3 +3,5 @@ These examples have been tested with Python version 2.7.6 and requires the Requests and JSON libraries. Place your API Key in an 'APIKEY' file in the root of this repository. + +Place your campaign ID in a 'CID' file in the root of this repository. Campaign IDs should be ten characters long and be mixed case. Example: 'abcde12345' diff --git a/python/config.py b/python/config.py index bb41be3..831ac1c 100644 --- a/python/config.py +++ b/python/config.py @@ -21,3 +21,15 @@ def __init__(self): self.apikey = apikey self.shard = parts[1] self.api_root = "https://" + self.shard + ".api.mailchimp.com/3.0/" + +class GetCampaignID: + def __init__(self): + if os.path.isfile('../CID') == False: + print "Please enter your Campaign ID into a CID file in the root" + sys.exit() + + f = open('../CID', 'r+') + cid = f.read().strip() + f.close() + + self.cid = cid diff --git a/python/requirements.txt b/python/requirements.txt new file mode 100644 index 0000000..88ce209 --- /dev/null +++ b/python/requirements.txt @@ -0,0 +1,2 @@ +requests==2.11.0 +wsgiref==0.1.2 diff --git a/python/send_email.py b/python/send_email.py new file mode 100644 index 0000000..952b514 --- /dev/null +++ b/python/send_email.py @@ -0,0 +1,31 @@ +# +# !!! DANGER !!! +# !!! DANGER !!! +# !!! DANGER !!! +# +# This script will send an email to all recipients of campaign +# To test the email, use the test_email.py file + +import requests +import json +from config import MailChimpConfig, GetCampaignID + +config = MailChimpConfig() +campaign_id = GetCampaignID() + +email = "campaigns/{0}/actions/send".format(campaign_id) +endpoint = config.api_root + email + +response = requests.post(endpoint, auth=('apikey', config.apikey)) + +#print "\nURL: " + response.url + "\n\n" + +try: + response.raise_for_status() + print "\n\nSENT!!!\n\n" +except requests.exceptions.HTTPError as err: + print '\n\n\nError: %s' % err + +print "\nHeaders:" +for header in response.headers: + print '\t'.join(['',header.ljust(20), response.headers[header]]) diff --git a/python/test_email.py b/python/test_email.py new file mode 100644 index 0000000..e5367b1 --- /dev/null +++ b/python/test_email.py @@ -0,0 +1,29 @@ +# This example shows a basic POST request that sends a test email. + +import requests +import json +from config import MailChimpConfig, GetCampaignID + +config = MailChimpConfig() +campaign_id = GetCampaignID() + +test = "campaigns/{0}/actions/test".format(campaign_id) +endpoint = config.api_root + test + +payload = json.dumps({"test_emails":["robdentonrg@gmail.com"],"send_type":"html"}) + +print "\nPayload: " + payload + +response = requests.post(endpoint, auth=('apikey', config.apikey), data=payload) + +print "\nURL: " + response.url + "\n\n" + +try: + response.raise_for_status() + print "\n\nSENT!!!\n\n" +except requests.exceptions.HTTPError as err: + print '\n\n\nError: %s' % err + +print "\nHeaders:" +for header in response.headers: + print '\t'.join(['',header.ljust(20), response.headers[header]]) From fe7a00b629dd3c478d246a0d7bc593ea85bc25ff Mon Sep 17 00:00:00 2001 From: Rob Denton Date: Thu, 11 Aug 2016 11:25:48 -0700 Subject: [PATCH 02/26] fix script to get CID --- python/send_email.py | 2 +- python/test_email.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/python/send_email.py b/python/send_email.py index 952b514..38fcec7 100644 --- a/python/send_email.py +++ b/python/send_email.py @@ -13,7 +13,7 @@ config = MailChimpConfig() campaign_id = GetCampaignID() -email = "campaigns/{0}/actions/send".format(campaign_id) +email = "campaigns/{0}/actions/send".format(campaign_id.cid) endpoint = config.api_root + email response = requests.post(endpoint, auth=('apikey', config.apikey)) diff --git a/python/test_email.py b/python/test_email.py index e5367b1..08e4f79 100644 --- a/python/test_email.py +++ b/python/test_email.py @@ -7,7 +7,7 @@ config = MailChimpConfig() campaign_id = GetCampaignID() -test = "campaigns/{0}/actions/test".format(campaign_id) +test = "campaigns/{0}/actions/test".format(campaign_id.cid) endpoint = config.api_root + test payload = json.dumps({"test_emails":["robdentonrg@gmail.com"],"send_type":"html"}) From 913fb303d3d9f7a1e27a315638ac2a83c198be96 Mon Sep 17 00:00:00 2001 From: Rob Denton Date: Fri, 12 Aug 2016 13:38:48 -0700 Subject: [PATCH 03/26] Working test email send --- python/rg.py | 128 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 python/rg.py diff --git a/python/rg.py b/python/rg.py new file mode 100644 index 0000000..69bad2e --- /dev/null +++ b/python/rg.py @@ -0,0 +1,128 @@ +# +# !!! DANGER !!! +# !!! DANGER !!! +# !!! DANGER !!! +# +# This script will send an email to all recipients of campaign +# To test the email, use the test_email.py file + +import requests +import json +from config import MailChimpConfig + +config = MailChimpConfig() +# print campaign_id + +def createCampaign(key): + path = "campaigns" + endpoint = config.api_root + path + + meta = {} + + meta['recipients'] = { + 'segment_opts': { + 'conditions': [{ + 'field': 'EMAIL', + 'condition_type': 'EmailAddress', + 'value': 'rob.denton', + 'op': 'contains' + }], + 'match': 'any' + }, + 'list_id': '824c7efd1d' + } + + meta['settings'] = { + 'subject_line': 'RG Daily Digest', + 'from_name': 'The Register-Gard', + 'title': 'Rob API test 1', + 'inline_css': True, + 'fb_comments': False, + 'auto_footer': False, + 'athenticate': True, + 'to_name': '*|FNAME|* *|LNAME|*', + 'folder_id': '', + 'reply_to': 'promotions@registerguard.com', + 'auto_tweet': False, + } + + meta['type'] = 'regular' + + #print meta + + payload = json.dumps(meta) + + #print payload + + response = requests.post(endpoint, auth=('apikey', key), data=payload) + + #print response.json() + + try: + response.raise_for_status() + body = response.json() + id = body['id'] + return id + except requests.exceptions.HTTPError as err: + print '\n\n\nError: %s' % err + + + +def setContent(key, id): + content = "campaigns/{0}/content".format(id) + endpoint = config.api_root + content + + payload = json.dumps({"url": "http://registerguard.com/csp/cms/rg/pages/newsletters/news.csp"}) + + response = requests.put(endpoint, auth=('apikey', key), data=payload) + + try: + response.raise_for_status() + except requests.exceptions.HTTPError as err: + print '\n\n\nError: %s' % err + print response.json() + +def sendTest(key, id): + test = "campaigns/{0}/actions/test".format(id) + endpoint = config.api_root + test + + payload = json.dumps({"test_emails":["robdentonrg@gmail.com"],"send_type":"html"}) + + #print "\nPayload: " + payload + + response = requests.post(endpoint, auth=('apikey', key), data=payload) + #print response.json() + + #print "\nURL: " + response.url + "\n\n" + + try: + response.raise_for_status() + print "\n\nTEST SENT!!!\n\n" + except requests.exceptions.HTTPError as err: + print '\n\n\nError: %s' % err + +# Call method +campaign_id = createCampaign(config.apikey) +#campaign_id = "3b97b6caca" # For testing (use last created campaign ID) +print campaign_id +setContent(config.apikey, campaign_id) +sendTest(config.apikey, campaign_id) + +# email = "campaigns/{0}/actions/send".format(campaign_id.cid) +# endpoint = config.api_root + email +# +# response = requests.post(endpoint, auth=('apikey', config.apikey)) +# +# #print "\nURL: " + response.url + "\n\n" +# +# try: +# response.raise_for_status() +# print "\n\nSENT!!!\n\n" +# except requests.exceptions.HTTPError as err: +# print '\n\n\nError: %s' % err +# +# print "\nHeaders:" +# for header in response.headers: +# print '\t'.join(['',header.ljust(20), response.headers[header]]) + + From 25b98d1000e446d75b8e2ffdcf16e619b1d754e4 Mon Sep 17 00:00:00 2001 From: Rob Denton Date: Fri, 12 Aug 2016 13:46:10 -0700 Subject: [PATCH 04/26] Send campaign working --- python/rg.py | 43 +++++++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/python/rg.py b/python/rg.py index 69bad2e..cf3484a 100644 --- a/python/rg.py +++ b/python/rg.py @@ -100,29 +100,28 @@ def sendTest(key, id): print "\n\nTEST SENT!!!\n\n" except requests.exceptions.HTTPError as err: print '\n\n\nError: %s' % err + + +def sendEmail(key, id): + email = "campaigns/{0}/actions/send".format(id) + endpoint = config.api_root + email + + response = requests.post(endpoint, auth=('apikey', key)) + + #print "\nURL: " + response.url + "\n\n" + + try: + response.raise_for_status() + print "\n\nCAMPAIGN SENT!!!\n\n" + except requests.exceptions.HTTPError as err: + print '\n\n\nError: %s' % err + print response.json() + # Call method campaign_id = createCampaign(config.apikey) -#campaign_id = "3b97b6caca" # For testing (use last created campaign ID) -print campaign_id +#campaign_id = "ea9d94e671" # For testing (use last created campaign ID) +#print campaign_id setContent(config.apikey, campaign_id) -sendTest(config.apikey, campaign_id) - -# email = "campaigns/{0}/actions/send".format(campaign_id.cid) -# endpoint = config.api_root + email -# -# response = requests.post(endpoint, auth=('apikey', config.apikey)) -# -# #print "\nURL: " + response.url + "\n\n" -# -# try: -# response.raise_for_status() -# print "\n\nSENT!!!\n\n" -# except requests.exceptions.HTTPError as err: -# print '\n\n\nError: %s' % err -# -# print "\nHeaders:" -# for header in response.headers: -# print '\t'.join(['',header.ljust(20), response.headers[header]]) - - +#sendTest(config.apikey, campaign_id) +sendEmail(config.apikey, campaign_id) From 27bbfe76011d684ab6923fd6377193cd2c3999ff Mon Sep 17 00:00:00 2001 From: Rob Denton Date: Fri, 12 Aug 2016 14:46:41 -0700 Subject: [PATCH 05/26] Add time to title --- python/rg.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/python/rg.py b/python/rg.py index cf3484a..636b2ac 100644 --- a/python/rg.py +++ b/python/rg.py @@ -6,10 +6,13 @@ # This script will send an email to all recipients of campaign # To test the email, use the test_email.py file -import requests -import json +import requests # Need to install +import json, datetime from config import MailChimpConfig +today = datetime.date.today() +date = "{:%x}".format(today) + config = MailChimpConfig() # print campaign_id @@ -33,9 +36,9 @@ def createCampaign(key): } meta['settings'] = { - 'subject_line': 'RG Daily Digest', + 'subject_line': 'RG Daily Digest: *|DATE:l, F j, Y|*', 'from_name': 'The Register-Gard', - 'title': 'Rob API test 1', + 'title': 'Rob API Test: {0}'.format(date), 'inline_css': True, 'fb_comments': False, 'auto_footer': False, From 73a40b6b767c4616921141570733121d505fb1c8 Mon Sep 17 00:00:00 2001 From: Rob Denton Date: Fri, 12 Aug 2016 15:06:47 -0700 Subject: [PATCH 06/26] add path for cron --- python/rg.py | 1 + 1 file changed, 1 insertion(+) diff --git a/python/rg.py b/python/rg.py index 636b2ac..e0e107b 100644 --- a/python/rg.py +++ b/python/rg.py @@ -1,3 +1,4 @@ +#!/home/newsoper/Envs/mailchimp/bin/python2.7 # # !!! DANGER !!! # !!! DANGER !!! From 02ee8f8a97d52107ecd84c0feef8425726d0e74e Mon Sep 17 00:00:00 2001 From: Rob Denton - Mailchimp on Wave Date: Fri, 12 Aug 2016 15:32:08 -0700 Subject: [PATCH 07/26] update gitignore to ignore logs --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 42c0e2d..5c3a42a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ APIKEY CID *.pyc +logs From b951f6229c7b955f214e34ebc8f472dbc1d617aa Mon Sep 17 00:00:00 2001 From: Rob Denton Date: Fri, 12 Aug 2016 15:37:33 -0700 Subject: [PATCH 08/26] remove sent comment --- python/rg.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/rg.py b/python/rg.py index e0e107b..5fee756 100644 --- a/python/rg.py +++ b/python/rg.py @@ -1,4 +1,4 @@ -#!/home/newsoper/Envs/mailchimp/bin/python2.7 +#!/home/newsoper/Envs/mailchimpda/bin/python2.7 # # !!! DANGER !!! # !!! DANGER !!! @@ -116,7 +116,7 @@ def sendEmail(key, id): try: response.raise_for_status() - print "\n\nCAMPAIGN SENT!!!\n\n" + #print "\n\nCAMPAIGN SENT!!!\n\n" except requests.exceptions.HTTPError as err: print '\n\n\nError: %s' % err print response.json() From 0c3884245d7d99719f971b8049ce983d5273697c Mon Sep 17 00:00:00 2001 From: Rob Denton Date: Fri, 12 Aug 2016 15:53:58 -0700 Subject: [PATCH 09/26] add absolute path for cron --- python/config.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/python/config.py b/python/config.py index 831ac1c..1f3fb69 100644 --- a/python/config.py +++ b/python/config.py @@ -8,7 +8,8 @@ def __init__(self): print "Please enter your API Key into the APIKEY file as mentioned in README.md" sys.exit() - f = open('../APIKEY', 'r+') + #f = open('../APIKEY', 'r+') + f = open('/home/newsoper/Envs/mailchimp/APIv3-examples/APIKEY', 'r+') # For cron apikey = f.read().strip() f.close() From 5b3ab8bc360cc155473b43ca10ead13f6341d818 Mon Sep 17 00:00:00 2001 From: Rob Denton Date: Fri, 12 Aug 2016 16:00:08 -0700 Subject: [PATCH 10/26] fix paths again --- python/config.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/python/config.py b/python/config.py index 1f3fb69..a9c81e9 100644 --- a/python/config.py +++ b/python/config.py @@ -4,7 +4,8 @@ class MailChimpConfig: def __init__(self): - if os.path.isfile('../APIKEY') == False: + #if os.path.isfile('../APIKEY') == False: + if os.path.isfile('/home/newsoper/Envs/mailchimp/APIv3-examples/APIKEY') == False: print "Please enter your API Key into the APIKEY file as mentioned in README.md" sys.exit() From 381a08652a7e4ad6ccdc7eb8c293c0a3102893b7 Mon Sep 17 00:00:00 2001 From: Rob Denton Date: Fri, 12 Aug 2016 16:12:56 -0700 Subject: [PATCH 11/26] update readme --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index e4dec62..274faac 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,7 @@ +# RG directions + +If there are any issues ssh into wave as newsoper and check the logs in ~/Envs/mailchimp/APIv3-examples/python/logs/mailchimp.log. + # MailChimp API v3.0 Examples Whithin this repository, you'll find straight-forward examples of working with v3 the MailChimp API without any @@ -16,3 +20,5 @@ We welcome pull requests to these examples to fix bugs or demonstrate new behavi If there's a certain kind of problem you'd like to see an example of or a language you'd like to see supported, please open a GitHub issue. Note, we can't promise that we'll be able to address all requests. + + From 3e5af3df35509922c028d5937dfddd1cb68136e5 Mon Sep 17 00:00:00 2001 From: Rob Denton Date: Mon, 15 Aug 2016 12:25:40 -0700 Subject: [PATCH 12/26] add group functionality --- .gitignore | 2 ++ python/rg.py | 16 ++++++++++------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 5c3a42a..0cbfc47 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ APIKEY CID *.pyc logs +*.json +read-* \ No newline at end of file diff --git a/python/rg.py b/python/rg.py index 5fee756..f7616d3 100644 --- a/python/rg.py +++ b/python/rg.py @@ -26,10 +26,14 @@ def createCampaign(key): meta['recipients'] = { 'segment_opts': { 'conditions': [{ - 'field': 'EMAIL', - 'condition_type': 'EmailAddress', - 'value': 'rob.denton', - 'op': 'contains' + # 'field': 'EMAIL', + # 'condition_type': 'EmailAddress', + # 'value': 'rob.denton', + # 'op': 'contains' + 'field': 'interests-3fcd22fb3e', + 'condition_type': 'Interests', + 'value': ['a51e8d83ce'], + 'op': 'interestcontains' }], 'match': 'any' }, @@ -37,7 +41,7 @@ def createCampaign(key): } meta['settings'] = { - 'subject_line': 'RG Daily Digest: *|DATE:l, F j, Y|*', + 'subject_line': '[TESTING] RG Daily Digest: *|DATE:l, F j, Y|*', 'from_name': 'The Register-Gard', 'title': 'Rob API Test: {0}'.format(date), 'inline_css': True, @@ -128,4 +132,4 @@ def sendEmail(key, id): #print campaign_id setContent(config.apikey, campaign_id) #sendTest(config.apikey, campaign_id) -sendEmail(config.apikey, campaign_id) +#sendEmail(config.apikey, campaign_id) From 147ac004e5beccf888a796a7d0b5a05cc162571b Mon Sep 17 00:00:00 2001 From: Rob Denton Date: Mon, 15 Aug 2016 12:26:05 -0700 Subject: [PATCH 13/26] Whoops, turn sending back on --- python/rg.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/rg.py b/python/rg.py index f7616d3..96ac600 100644 --- a/python/rg.py +++ b/python/rg.py @@ -132,4 +132,4 @@ def sendEmail(key, id): #print campaign_id setContent(config.apikey, campaign_id) #sendTest(config.apikey, campaign_id) -#sendEmail(config.apikey, campaign_id) +sendEmail(config.apikey, campaign_id) From 4b2127f4858592b1dd92b2287d3de15aa81e9c26 Mon Sep 17 00:00:00 2001 From: Rob Denton Date: Mon, 15 Aug 2016 16:41:25 -0700 Subject: [PATCH 14/26] remove rg.py --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 0cbfc47..20f90ea 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ CID *.pyc logs *.json -read-* \ No newline at end of file +read-* +rg.py \ No newline at end of file From 9b99c69265655cd68c37b80124e3f0cf200f8e01 Mon Sep 17 00:00:00 2001 From: Rob Denton Date: Mon, 15 Aug 2016 16:42:56 -0700 Subject: [PATCH 15/26] remove CID from gitignore --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index 20f90ea..5dcd120 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ APIKEY -CID *.pyc logs *.json From ecbdf219b7cf019f2192e9da518089c5c994b56f Mon Sep 17 00:00:00 2001 From: Rob Denton Date: Mon, 15 Aug 2016 16:44:18 -0700 Subject: [PATCH 16/26] update readme --- README.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/README.md b/README.md index 274faac..b4303e8 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,3 @@ -# RG directions - -If there are any issues ssh into wave as newsoper and check the logs in ~/Envs/mailchimp/APIv3-examples/python/logs/mailchimp.log. - # MailChimp API v3.0 Examples Whithin this repository, you'll find straight-forward examples of working with v3 the MailChimp API without any From 4c669c75239694bd670c10a11f47cf54d7a0d96e Mon Sep 17 00:00:00 2001 From: Rob Denton Date: Mon, 15 Aug 2016 16:45:28 -0700 Subject: [PATCH 17/26] revert config back --- python/config.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/python/config.py b/python/config.py index a9c81e9..831ac1c 100644 --- a/python/config.py +++ b/python/config.py @@ -4,13 +4,11 @@ class MailChimpConfig: def __init__(self): - #if os.path.isfile('../APIKEY') == False: - if os.path.isfile('/home/newsoper/Envs/mailchimp/APIv3-examples/APIKEY') == False: + if os.path.isfile('../APIKEY') == False: print "Please enter your API Key into the APIKEY file as mentioned in README.md" sys.exit() - #f = open('../APIKEY', 'r+') - f = open('/home/newsoper/Envs/mailchimp/APIv3-examples/APIKEY', 'r+') # For cron + f = open('../APIKEY', 'r+') apikey = f.read().strip() f.close() From 10045f8b69cad43b19879769194dd1fc6b5cefca Mon Sep 17 00:00:00 2001 From: Rob Denton Date: Mon, 15 Aug 2016 16:49:02 -0700 Subject: [PATCH 18/26] actually remove rg.py --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 5dcd120..9b3ab25 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,4 @@ APIKEY logs *.json read-* -rg.py \ No newline at end of file +python/rg.py \ No newline at end of file From 24b2c658d74a6f7cab6730face9a3c60178b2d12 Mon Sep 17 00:00:00 2001 From: Rob Denton Date: Mon, 15 Aug 2016 16:51:15 -0700 Subject: [PATCH 19/26] remove email from test email --- .gitignore | 2 +- python/test_email.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 9b3ab25..dc9466f 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,4 @@ APIKEY logs *.json read-* -python/rg.py \ No newline at end of file +python/rg.py diff --git a/python/test_email.py b/python/test_email.py index 08e4f79..ddab18e 100644 --- a/python/test_email.py +++ b/python/test_email.py @@ -10,7 +10,7 @@ test = "campaigns/{0}/actions/test".format(campaign_id.cid) endpoint = config.api_root + test -payload = json.dumps({"test_emails":["robdentonrg@gmail.com"],"send_type":"html"}) +payload = json.dumps({"test_emails":["INSERT EMAIL HERE"],"send_type":"html"}) print "\nPayload: " + payload From 8c3fab3561f5965ad0f6fa02442b9addb1b09191 Mon Sep 17 00:00:00 2001 From: Rob Denton Date: Mon, 15 Aug 2016 16:53:24 -0700 Subject: [PATCH 20/26] remove rg --- python/rg.py | 135 --------------------------------------------------- 1 file changed, 135 deletions(-) delete mode 100644 python/rg.py diff --git a/python/rg.py b/python/rg.py deleted file mode 100644 index 96ac600..0000000 --- a/python/rg.py +++ /dev/null @@ -1,135 +0,0 @@ -#!/home/newsoper/Envs/mailchimpda/bin/python2.7 -# -# !!! DANGER !!! -# !!! DANGER !!! -# !!! DANGER !!! -# -# This script will send an email to all recipients of campaign -# To test the email, use the test_email.py file - -import requests # Need to install -import json, datetime -from config import MailChimpConfig - -today = datetime.date.today() -date = "{:%x}".format(today) - -config = MailChimpConfig() -# print campaign_id - -def createCampaign(key): - path = "campaigns" - endpoint = config.api_root + path - - meta = {} - - meta['recipients'] = { - 'segment_opts': { - 'conditions': [{ - # 'field': 'EMAIL', - # 'condition_type': 'EmailAddress', - # 'value': 'rob.denton', - # 'op': 'contains' - 'field': 'interests-3fcd22fb3e', - 'condition_type': 'Interests', - 'value': ['a51e8d83ce'], - 'op': 'interestcontains' - }], - 'match': 'any' - }, - 'list_id': '824c7efd1d' - } - - meta['settings'] = { - 'subject_line': '[TESTING] RG Daily Digest: *|DATE:l, F j, Y|*', - 'from_name': 'The Register-Gard', - 'title': 'Rob API Test: {0}'.format(date), - 'inline_css': True, - 'fb_comments': False, - 'auto_footer': False, - 'athenticate': True, - 'to_name': '*|FNAME|* *|LNAME|*', - 'folder_id': '', - 'reply_to': 'promotions@registerguard.com', - 'auto_tweet': False, - } - - meta['type'] = 'regular' - - #print meta - - payload = json.dumps(meta) - - #print payload - - response = requests.post(endpoint, auth=('apikey', key), data=payload) - - #print response.json() - - try: - response.raise_for_status() - body = response.json() - id = body['id'] - return id - except requests.exceptions.HTTPError as err: - print '\n\n\nError: %s' % err - - - -def setContent(key, id): - content = "campaigns/{0}/content".format(id) - endpoint = config.api_root + content - - payload = json.dumps({"url": "http://registerguard.com/csp/cms/rg/pages/newsletters/news.csp"}) - - response = requests.put(endpoint, auth=('apikey', key), data=payload) - - try: - response.raise_for_status() - except requests.exceptions.HTTPError as err: - print '\n\n\nError: %s' % err - print response.json() - -def sendTest(key, id): - test = "campaigns/{0}/actions/test".format(id) - endpoint = config.api_root + test - - payload = json.dumps({"test_emails":["robdentonrg@gmail.com"],"send_type":"html"}) - - #print "\nPayload: " + payload - - response = requests.post(endpoint, auth=('apikey', key), data=payload) - #print response.json() - - #print "\nURL: " + response.url + "\n\n" - - try: - response.raise_for_status() - print "\n\nTEST SENT!!!\n\n" - except requests.exceptions.HTTPError as err: - print '\n\n\nError: %s' % err - - -def sendEmail(key, id): - email = "campaigns/{0}/actions/send".format(id) - endpoint = config.api_root + email - - response = requests.post(endpoint, auth=('apikey', key)) - - #print "\nURL: " + response.url + "\n\n" - - try: - response.raise_for_status() - #print "\n\nCAMPAIGN SENT!!!\n\n" - except requests.exceptions.HTTPError as err: - print '\n\n\nError: %s' % err - print response.json() - - -# Call method -campaign_id = createCampaign(config.apikey) -#campaign_id = "ea9d94e671" # For testing (use last created campaign ID) -#print campaign_id -setContent(config.apikey, campaign_id) -#sendTest(config.apikey, campaign_id) -sendEmail(config.apikey, campaign_id) From 29a653ae161067ddfe65c712382b4a8e7a4d8a05 Mon Sep 17 00:00:00 2001 From: Rob Denton Date: Tue, 6 Sep 2016 13:35:43 -0700 Subject: [PATCH 21/26] Update for pull request --- .gitignore | 4 --- python/README.md | 4 +-- python/config.py | 12 --------- python/create_campaign.py | 57 +++++++++++++++++++++++++++++++++++++++ python/requirements.txt | 1 - python/send_email.py | 31 --------------------- python/test_email.py | 23 ++++++++-------- 7 files changed, 69 insertions(+), 63 deletions(-) create mode 100644 python/create_campaign.py delete mode 100644 python/send_email.py diff --git a/.gitignore b/.gitignore index dc9466f..b14f1e6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,2 @@ APIKEY *.pyc -logs -*.json -read-* -python/rg.py diff --git a/python/README.md b/python/README.md index d961c9f..092d58d 100644 --- a/python/README.md +++ b/python/README.md @@ -1,7 +1,5 @@ # MailChimp API v3.0 Examples for Python -These examples have been tested with Python version 2.7.6 and requires the Requests and JSON libraries. +These examples have been tested with Python version 2.7.6 and requires the Requests and JSON modules. Place your API Key in an 'APIKEY' file in the root of this repository. - -Place your campaign ID in a 'CID' file in the root of this repository. Campaign IDs should be ten characters long and be mixed case. Example: 'abcde12345' diff --git a/python/config.py b/python/config.py index 831ac1c..bb41be3 100644 --- a/python/config.py +++ b/python/config.py @@ -21,15 +21,3 @@ def __init__(self): self.apikey = apikey self.shard = parts[1] self.api_root = "https://" + self.shard + ".api.mailchimp.com/3.0/" - -class GetCampaignID: - def __init__(self): - if os.path.isfile('../CID') == False: - print "Please enter your Campaign ID into a CID file in the root" - sys.exit() - - f = open('../CID', 'r+') - cid = f.read().strip() - f.close() - - self.cid = cid diff --git a/python/create_campaign.py b/python/create_campaign.py new file mode 100644 index 0000000..d851b76 --- /dev/null +++ b/python/create_campaign.py @@ -0,0 +1,57 @@ +# This example shows a basic POST request to create a new campaign +# See below, there are several variables you need to update + +import requests # Need to install +import json +from config import MailChimpConfig + +config = MailChimpConfig() + +path = "campaigns" +endpoint = config.api_root + path + +# Create metadata for campaign +# See: http://developer.mailchimp.com/documentation/mailchimp/reference/campaigns/ +meta = {} + +meta['recipients'] = { + # Insert recipient list ID here + # See: Lists > Settings > List name and campaign defaults for ID + 'list_id': 'ABCDE12345' +} + +meta['settings'] = { + 'subject_line': 'New product announcement!', # Subject line + 'from_name': 'Your company', # From name + 'title': '1/1/16: New product', # Campaign title + 'inline_css': True, # Automatically inline CSS + 'fb_comments': False, # Facebook comments + 'auto_footer': False, # Auto MailChimp footer + 'to_name': '*|FNAME|* *|LNAME|*', # To name (See merge tag cheat sheet: http://kb.mailchimp.com/merge-tags/all-the-merge-tags-cheat-sheet) + 'folder_id': '', # Put campaign in folder + 'reply_to': 'test@test.com', # Reply-to email + 'auto_tweet': False, # Auto tweet newsletter +} + +meta['type'] = 'regular' # Campaign type + +#print meta + +# JSON-ify metadata +payload = json.dumps(meta) + +#print payload + +# Send post request +response = requests.post(endpoint, auth=('apikey', config.apikey), data=payload) + +#print response.json() + +try: + response.raise_for_status() + body = response.json() + id = body['id'] + # Print out new campaign ID to do something else with it (like set content) + print id +except requests.exceptions.HTTPError as err: + print '\n\n\nError: %s' % err \ No newline at end of file diff --git a/python/requirements.txt b/python/requirements.txt index 88ce209..4afac4f 100644 --- a/python/requirements.txt +++ b/python/requirements.txt @@ -1,2 +1 @@ requests==2.11.0 -wsgiref==0.1.2 diff --git a/python/send_email.py b/python/send_email.py deleted file mode 100644 index 38fcec7..0000000 --- a/python/send_email.py +++ /dev/null @@ -1,31 +0,0 @@ -# -# !!! DANGER !!! -# !!! DANGER !!! -# !!! DANGER !!! -# -# This script will send an email to all recipients of campaign -# To test the email, use the test_email.py file - -import requests -import json -from config import MailChimpConfig, GetCampaignID - -config = MailChimpConfig() -campaign_id = GetCampaignID() - -email = "campaigns/{0}/actions/send".format(campaign_id.cid) -endpoint = config.api_root + email - -response = requests.post(endpoint, auth=('apikey', config.apikey)) - -#print "\nURL: " + response.url + "\n\n" - -try: - response.raise_for_status() - print "\n\nSENT!!!\n\n" -except requests.exceptions.HTTPError as err: - print '\n\n\nError: %s' % err - -print "\nHeaders:" -for header in response.headers: - print '\t'.join(['',header.ljust(20), response.headers[header]]) diff --git a/python/test_email.py b/python/test_email.py index ddab18e..e29f08d 100644 --- a/python/test_email.py +++ b/python/test_email.py @@ -1,29 +1,28 @@ # This example shows a basic POST request that sends a test email. -import requests +import requests # Need to install import json -from config import MailChimpConfig, GetCampaignID +from config import MailChimpConfig config = MailChimpConfig() -campaign_id = GetCampaignID() -test = "campaigns/{0}/actions/test".format(campaign_id.cid) +# Set your variables here +campaign_id = "ABCD1234" # INSERT CAMPAIGN ID HERE +test_email = ["test@test.com"] # INSERT TEST EMAIL HERE + +test = "campaigns/{0}/actions/test".format(campaign_id) endpoint = config.api_root + test -payload = json.dumps({"test_emails":["INSERT EMAIL HERE"],"send_type":"html"}) +payload = json.dumps({"test_emails": test_email, "send_type": "html"}) -print "\nPayload: " + payload +#print "\nPayload: " + payload response = requests.post(endpoint, auth=('apikey', config.apikey), data=payload) -print "\nURL: " + response.url + "\n\n" +#print "\nURL: " + response.url + "\n\n" try: response.raise_for_status() - print "\n\nSENT!!!\n\n" + print "\n\n Test sent \n\n" except requests.exceptions.HTTPError as err: print '\n\n\nError: %s' % err - -print "\nHeaders:" -for header in response.headers: - print '\t'.join(['',header.ljust(20), response.headers[header]]) From a94a9be7d3750f8647d33daed88539d6c5650d4d Mon Sep 17 00:00:00 2001 From: Rob Denton Date: Tue, 6 Sep 2016 13:38:31 -0700 Subject: [PATCH 22/26] fix diffs for pull --- README.md | 4 +--- python/README.md | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index b4303e8..a568231 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ folder, and each of those folders will have a README.md file describing what the ## General Instructions -Each of the examples will expect to find an API key in a file called 'APIKEY' here in the root folder. Python POST examples will expect your to find a campaign ID in a file called 'CID' here in the root folder. +Each of the examples will expect to find an API key in a file called 'APIKEY' here in the root folder. ## Contributions @@ -16,5 +16,3 @@ We welcome pull requests to these examples to fix bugs or demonstrate new behavi If there's a certain kind of problem you'd like to see an example of or a language you'd like to see supported, please open a GitHub issue. Note, we can't promise that we'll be able to address all requests. - - diff --git a/python/README.md b/python/README.md index 092d58d..3822abd 100644 --- a/python/README.md +++ b/python/README.md @@ -1,5 +1,5 @@ # MailChimp API v3.0 Examples for Python -These examples have been tested with Python version 2.7.6 and requires the Requests and JSON modules. +These examples have been tested with Python version 2.7.6 and requires the Requests and JSON libraries. Place your API Key in an 'APIKEY' file in the root of this repository. From d0e44cdbd1789904aac9511dd96cf39e7afba5f5 Mon Sep 17 00:00:00 2001 From: Rob Denton Date: Tue, 6 Sep 2016 13:39:31 -0700 Subject: [PATCH 23/26] space --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a568231..2a74b4e 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ folder, and each of those folders will have a README.md file describing what the ## General Instructions -Each of the examples will expect to find an API key in a file called 'APIKEY' here in the root folder. +Each of the examples will expect to find an API key in a file called 'APIKEY' here in the root folder. ## Contributions From c9c1747ccf2e465537eefe2f39913f1e61ee9579 Mon Sep 17 00:00:00 2001 From: Rob Denton Date: Tue, 6 Sep 2016 13:48:48 -0700 Subject: [PATCH 24/26] Fix some quotes --- python/create_campaign.py | 4 ++-- python/test_email.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/python/create_campaign.py b/python/create_campaign.py index d851b76..6ad16cc 100644 --- a/python/create_campaign.py +++ b/python/create_campaign.py @@ -7,7 +7,7 @@ config = MailChimpConfig() -path = "campaigns" +path = 'campaigns' endpoint = config.api_root + path # Create metadata for campaign @@ -54,4 +54,4 @@ # Print out new campaign ID to do something else with it (like set content) print id except requests.exceptions.HTTPError as err: - print '\n\n\nError: %s' % err \ No newline at end of file + print "\n\n\nError: %s" % err \ No newline at end of file diff --git a/python/test_email.py b/python/test_email.py index e29f08d..1f7e354 100644 --- a/python/test_email.py +++ b/python/test_email.py @@ -7,13 +7,13 @@ config = MailChimpConfig() # Set your variables here -campaign_id = "ABCD1234" # INSERT CAMPAIGN ID HERE +campaign_id = 'ABCD1234' # INSERT CAMPAIGN ID HERE test_email = ["test@test.com"] # INSERT TEST EMAIL HERE test = "campaigns/{0}/actions/test".format(campaign_id) endpoint = config.api_root + test -payload = json.dumps({"test_emails": test_email, "send_type": "html"}) +payload = json.dumps({'test_emails': test_email, 'send_type': 'html'}) #print "\nPayload: " + payload From 3e90972a99139f881261cca95635b06d8eb36ca8 Mon Sep 17 00:00:00 2001 From: Rob Denton Date: Tue, 7 Mar 2017 16:57:48 -0800 Subject: [PATCH 25/26] add folders script --- python/folders.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 python/folders.py diff --git a/python/folders.py b/python/folders.py new file mode 100644 index 0000000..3e2f1cf --- /dev/null +++ b/python/folders.py @@ -0,0 +1,53 @@ +""" This example demonstrates showing some basic details about your folders +Below you can see examples of pagination as well as partial response +""" + +import requests +import json +try: + import urlparse +except ImportError: + from urllib import parse as urlparse +from config import MailChimpConfig + + +config = MailChimpConfig() + +endpoint = urlparse.urljoin(config.api_root, 'campaign-folders') +params = { + # With Partial Response, you choose which fields you want to see + 'fields': 'folders.id,folders.name,folders.stats.member_count', + # Pagination in API v3.0 is always done with count and offset + 'count': 10, 'offset': 0 +} + +total_folders = 0 + +while True: + response = requests.get(endpoint, auth=('apikey', config.apikey), + params=params, verify=False) + + try: + response.raise_for_status() + body = response.json() + except requests.exceptions.HTTPError as err: + print "Error: {} {}".format(str(response.status_code), err) + print json.dumps(response.json(), indent=4) + break + except ValueError: + print "Cannot decode json, got %s" % response.text + break + + if len(body['folders']) == 0: + break + + total_folders += len(body['folders']) + + for folder in body['folders']: + print u'%s: %s' % ( + folder['id'], + folder['name']) + + params['offset'] += params['count'] + +print "\n" + str(total_folders) + " folders found." From 20572f8eeb16d4f440aeb3473647e2e589fcdb8b Mon Sep 17 00:00:00 2001 From: Rob Denton Date: Thu, 2 Nov 2017 17:22:42 -0700 Subject: [PATCH 26/26] Move APIKEY to python folder --- python/config.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/python/config.py b/python/config.py index bb41be3..023ad31 100644 --- a/python/config.py +++ b/python/config.py @@ -4,20 +4,20 @@ class MailChimpConfig: def __init__(self): - if os.path.isfile('../APIKEY') == False: + if os.path.isfile('APIKEY') == False: print "Please enter your API Key into the APIKEY file as mentioned in README.md" sys.exit() - - f = open('../APIKEY', 'r+') + + f = open('APIKEY', 'r+') apikey = f.read().strip() f.close() - + parts = apikey.split('-') if len(parts) != 2: print "This doesn't look like an API Key: " + apikey print "The API Key should have both a key and a server name, separated by a dash, like this: abcdefg8abcdefg6abcdefg4-us1" sys.exit() - + self.apikey = apikey self.shard = parts[1] self.api_root = "https://" + self.shard + ".api.mailchimp.com/3.0/"