本文整理汇总了Python中postmonkey.PostMonkey.lists方法的典型用法代码示例。如果您正苦于以下问题:Python PostMonkey.lists方法的具体用法?Python PostMonkey.lists怎么用?Python PostMonkey.lists使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类postmonkey.PostMonkey
的用法示例。
在下文中一共展示了PostMonkey.lists方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: snapshot_mailchimp
# 需要导入模块: from postmonkey import PostMonkey [as 别名]
# 或者: from postmonkey.PostMonkey import lists [as 别名]
def snapshot_mailchimp(verbose=False):
api_key = os.environ.get('MAILCHIMP_API_KEY')
assert api_key, 'No MAILCHIMP_API_KEY defined in environment.'
pm = PostMonkey(api_key, timeout=10)
ping_string = pm.ping()
expected = u'Everything\'s Chimpy!'
assert ping_string==expected, 'Bad handshake, got "%s", expected "%s"' % (ping_string,expected)
if verbose:
print 'handshake ok'
lists = pm.lists()
if not 'data' in lists:
print 'Got bad lists object from server.'
pprint(lists)
raise ValueError('Bad lists object from server')
# Snapshot creation code...
today = datetime.now().date()
for l in lists['data']:
try:
if verbose: print 'Scraping %s...' % l['name']
latest = Session.query(model.SnapshotOfMailchimp)\
.filter(model.SnapshotOfMailchimp.name==l['name'])\
.order_by(model.SnapshotOfMailchimp.timestamp.desc())\
.first()
if latest and latest.timestamp>=today:
if verbose: print ' -> most recent snapshots have already been processed.'
continue
snapshot = model.SnapshotOfMailchimp(\
name = l['name'],\
members = l['stats']['member_count'],
timestamp = today)
if verbose: print ' -> ',snapshot.toJson()
Session.add(snapshot)
Session.commit()
except Exception, e:
pprint({'list':l,'exception':str(e)})
示例2: lists
# 需要导入模块: from postmonkey import PostMonkey [as 别名]
# 或者: from postmonkey.PostMonkey import lists [as 别名]
def lists():
pm = PostMonkey(os.environ.get('MAILCHIMP_API_KEY',''))
lists = pm.lists()
for list in lists['data']:
print list['id'], list['name']
return file('templates/200.json').read(), 200
示例3: MailchimpLocator
# 需要导入模块: from postmonkey import PostMonkey [as 别名]
# 或者: from postmonkey.PostMonkey import lists [as 别名]
class MailchimpLocator(object):
"""Utility for MailChimp API calls.
"""
implements(IMailchimpLocator)
def connect(self):
registry = getUtility(IRegistry)
self.settings = registry.forInterface(IMailchimpSettings)
self.mailchimp = PostMonkey(self.settings.api_key)
def lists(self):
"""Return all available MailChimp lists.
http://apidocs.mailchimp.com/api/rtfm/lists.func.php
"""
#print("MAILCHIMP LOCATOR: lists")
self.connect()
try:
# lists returns a dict with 'total' and 'data'. we just need data
return self.mailchimp.lists()['data']
except MailChimpException:
return []
except PostRequestError:
return []
except:
raise
def default_list_id(self):
self.connect()
if self.settings.default_list:
return self.settings.default_list
lists = self.lists()
if len(lists) > 0:
return lists[0]['id']
def groups(self, list_id=None):
"""Return all available MailChimp interest groups.
@id: the list id to connect to. e.g. u'a1346945ab'. Not the web_id!
http://apidocs.mailchimp.com/api/rtfm/listinterestgroupings.func.php
"""
if not list_id:
return
#print("MAILCHIMP LOCATOR: groups")
self.connect()
try:
# mailchimp returns a list of groups for a single mailinglist.
# We always choose the first and return just the groups part.
return self.mailchimp.listInterestGroupings(id=list_id)[0]
except MailChimpException, error:
if error.code == 211:
# "This list does not have interest groups enabled"
# http://apidocs.mailchimp.com/api/1.3/exceptions.field.php#210-list-_-basic-actions
return
elif error.code == 200:
# "Invalid MailChimp List ID"
# http://apidocs.mailchimp.com/api/1.3/exceptions.field.php#200-list-related-errors
return
raise
示例4: review
# 需要导入模块: from postmonkey import PostMonkey [as 别名]
# 或者: from postmonkey.PostMonkey import lists [as 别名]
def review():
app.logger.debug(request.form)
# get form data (WE SHOULD VALIDATE)
message = request.form.get('message')
subject = request.form.get('subject')
from_email = request.form.get('from_email')
from_name = request.form.get('from_name')
app.logger.debug(from_email)
app.logger.debug(from_name)
pm = PostMonkey(app.config['PM_API_KEY'])
list_info = pm.lists(filters={'list_id':app.config['PM_LIST_ID']})
list_name = list_info['data'][0]['name']
list_count = list_info['data'][0]['stats']['member_count']
return render_template('review_form.html',
page_title="Review E-mail",
from_name=from_name,
from_email=from_email,
list_name=list_name,
list_count=list_count,
message=message,
subject=subject)
示例5: handleApply
# 需要导入模块: from postmonkey import PostMonkey [as 别名]
# 或者: from postmonkey.PostMonkey import lists [as 别名]
def handleApply(self, action):
data, errors = self.extractData()
if 'email' in data:
# Fetch MailChimp settings
registry = getUtility(IRegistry)
mailchimp_settings = registry.forInterface(IMailchimpSettings)
if len(mailchimp_settings.api_key) == 0:
return
mailchimp = PostMonkey(mailchimp_settings.api_key)
# Fetch MailChimp lists
# XXX, Todo: For now we just fetch the first list.
try:
lists = mailchimp.lists()['data']
list_id = lists[0]['id']
except MailChimpException, error:
raise WidgetActionExecutionError(
Invalid(_(u"Could not fetch list from mailchimp.com: %s" %
error)))
# Subscribe to MailChimp list
try:
mailchimp.listSubscribe(
id=list_id,
email_address=data['email'])
except MailChimpException, error:
raise WidgetActionExecutionError(
'email',
Invalid(_(
u"Could not subscribe to newsletter: %s" % error)))
示例6: available_lists
# 需要导入模块: from postmonkey import PostMonkey [as 别名]
# 或者: from postmonkey.PostMonkey import lists [as 别名]
def available_lists(self):
registry = getUtility(IRegistry)
mailchimp_settings = registry.forInterface(IMailchimpSettings)
mailchimp = PostMonkey(mailchimp_settings.api_key)
try:
return mailchimp.lists()
except MailChimpException, error:
raise WidgetActionExecutionError(
Invalid(u"Could not fetch available lists from MailChimp. " +
"Please check your MailChimp API key: %s" % error))
示例7: available_lists
# 需要导入模块: from postmonkey import PostMonkey [as 别名]
# 或者: from postmonkey.PostMonkey import lists [as 别名]
def available_lists(context):
registry = getUtility(IRegistry)
mailchimp_settings = registry.forInterface(IMailchimpSettings)
if len(mailchimp_settings.api_key) == 0:
return SimpleVocabulary([])
mailchimp = PostMonkey(mailchimp_settings.api_key)
try:
lists = mailchimp.lists()["data"]
except MailChimpException:
pass
except:
return SimpleVocabulary([])
return SimpleVocabulary([SimpleTerm(value=li["id"], title=li["name"]) for li in lists])
示例8: MailchimpLocator
# 需要导入模块: from postmonkey import PostMonkey [as 别名]
# 或者: from postmonkey.PostMonkey import lists [as 别名]
class MailchimpLocator(object):
"""Utility for MailChimp API calls.
"""
implements(IMailchimpLocator)
key_account = "collective.mailchimp.cache.account"
key_groups = "collective.mailchimp.cache.groups"
key_lists = "collective.mailchimp.cache.lists"
def __init__(self):
self.mailchimp = None
self.registry = None
self.settings = None
def initialize(self):
if self.registry is None:
self.registry = getUtility(IRegistry)
if self.settings is None:
self.settings = self.registry.forInterface(IMailchimpSettings)
def connect(self):
if self.mailchimp is not None:
return
self.initialize()
self.mailchimp = PostMonkey(self.settings.api_key)
def lists(self):
self.initialize()
cache = self.registry.get(self.key_lists, _marker)
if cache and cache is not _marker:
return cache
return self._lists()
def _lists(self):
self.connect()
try:
# lists returns a dict with 'total' and 'data'. we just need data
return self.mailchimp.lists()['data']
except MailChimpException:
return []
except PostRequestError:
return []
except:
raise
def default_list_id(self):
self.connect()
if self.settings.default_list:
return self.settings.default_list
lists = self.lists()
if len(lists) > 0:
return lists[0]['id']
def groups(self, list_id=None):
if not list_id:
return
self.initialize()
cache = self.registry.get(self.key_groups, _marker)
if cache and cache is not _marker:
groups = cache.get(list_id, _marker)
if groups and groups is not _marker:
return groups
return self._groups(list_id)
def _groups(self, list_id=None):
if not list_id:
return
self.connect()
try:
# mailchimp returns a list of groups for a single mailinglist.
# We always choose the first and return just the groups part.
return self.mailchimp.listInterestGroupings(id=list_id)[0]
except MailChimpException, error:
if error.code == 211:
# "This list does not have interest groups enabled"
# http://apidocs.mailchimp.com/api/1.3/exceptions.field.php#210-list-_-basic-actions
return
elif error.code == 200:
# "Invalid MailChimp List ID"
# http://apidocs.mailchimp.com/api/1.3/exceptions.field.php#200-list-related-errors
return
raise
示例9: event
# 需要导入模块: from postmonkey import PostMonkey [as 别名]
# 或者: from postmonkey.PostMonkey import lists [as 别名]
userEvents = eb_client.user_list_events()
#print pm.ping() # returns u"Everything's Chimpy!"
idlist = [evnt['event']['id'] for evnt in userEvents['events']]
print "Found %s event(s)" % len(idlist)
print idlist
all_emails = []
print "Getting attendee emails..."
for thisid in idlist:
try:
peeps = eb_client.event_list_attendees({'id':thisid})
except EnvironmentError as e:
print e
these_emails = [peep['attendee']['email'] for peep in peeps['attendees']]
print "%s: [%s]" % (thisid, these_emails)
all_emails.extend(these_emails)
print "Found %s email(s)" % len(all_emails)
print "Updating Mailchimp list..."
mc_list_id = pm.lists()['data'][0]['id']
# the Mailchimp API wants an array of structs
email_structs = [{'EMAIL':eml} for eml in all_emails]
results = pm.listBatchSubscribe(id=mc_list_id, batch=email_structs, update_existing=True, double_optin=False)
print results
print "*** DONE ***"