0
我已經分配了使用MailChimp API提取某些數據的任務。到目前爲止,該腳本已經毫無問題地提取了報告,廣告系列,列表和成員詳細信息。但是,文檔here中提到的工作流程自動化API不起作用。事實上,在DOC給出的這個非常CURL
例子是給404(資源未找到)錯誤:Mailchimp自動化API返回404錯誤(資源未找到)
curl --request GET \
--url 'https://usX.api.mailchimp.com/3.0/automations/b0a1c24f1a/emails' \
--user 'anystring:apikey' \
--include
我只能用我自己的數據中心數量取代了usX
並更新了apiKey
。在任何情況下,這裏是我的用戶都在抱怨我的整個automation.py
python腳本的自動化部分:
#!/usr/bin/python
import urllib
import urllib2
import base64
import json
import csv
import sys
import os
import codecs
__version__ = "1.4"
##
# Configures the MailChimpExpress
reload(sys)
sys.setdefaultencoding('utf8')
sys.stdout = codecs.getwriter('utf8')(sys.stdout)
workdir = "data/mailchimp/"
workflow_id = "b0a1c24f1a"
##
# Writes a list to csv
#
# @param tname name of the csv file to output without extension
# @param tlist the python list to write
def write_to_csv(tname, tlist):
myfile = open(workdir + tname + ".csv", 'wb')
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
for li in tlist:
wr.writerow(li)
myfile.close()
return tname + ".csv"
##
# Creates a file with specified name and text content.
#
# @param tname Name of the file with extension.
# @param ttext Content to write.
def createfile(tname, ttext):
myfile = open(workdir + tname , 'w')
myfile.write(ttext)
myfile.close()
##
# Pulls data from Mailchimp automation API.
def run():
key = sys.argv[1] # CAPTURE THE API KEY
dc = key.split("-")[-1] #this is the data-center where your request goes
username = "anystring" #could be literally anything as per mailchimp docs!
output = "" #var to hold raw json
data = "" #var to hold json dict
cnt = 0 #counter to keep track of fetched objects
##
# FETCH ALL REPORTS
#
campaigns = []
baseurl = "https://" + dc + ".api.mailchimp.com/3.0/"
psize, i = 1000, 0
##
# Lets fetch the automation data
#
print "Now pulling automation data (UNSTABLE AND UNTESTED FEATURE)"
autos = []
data ={}
#while(True): #No longer needed as there is no limit/offset here
turl = baseurl + "automations/" + workflow_id + "/emails" #lists/" + lid + "/members"
print "turl is " + turl
if False: #DEBUG:
tfp = open('sample_workflow_response.json')
output = tfp.read()
tfp.close()
else:
request = urllib2.Request(turl)
base64string = base64.encodestring('%s:%s' % (username, key)).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
output = urllib2.urlopen(request).read()
tdata = json.loads(output)
tcnt = len(tdata['emails'])
print tcnt, " emails pulled."
print ""
print 'ID', 'position', "create_time","start_time","archive_url"
for email in tdata["emails"]:
print email['id'], email['position'], email["create_time"], email["start_time"], email["archive_url"]
MailChimpExpress.createfile("lastauto.json", output)
###
print "All is well.."
if __name__ == "__main__":
run()
當我運行上面的代碼,它給了我下面的輸出:
Now pulling automation data (UNSTABLE AND UNTESTED FEATURE)
turl is https://us8.api.mailchimp.com/3.0/automations/b0a1c24f1a/emails
Traceback (most recent call last):
File "./automation.py", line 97, in <module>
run()
File "./automation.py", line 80, in run
output = urllib2.urlopen(request).read()
File "/usr/lib/python2.7/urllib2.py", line 127, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 410, in open
response = meth(req, response)
File "/usr/lib/python2.7/urllib2.py", line 523, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python2.7/urllib2.py", line 448, in error
return self._call_chain(*args)
File "/usr/lib/python2.7/urllib2.py", line 382, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 531, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 404: Not Found
我suggestin使用'requests'這是一個生命的救星。 – taesu
@taesu我知道它是一個很好的建議,我使用'requests'。但在某些情況下,我更喜歡使用香草python,而不是依賴外部庫。省去包裝和配置的麻煩。 –
祝你好運。 404只是一個404.你應該聯繫mail-chimp不要SO。 – taesu