예제 #1
0
def sanity_check(drafts):
    """
    look before you leap
    """
    print(drafts)
    print('Drafts found:', len(drafts))
    verify = user_input('Everything ready? [y/N]: ')
    return verify in ['Y', 'y']
예제 #2
0
def delete_contacts(cs=None):
    """
    don't think this works but you can do it from the UI
    """
    if not cs:
        dac = user_input('delete ALL contacts? [y/N]: ')
        if dac.lower == 'y':
            cs = get_contacts()
    for c in cs:
        gd_client.Delete(c.GetEditLink().href, force=True)
예제 #3
0
def delete_contacts(cs=None):
    """
    delete contacts using API
    """
    if not cs:
        dac = user_input('delete ALL contacts? [y/N]: ')
        if dac.lower == 'y':
            cs = get_contacts()
    for c in cs:
        people_service.deleteContact(resourceName=c["resourceName"]).execute()
예제 #4
0
def load_contacts():
    """
    loads contacts via api
    """
    user_input('about to create contacts. press enter to continue...')
    for contact in import_contacts():
        try:
            sleep(1)
            new_contact = gdata.contacts.data.ContactEntry()
            new_contact.name = gdata.data.Name(full_name=gdata.data.FullName(
                text=contact['first_name'] + ' ' + contact['last_name']))
            new_contact.email.append(
                gdata.data.Email(address=contact['email'], primary='true'))
            new_contact.organization = gdata.data.Organization(
                name=gdata.data.OrgName(contact['agency']), rel='work')
            new_contact.email[0].label = 'work'
            contact_entry = gd_client.CreateContact(new_contact)
            print('new contacts', contact_entry)
        except Exception as e:
            print('problem with', contact['email'], error_info(e))
            log.log_data('contact', contact)
예제 #5
0
def prep_agency_drafts(contacts_by_agency=None):
    """
    preps drafts for unsent agencies, with:
    - agency name appended to subject
    - agency slug appended to body
    - agency name attached to draft list for labeling

    a draft is a dict containing
    - the gmail draft object
    - the name of the agency (for labeling)
    i.e.:
    draft = [{'agency': agency_name,'draft': draft}]

    """
    if not contacts_by_agency:
        contacts_by_agency = unsent_agency_contacts()
    # first delete existing drafts
    delete_drafts()
    # then create new drafts
    print('agencies to be prepped:', list(contacts_by_agency.keys()))
    pd = user_input('prep drafts now? [y/N]: ')
    if pd.lower() == 'y':
        drafts = []
        date = datetime.now().date().strftime("%a, %b %d, %Y")
        for agency in contacts_by_agency:
            foia_text = load_foia_text(AGENCY=agency.title(), DATE=date)
            slug = agency_slug(agency)
            body = foia_text + '\r\n\r\n' + slug
            if subject_add_method == 'append':
                slug_subject = agency.title() + subject
            elif subject_add_method == 'prepend':
                slug_subject = subject + agency.title()
            else:
                raise ValueError(
                    "Config subject_add_method should be 'append' or 'prepend'"
                )
            contacts = ','.join(contacts_by_agency[agency])
            draft = {
                "agency": agency,
                "draft": compose_draft(body, slug_subject, contacts),
            }
            # TODO label the draft here not when you send so you can verify
            # thanks
            drafts.append(draft)
            print(draft)
        # TODO verify all agencies have a draft ... some get skipped i.e.
        # service errors
        return drafts
    else:
        print('skipping')
예제 #6
0
def load_contacts():
    """
    loads contacts via api
    """
    user_input('about to create contacts. press enter to continue...')
    for contact in import_contacts():
        try:
            sleep(1)
            body = {
                "names": [{
                    "givenName": contact["Name"]
                }],
                "emailAddresses": [{
                    "value": contact['E-mail 1 - Value']
                }],
                "organizations": [{
                    "current": True,
                    "name": contact['Organization 1 - Name']
                }]
            }
            print("new contacts", body)
        except Exception as e:
            print('problem with', contact['E-mail 1 - Value'], error_info(e))
            log.log_data('contact', contact)
예제 #7
0
def delete_drafts(draft_ids=None):
    """
    this can be handled via UI
    """
    if not draft_ids:
        # check for existence of drafts
        drafts = get_drafts()
        draft_ids = [x['id'] for x in drafts if type(drafts) == list]  # hack
    print('Existing drafts found:', len(draft_ids))
    if not len(draft_ids):
        return
    dd = user_input('existing drafts found ... delete? [y/N]: ')
    if dd.lower() == 'y':
        print(drafts)
        for draft_id in draft_ids:
            print('deleting', draft_id)
            service.users().drafts().delete(userId='me', id=draft_id).execute()
예제 #8
0
def distribute(send=False):
    """
    prepares drafts for each agency
    and sends if specified and tests pass

    due to potential for sending failure,
    this function will attempt to retry
    sending unsent drafts under the following conditions:
    - unsent agencies exist
    - previous attempts successfully sent 1 or more drafts
    *** retry logic is untested ***
    """
    drafts = prep_agency_drafts()
    if send and sanity_check(drafts):
        ready = user_input(
            'drafts created. inspect and type "send" to distribute: ')
        if ready.lower() == 'send':
            while True:
                original_draft_len = len(drafts)
                for draft in drafts:
                    print('Sending', draft)
                    sender(draft)
                # make sure everything sent, or else retry
                print("FOIAmail will try to re-create any unsent messages...")
                drafts = prep_agency_drafts()
                if drafts and len(drafts) < original_draft_len:
                    print(len(drafts), 'drafts remaining ... retrying')
                    continue
                elif not drafts:
                    print('distribution complete.')
                    break
                elif drafts and len(drafts) == original_draft_len:
                    # drafts aren't sending, it's a lost cause. avoid infinite
                    # loop
                    print('distribution incomplete:', len(drafts), 'unsent')
                    break
                else:
                    break
        else:
            print('aborting')