def test_get_collection_raises(): with pytest.raises(NotImplementedError) as excinfo: mailchimp.get_collection( { '_links': [{ 'rel': 'self', 'href': 'https://api.example.com/things/' }], 'things': [1, 2, 3, 4, 5], 'total_items': 100, }, 'things') exc_message = str(excinfo.value).lower() assert 'pagination' in exc_message assert '5/100 things' in exc_message assert 'https://api.example.com/things/' in exc_message
def fetch_from_mailchimp(): metrics = {} api = MailChimpClient(MAILCHIMP_API_KEY) lists = get_collection(api.get('/lists/', count=1), 'lists') metrics['subscribers'] = lists[0]['stats']['member_count'] urls_clicked = [] reports = get_collection(api.get('/reports/', count=1000), 'reports') for report in reports: url = get_link(report, 'click-details') data = api.get(url, count=1000) urls_clicked.extend(get_collection(data, 'urls_clicked')) metrics['users_per_external_url'] = sum_clicks_per_external_url( urls_clicked, 'unique_clicks') metrics['pageviews_per_external_url'] = sum_clicks_per_external_url( urls_clicked, 'total_clicks') return metrics
def test_get_collection(): assert mailchimp.get_collection( { '_links': [{ 'rel': 'self', 'href': 'https://api.example.com/things/' }], 'things': [1, 2, 3, 4, 5], 'total_items': 5, }, 'things') == [1, 2, 3, 4, 5]
def main(): with db: JobNewsletterMention.drop_table() JobNewsletterMention.create_table() api = MailChimpClient(MAILCHIMP_API_KEY) campaigns = get_collection(api.get('/campaigns/'), 'campaigns') for campaign in campaigns: if not campaign.get('send_time'): continue data = api.get(get_link(campaign, 'content'), count=1000) for url in find_urls(data['html']): try: job = Job.get_by_url(url) except (ValueError, Job.DoesNotExist): pass else: JobNewsletterMention.create( job=job, sent_at=arrow.get(campaign['send_time']).date(), link=campaign['long_archive_url'])