# Path: mailgun/api.py
# def call_mailgun(method, api_sub_url, data, params=None):
# def send_email(to_email, from_email, subject, text_body, html_body=None, tags=None, campaign_id=None):
# def send_mass_email(to_emails, from_email, subject, text_body, html_body=None, tags=None, campaign_id=None, recipient_variables=None):
# def create_campaign(campaign_id, campaign_name):
# def create_list(address, name=None, description=None, access_level=None):
# def update_list(address, name=None, description=None, access_level=None):
# def add_list_member(list_address, member_address):
# def remove_list_member(list_address, member_address):
# def get_list(list_address):
# def get_list_members(list_address):
# def get_unsubscribes(address):
# def delete_all_unsubscribes(address):
# def get_list_stats(list_address):
# def get_logs(limit=100, skip=0):
# def fetch_all_logs():
# def get_events(limit, **kwargs):
# def get_all_events(**kwargs):
# def get_campaign_events(campaign_id, event, recipient=None, limit=None, page=None, count=None):
# def _get_campaign_action(campaign_id, action, group_by, limit=None, page=None, count=None):
# def get_campaign_opens(campaign_id, group_by, limit=None, page=None, count=None):
# def get_campaign_clicks(campaign_id, group_by, limit=None, page=None, count=None):
# 
# Path: mailgun/utils.py
# def parse_timestamp(timestamp_text):
#      return datetime.datetime.strptime(timestamp_text, '%a, %d %b %Y %H:%M:%S GMT')
# 
# Path: signup/models.py
# def create_signup( email, questions ):
# def update_signup( email, questions ):
# def create_or_update_signup( email, questions ):
# def delete_signup( email, sequence ):
# def _signup2json( signup_db ):
# def get_signup( email, sequence ):
# def get_all_user_signups( email ):
# def get_signup_by_invite_code( invite_code ):
# def get_signups( sequence ):
# def get_signups_for_archiving( sequence ):
# def get_new_signups( ):
# def handle_new_signups( ):
# def add_user_to_global_list( email, sequence ):
# 
# Path: groups/models.py
# def group_uri2id( group_uri ):
# def group_id2uri( group_id ):
# def create_group( address, description, sequence ):
# def _group2json(group_db):
# def get_group( group_uri ):
# def get_groups( sequence=None ):
# def get_member_groups( member_email ):
# def add_group_member( group_uri, member_email ):
# def remove_group_member( group_uri, member_email ):
# def sync_group_with_mailgun( group_uri ):
# 
# Path: data/utils.py
# def write_to_csv(data, filename):
#     """ data should be a list of dicts. All dicts should have the same keys! """
#     if len(data) == 0:
#         return
#     with open(filename, 'w') as f:
#         writer = unicodecsv.writer(f)
#         writer.writerow(data[0].keys())
#         for elem in data:
#             writer.writerow(elem.values())
# 
# Path: data/emails.py
# 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))


from mailgun import api as mailgun_api
from mailgun.utils import parse_timestamp
from signup import models as signup_api
from groups import models as groups_api
from data.utils import write_to_csv
from data.emails import export_emails
from django.conf import settings
from datetime import datetime
import sqlite3
import unicodecsv
        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 dump_old_data():

    sequences = [('1', 'bqtde'), ('2', 'br67o'), ('3','brmcg')]
    user_keys = ['email', 'created_at', 'group_id', 'group_size']
    event_keys = ['event', 'timestamp', 'tags', 'link']
    for sequence, campaign_id in sequences:
        writer = unicodecsv.writer(open('sequence_{0}_all.csv'.format(sequence), 'w'))
        users = old_sequence_users(sequence)
        for user in users:
            user['group_size'] = 0
            if user['group_id']:
                user['group_size'] = len([u for u in users if u['group_id'] == user['group_id']])
        sequence_data = []
        for i, user in enumerate(users):
            print('getting data for user {0} of {1}: {2}'.format(i,len(users),user['email']))

# Path: mailgun/api.py
# def call_mailgun(method, api_sub_url, data, params=None):
# def send_email(to_email, from_email, subject, text_body, html_body=None, tags=None, campaign_id=None):
# def send_mass_email(to_emails, from_email, subject, text_body, html_body=None, tags=None, campaign_id=None, recipient_variables=None):
# def create_campaign(campaign_id, campaign_name):
# def create_list(address, name=None, description=None, access_level=None):
# def update_list(address, name=None, description=None, access_level=None):
# def add_list_member(list_address, member_address):
# def remove_list_member(list_address, member_address):
# def get_list(list_address):
# def get_list_members(list_address):
# def get_unsubscribes(address):
# def delete_all_unsubscribes(address):
# def get_list_stats(list_address):
# def get_logs(limit=100, skip=0):
# def fetch_all_logs():
# def get_events(limit, **kwargs):
# def get_all_events(**kwargs):
# def get_campaign_events(campaign_id, event, recipient=None, limit=None, page=None, count=None):
# def _get_campaign_action(campaign_id, action, group_by, limit=None, page=None, count=None):
# def get_campaign_opens(campaign_id, group_by, limit=None, page=None, count=None):
# def get_campaign_clicks(campaign_id, group_by, limit=None, page=None, count=None):
# 
# Path: mailgun/utils.py
# def parse_timestamp(timestamp_text):
#      return datetime.datetime.strptime(timestamp_text, '%a, %d %b %Y %H:%M:%S GMT')
# 
# Path: signup/models.py
# def create_signup( email, questions ):
# def update_signup( email, questions ):
# def create_or_update_signup( email, questions ):
# def delete_signup( email, sequence ):
# def _signup2json( signup_db ):
# def get_signup( email, sequence ):
# def get_all_user_signups( email ):
# def get_signup_by_invite_code( invite_code ):
# def get_signups( sequence ):
# def get_signups_for_archiving( sequence ):
# def get_new_signups( ):
# def handle_new_signups( ):
# def add_user_to_global_list( email, sequence ):
# 
# Path: groups/models.py
# def group_uri2id( group_uri ):
# def group_id2uri( group_id ):
# def create_group( address, description, sequence ):
# def _group2json(group_db):
# def get_group( group_uri ):
# def get_groups( sequence=None ):
# def get_member_groups( member_email ):
# def add_group_member( group_uri, member_email ):
# def remove_group_member( group_uri, member_email ):
# def sync_group_with_mailgun( group_uri ):
# 
# Path: data/utils.py
# def write_to_csv(data, filename):
#     """ data should be a list of dicts. All dicts should have the same keys! """
#     if len(data) == 0:
#         return
#     with open(filename, 'w') as f:
#         writer = unicodecsv.writer(f)
#         writer.writerow(data[0].keys())
#         for elem in data:
#             writer.writerow(elem.values())
# 
# Path: data/emails.py
# 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))


# Path: data/models.py
from mailgun import api as mailgun_api
from mailgun.utils import parse_timestamp
from signup import models as signup_api
from groups import models as groups_api
from data.utils import write_to_csv
from data.emails import export_emails
from django.conf import settings
from datetime import datetime
import sqlite3
import unicodecsv

        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 dump_old_data():

    sequences = [('1', 'bqtde'), ('2', 'br67o'), ('3','brmcg')]
    user_keys = ['email', 'created_at', 'group_id', 'group_size']
    event_keys = ['event', 'timestamp', 'tags', 'link']
    for sequence, campaign_id in sequences:
        writer = unicodecsv.writer(open('sequence_{0}_all.csv'.format(sequence), 'w'))
        users = old_sequence_users(sequence)
        for user in users:
            user['group_size'] = 0
            if user['group_id']:
                user['group_size'] = len([u for u in users if u['group_id'] == user['group_id']])
        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)