def export_emails(prefix=''): messages = get_emails() timestamp = datetime.now().date().isoformat() mb = mailbox.mbox('{0}_emails_{1}.mbox'.format(prefix, timestamp)) for msg in messages: mb.add(msg) mb.close() csv_data = process_messages(messages) write_to_csv(csv_data, '{0}_emails_{1}.csv'.format(prefix, timestamp))
def dump_data(prefix=''): sequences = [(4, 'sequence_4_campaign'), (5, 'sequence_5_campaign')] #sequences = [(1, 'sequence_1_campaign')] user_keys = ['email', 'date_created', 'group_id', 'group_size'] event_keys = ['event', 'timestamp', 'tags', 'link'] for sequence, campaign_id in sequences: users = signup_api.get_signups_for_archiving(sequence) for user in users: user['group_size'] = 0 user['group_id'] = None user_groups = groups_api.get_member_groups(user['email']) if len(user_groups) == 1: user['group_id'] = user_groups[0]['address'] user['group_size'] = len(user_groups[0]['members']) user.update(user['questions']) del user['questions'] # open file for writing data timestamp = datetime.now().date().isoformat() filename = '_'.join( [prefix, 'sequence', str(sequence), 'users', timestamp]).strip('_') + '.csv' write_to_csv(users, filename) filename = '_'.join( [prefix, 'sequence', str(sequence), 'events', timestamp]).strip('_') + '.csv' writer = unicodecsv.writer(open(filename, 'w')) writer.writerow(user_keys + event_keys) sequence_data = [] for i, user in enumerate(users): print('getting data for user {0} of {1}: {2}'.format( i, len(users), user['email'])) get_stats = lambda p: mailgun_api.get_campaign_events( campaign_id, ['opened', 'clicked'], recipient=user['email'], page=p) user_stats = _fetch_all(get_stats) for event in user_stats: if 'link' not in event: event['link'] = '' event['tags'] = str(event['tags']) row = [user[key] for key in user_keys] row += [event[key] for key in event_keys] writer.writerow(row) export_emails('_'.join([prefix, 'sequence', str(sequence)]).strip('_'))
def dump_data(prefix=''): sequences = [(4, 'sequence_4_campaign'), (5, 'sequence_5_campaign')] #sequences = [(1, 'sequence_1_campaign')] user_keys = ['email', 'date_created', 'group_id', 'group_size'] event_keys = ['event', 'timestamp', 'tags', 'link'] for sequence, campaign_id in sequences: users = signup_api.get_signups_for_archiving(sequence) for user in users: user['group_size'] = 0 user['group_id'] = None user_groups = groups_api.get_member_groups(user['email']) if len(user_groups) == 1: user['group_id'] = user_groups[0]['address'] user['group_size'] = len(user_groups[0]['members']) user.update(user['questions']) del user['questions'] # open file for writing data timestamp = datetime.now().date().isoformat() filename = '_'.join([prefix, 'sequence', str(sequence), 'users', timestamp]).strip('_') + '.csv' write_to_csv(users, filename) filename = '_'.join([prefix, 'sequence', str(sequence), 'events', timestamp]).strip('_') + '.csv' writer = unicodecsv.writer(open(filename, 'w')) writer.writerow(user_keys + event_keys) sequence_data = [] for i, user in enumerate(users): print('getting data for user {0} of {1}: {2}'.format(i,len(users),user['email'])) get_stats = lambda p: mailgun_api.get_campaign_events(campaign_id, ['opened', 'clicked'], recipient=user['email'], page=p) user_stats = _fetch_all(get_stats) for event in user_stats: if 'link' not in event: event['link'] = '' event['tags'] = str(event['tags']) row = [ user[key] for key in user_keys ] row += [ event[key] for key in event_keys ] writer.writerow(row) export_emails('_'.join([prefix, 'sequence', str(sequence)]).strip('_'))
def get_old_data_aggregated(): # get campaign data for mooc sequence 1,2 & 3 sequences = [('1', 'bqtde'), ('2', 'br67o'), ('3','brmcg')] for sequence, campaign_id in sequences: print(sequence) print('getting opens') get_opens = lambda p: mailgun_api.get_campaign_opens(campaign_id, ['recipient', 'day'], page=p) opens = _fetch_all(get_opens) write_to_csv(opens, 'sequence_{0}_opens.csv'.format(sequence)) print('getting clicks') get_clicks = lambda p: mailgun_api.get_campaign_clicks(campaign_id, ['recipient', 'day'], page=p) clicks = _fetch_all(get_clicks) write_to_csv(clicks, 'sequence_{0}_clicks.csv'.format(sequence)) print('getting links') get_links = lambda p: mailgun_api.get_campaign_clicks(campaign_id, ['link', 'day'], page=p) links = _fetch_all(get_links) write_to_csv(links, 'sequence_{0}_links.csv'.format(sequence))
def get_old_data_aggregated(): # get campaign data for mooc sequence 1,2 & 3 sequences = [('1', 'bqtde'), ('2', 'br67o'), ('3', 'brmcg')] for sequence, campaign_id in sequences: print(sequence) print('getting opens') get_opens = lambda p: mailgun_api.get_campaign_opens( campaign_id, ['recipient', 'day'], page=p) opens = _fetch_all(get_opens) write_to_csv(opens, 'sequence_{0}_opens.csv'.format(sequence)) print('getting clicks') get_clicks = lambda p: mailgun_api.get_campaign_clicks( campaign_id, ['recipient', 'day'], page=p) clicks = _fetch_all(get_clicks) write_to_csv(clicks, 'sequence_{0}_clicks.csv'.format(sequence)) print('getting links') get_links = lambda p: mailgun_api.get_campaign_clicks( campaign_id, ['link', 'day'], page=p) links = _fetch_all(get_links) write_to_csv(links, 'sequence_{0}_links.csv'.format(sequence))