# Configure web server

web_server_enabled = Config.getboolean('BOT', 'startWebServer')
if web_server_enabled:  # Run web server
    import modules.WebServer as WebServer
    WebServer.initialize_web_server(Config)

try:
    while True:
        try:
            Data.update_conversion_rates(output_currency, json_output_enabled)
            Lending.transfer_balances()
            Lending.cancel_all()
            Lending.lend_all()
            log.refreshStatus(
                Data.stringify_total_lended(*Data.get_total_lended()),
                Data.get_max_duration(end_date, "status"))
            log.persistStatus()
            sys.stdout.flush()
            time.sleep(Lending.get_sleep_time())
        except KeyboardInterrupt:
            # allow existing the main bot loop
            raise
        except Exception as ex:
            log.log_error(ex.message)
            log.persistStatus()
            if 'Invalid API key' in ex.message:
                print "!!! Troubleshooting !!!"
                print "Are your API keys correct? No quotation. Just plain keys."
                exit(1)
            elif 'Nonce must be greater' in ex.message:
    WebServer.initialize_web_server(Config)

# if config includes autorenew - start by clearing the current loans
if auto_renew == 1:
    set_auto_renew(0)

try:
    while True:
        try:
            Data.update_conversion_rates(output_currency, json_output_enabled)
            Lending.transfer_balances()
            Lending.cancel_all()
            Lending.lend_all()
            log.refreshStatus(
                Data.stringify_total_lended(*Data.get_total_lended()), Data.get_max_duration(end_date, "status")
            )
            log.persistStatus()
            sys.stdout.flush()
            time.sleep(Lending.get_sleep_time())
        except Exception as ex:
            log.log("ERROR: " + str(ex))
            log.persistStatus()
            print timestamp()
            print traceback.format_exc()
            if "Invalid API key" in str(ex):
                print "!!! Troubleshooting !!!"
                print "Are your API keys correct? No quotation. Just plain keys."
                exit(1)
            if "Nonce must be greater" in str(ex):
                print "!!! Troubleshooting !!!"
print 'Welcome to Poloniex Lending Bot'
# Configure web server

web_server_enabled = Config.get('BOT', 'startWebServer')
if web_server_enabled:  # Run web server
    import modules.WebServer as WebServer
    WebServer.initialize_web_server(Config)


while True:
    try:
        Data.update_conversion_rates(output_currency, json_output_enabled)
        Lending.transfer_balances()
        Lending.cancel_all()
        Lending.lend_all()
        log.refreshStatus(Data.stringify_total_lended(*Data.get_total_lended()), Data.get_max_duration(
            end_date, "status"))
        log.persistStatus()
        sys.stdout.flush()
        time.sleep(Lending.get_sleep_time())
    except KeyboardInterrupt:
        if web_server_enabled:
            WebServer.stop_web_server()
        log.log('bye')
        print 'bye'
        os._exit(0)  # Ad-hoc solution in place of 'exit(0)' TODO: Find out why non-daemon thread(s) are hanging on exit
    except Exception as ex:
        log.log_error(str(ex))
        log.persistStatus()
        if 'Invalid API key' in str(ex):
            print "!!! Troubleshooting !!!"
print 'Welcome to Poloniex Lending Bot'
# Configure web server

web_server_enabled = Config.getboolean('BOT', 'startWebServer')
if web_server_enabled:  # Run web server
    import modules.WebServer as WebServer
    WebServer.initialize_web_server(Config)

try:
    while True:
        try:
            Data.update_conversion_rates(output_currency, json_output_enabled)
            Lending.transfer_balances()
            Lending.cancel_all()
            Lending.lend_all()
            log.refreshStatus(Data.stringify_total_lended(*Data.get_total_lended()), Data.get_max_duration(
                end_date, "status"))
            log.persistStatus()
            sys.stdout.flush()
            time.sleep(Lending.get_sleep_time())
        except KeyboardInterrupt:
            # allow existing the main bot loop
            raise
        except Exception as ex:
            log.log_error(ex.message)
            log.persistStatus()
            if 'Invalid API key' in ex.message:
                print "!!! Troubleshooting !!!"
                print "Are your API keys correct? No quotation. Just plain keys."
                exit(1)
            elif 'Nonce must be greater' in ex.message:
print 'Welcome to Poloniex Lending Bot'
# Configure web server

web_server_enabled = Config.getboolean('BOT', 'startWebServer')
if web_server_enabled:  # Run web server
    import modules.WebServer as WebServer
    WebServer.initialize_web_server(Config)

try:
    while True:
        try:
            Data.update_conversion_rates(output_currency, json_output_enabled)
            Lending.transfer_balances()
            Lending.cancel_all()
            Lending.lend_all()
            log.refreshStatus(Data.stringify_total_lended(*Data.get_total_lended()),
                              Data.get_max_duration(end_date, "status"))
            log.persistStatus()
            sys.stdout.flush()
            time.sleep(Lending.get_sleep_time())
        except KeyboardInterrupt:
            # allow existing the main bot loop
            raise
        except Exception as ex:
            log.log_error(ex.message)
            log.persistStatus()
            if 'Invalid API key' in ex.message:
                print "!!! Troubleshooting !!!"
                print "Are your API keys correct? No quotation. Just plain keys."
                exit(1)
            elif 'Nonce must be greater' in ex.message: