aboutsummaryrefslogtreecommitdiff
path: root/main.py
diff options
context:
space:
mode:
Diffstat (limited to 'main.py')
-rw-r--r--main.py30
1 files changed, 20 insertions, 10 deletions
diff --git a/main.py b/main.py
index 856d449..4462192 100644
--- a/main.py
+++ b/main.py
@@ -62,17 +62,20 @@ def make_order(market, value, currency, action="acquire",
62 62
63def get_user_market(config_path, user_id, debug=False): 63def get_user_market(config_path, user_id, debug=False):
64 pg_config, report_path = parse_config(config_path) 64 pg_config, report_path = parse_config(config_path)
65 market_config = list(fetch_markets(pg_config, str(user_id)))[0][0] 65 market_id, market_config, user_id = list(fetch_markets(pg_config, str(user_id)))[0]
66 return market.Market.from_config(market_config, debug=debug) 66 args = type('Args', (object,), { "debug": debug, "quiet": False })()
67 return market.Market.from_config(market_config, args,
68 pg_config=pg_config, market_id=market_id,
69 user_id=user_id, report_path=report_path)
67 70
68def fetch_markets(pg_config, user): 71def fetch_markets(pg_config, user):
69 connection = psycopg2.connect(**pg_config) 72 connection = psycopg2.connect(**pg_config)
70 cursor = connection.cursor() 73 cursor = connection.cursor()
71 74
72 if user is None: 75 if user is None:
73 cursor.execute("SELECT config,user_id FROM market_configs") 76 cursor.execute("SELECT id,config,user_id FROM market_configs")
74 else: 77 else:
75 cursor.execute("SELECT config,user_id FROM market_configs WHERE user_id = %s", user) 78 cursor.execute("SELECT id,config,user_id FROM market_configs WHERE user_id = %s", user)
76 79
77 for row in cursor: 80 for row in cursor:
78 yield row 81 yield row
@@ -109,6 +112,9 @@ def parse_args(argv):
109 parser.add_argument("--after", 112 parser.add_argument("--after",
110 default=False, action='store_const', const=True, 113 default=False, action='store_const', const=True,
111 help="Run the steps after the cryptoportfolio update") 114 help="Run the steps after the cryptoportfolio update")
115 parser.add_argument("--quiet",
116 default=False, action='store_const', const=True,
117 help="Don't print messages")
112 parser.add_argument("--debug", 118 parser.add_argument("--debug",
113 default=False, action='store_const', const=True, 119 default=False, action='store_const', const=True,
114 help="Run in debug mode") 120 help="Run in debug mode")
@@ -128,10 +134,12 @@ def parse_args(argv):
128 134
129 return args 135 return args
130 136
131def process(market_config, user_id, report_path, args): 137def process(market_config, market_id, user_id, args, report_path, pg_config):
132 try: 138 try:
133 market.Market\ 139 market.Market\
134 .from_config(market_config, debug=args.debug, user_id=user_id, report_path=report_path)\ 140 .from_config(market_config, args,
141 pg_config=pg_config, market_id=market_id,
142 user_id=user_id, report_path=report_path)\
135 .process(args.action, before=args.before, after=args.after) 143 .process(args.action, before=args.before, after=args.after)
136 except Exception as e: 144 except Exception as e:
137 print("{}: {}".format(e.__class__.__name__, e)) 145 print("{}: {}".format(e.__class__.__name__, e))
@@ -145,11 +153,13 @@ def main(argv):
145 import threading 153 import threading
146 market.Portfolio.start_worker() 154 market.Portfolio.start_worker()
147 155
148 for market_config, user_id in fetch_markets(pg_config, args.user): 156 def process_(*args):
149 threading.Thread(target=process, args=[market_config, user_id, report_path, args]).start() 157 threading.Thread(target=process, args=args).start()
150 else: 158 else:
151 for market_config, user_id in fetch_markets(pg_config, args.user): 159 process_ = process
152 process(market_config, user_id, report_path, args) 160
161 for market_id, market_config, user_id in fetch_markets(pg_config, args.user):
162 process_(market_config, market_id, user_id, args, report_path, pg_config)
153 163
154if __name__ == '__main__': # pragma: no cover 164if __name__ == '__main__': # pragma: no cover
155 main(sys.argv[1:]) 165 main(sys.argv[1:])