Пример #1
0
def load(data_type):
    utils.log_debug('load', data_type)
    assert data_type in data_parsers
    target_path = csv_paths[data_type]
    try:
        if not os.path.exists(target_path) or stale(target_path):
            d = gui.driver()
            url = 'https://smile.amazon.com/gp/b2b/reports'
            if url not in d.current_url:
                d.get(url)

            d.find_element_by_id('report-use-today').click()
            enter_start_date()
            d.find_element_by_id('report-type').click()
            d.find_element_by_id('report-type').send_keys(data_type)
            d.find_element_by_id('report-confirm').click()
            path = wait_for_download()
            utils.log_debug(path, target_path)
            os.rename(path, target_path)

    except BaseException:
        utils.log_exception_debug()
        if 'AttributeError' in traceback.format_exc():  # TODO: only retry on missing csv
            utils.log_info('real error', traceback.format_exc())
            sys.exit()
        if input('One more try? [Y/n]').lower() != 'n':
            load(data_type)

    list_of_dicts = read(target_path)
    utils.log_info('Found %s %s' % (len(list_of_dicts), data_type))
    return data_parsers[data_type](list_of_dicts)
Пример #2
0
def enter_start_date():
    today = datetime.datetime.now()
    start_date = today - utils.one_day * settings.max_amazon_eligible_days
    month, day, year = start_date.strftime('%B %d %Y').split()
    d = gui.driver()
    d.find_element_by_id('report-month-start').click()
    d.find_element_by_id('report-month-start').send_keys(month)
    d.find_element_by_id('report-day-start').click()
    d.find_element_by_id('report-day-start').send_keys(day)
    d.find_element_by_id('report-year-start').click()
    d.find_element_by_id('report-year-start').send_keys(year)
Пример #3
0
def load_gui():
    utils.log_debug('load_gui')
    url = 'https://app.youneedabudget.com/%s/accounts' % afy.settings.budget_id
    d = gui.driver()
    if url in d.current_url:
        return
    d.get(url)
    if not gui.get('user-logged-in', require=False):
        selection = input('Must be logged in. Try again? [Y/n]')
        if selection.lower() != 'n':
            load_gui()
Пример #4
0
def setup_ynab_budget_id():
    utils.log_info('Checking for selected budget')
    if settings.get('budget_id'):
        return
    utils.log_info('Selecting budget')
    url = 'https://app.youneedabudget.com/'
    driver = gui.driver()
    driver.get(url)
    utils.log_info('Log in if needed')
    while not re.search('youneedabudget.com/([^/]+)/', driver.current_url):
        time.sleep(.5)
    budget_selection_prompt = 'Press Enter when you have loaded the budget you want to use.'
    input(budget_selection_prompt)
    utils.log_debug(budget_selection_prompt)
    while not re.search('youneedabudget.com/([^/]+)/', driver.current_url):
        time.sleep(.5)
    settings.set(
        'budget_id',
        re.search('youneedabudget.com/([^/]+)/',
                  driver.current_url).groups()[0])
    utils.log_debug('settings.budget_id', settings.budget_id)
    assert settings.budget_id
    gui.quit()
Пример #5
0
def setup_ynab_auth():
    utils.log_info('Checking for YNAB authentication')
    # TODO: make this use oauth instead of api tokens
    if settings.get('api_token'):
        return
    utils.log_info('Installing YNAB authentication')
    api_token_url = 'https://app.youneedabudget.com/settings/developer'
    d = gui.driver()
    d.get(api_token_url)
    utils.log_info('Log in if needed')
    new_token_button = gui.get_by_text('button', 'New Token')
    gui.click(new_token_button)
    utils.log_info(
        'Enter your password in the YNAB Web App, then click "Generate"')
    while 'New Personal Access Token' not in d.page_source:
        time.sleep(.5)
    api_token = re.search(
        'New Personal Access Token: <strong>([^<]*)</strong>',
        d.page_source).groups()[0]
    settings.set('api_token', api_token)
    utils.log_debug('settings.api_token', settings.api_token)
    assert settings.api_token
    gui.quit()