From f86ee14037646bedc3a3dee4a48f085308981757 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isma=C3=ABl=20Bouya?= Date: Sun, 25 Feb 2018 18:02:44 +0100 Subject: Refactor the store to be more conciliant with multiple markets --- main.py | 66 +++++++++-------------------------------------------------------- 1 file changed, 9 insertions(+), 57 deletions(-) (limited to 'main.py') diff --git a/main.py b/main.py index 41abe9e..e7cdcf0 100644 --- a/main.py +++ b/main.py @@ -1,64 +1,16 @@ -import os import sys -import configparser -import psycopg2 -import argparse -from datetime import datetime +import helper, market -import portfolio, market +args = helper.main_parse_args(sys.argv[1:]) -parser = argparse.ArgumentParser( - description="Run the trade bot") +pg_config, report_path = helper.main_parse_config(args.config) -parser.add_argument("-c", "--config", - default="config.ini", - required=False, - help="Config file to load (default: config.ini)") -parser.add_argument("--before", - default=False, action='store_const', const=True, - help="Run the steps before the cryptoportfolio update") -parser.add_argument("--after", - default=False, action='store_const', const=True, - help="Run the steps after the cryptoportfolio update") -parser.add_argument("--debug", - default=False, action='store_const', const=True, - help="Run in debug mode") - -args = parser.parse_args() - -if not os.path.exists(args.config): - print("no config file found, exiting") - sys.exit(1) - -config = configparser.ConfigParser() -config.read(args.config) - -pg_config = config["postgresql"] - -connection = psycopg2.connect(**pg_config) -cursor = connection.cursor() - -cursor.execute("SELECT config,user_id FROM market_configs") - -report_path = config["app"]["report_path"] -if not os.path.exists(report_path): - os.makedirs(report_path) - -for row in cursor: - market_config, user_id = row +for market_config, user_id in helper.main_fetch_markets(pg_config): try: - user_market = market.get_market(market_config) - if args.before: - portfolio.h.process_sell_all__1_all_sell(user_market, debug=args.debug) - if args.after: - portfolio.Portfolio.wait_for_recent() - portfolio.h.process_sell_all__2_all_buy(user_market, debug=args.debug) + market_config["apiKey"] = market_config.pop("key") + user_market = market.Market.from_config(market_config, debug=args.debug) + helper.main_process_market(user_market, before=args.before, after=args.after) except Exception as e: - print(e) - pass + print("{}: {}".format(e.__class__.__name__, e)) finally: - report_file = "{}/{}_{}.json".format(report_path, datetime.now().isoformat(), user_id) - with open(report_file, "w") as f: - f.write(portfolio.ReportStore.to_json()) - portfolio.h.reset_all() - + helper.main_store_report(report_path, user_id, user_market) -- cgit v1.2.3