Esempio n. 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)
Esempio n. 2
0
def driver():
    utils.log_debug('driver')
    global _driver
    install.setup_chromedriver()
    try:
        if is_alive(_driver):
            return _driver
        close_orphan_drivers()
        options = Options()
        assert os.path.exists(settings.chrome_data_dir)
        options.add_argument('user-data-dir={}'.format(
            settings.chrome_data_dir))
        options.add_argument('--disable-extensions')
        assert os.path.exists(settings.chromedriver_path)
        _driver = webdriver.Chrome(options=options,
                                   executable_path=settings.chromedriver_path)
    except BaseException:
        quit()
        if 'data directory is already in use' in traceback.format_exc():
            utils.log_exception_debug()
            utils.log_error('Must close Selenium-controlled Chrome.')
            if input('Try again? [Y/n]\n').lower() != 'n':
                utils.log_info('Trying again')
                return driver()
            sys.exit()
        else:
            utils.log_exception()
    assert _driver
    return _driver
Esempio n. 3
0
def is_alive(d):
    utils.log_debug('is_alive')
    try:
        d.execute(Command.STATUS)
        d.current_url
        return True
    except BaseException:
        utils.log_exception_debug()
        return False