diff options
Diffstat (limited to 'main.py')
-rw-r--r-- | main.py | 55 |
1 files changed, 15 insertions, 40 deletions
@@ -1,5 +1,5 @@ | |||
1 | import configargparse | 1 | import configargparse |
2 | import psycopg2 | 2 | import dbs |
3 | import os | 3 | import os |
4 | import sys | 4 | import sys |
5 | 5 | ||
@@ -63,15 +63,12 @@ def get_user_market(config_path, user_id, debug=False): | |||
63 | if debug: | 63 | if debug: |
64 | args.append("--debug") | 64 | args.append("--debug") |
65 | args = parse_args(args) | 65 | args = parse_args(args) |
66 | pg_config, redis_config = parse_config(args) | 66 | parse_config(args) |
67 | market_id, market_config, user_id = list(fetch_markets(pg_config, str(user_id)))[0] | 67 | market_id, market_config, user_id = list(fetch_markets(str(user_id)))[0] |
68 | return market.Market.from_config(market_config, args, | 68 | return market.Market.from_config(market_config, args, user_id=user_id) |
69 | pg_config=pg_config, market_id=market_id, | ||
70 | user_id=user_id) | ||
71 | 69 | ||
72 | def fetch_markets(pg_config, user): | 70 | def fetch_markets(user): |
73 | connection = psycopg2.connect(**pg_config) | 71 | cursor = dbs.psql.cursor() |
74 | cursor = connection.cursor() | ||
75 | 72 | ||
76 | if user is None: | 73 | if user is None: |
77 | cursor.execute("SELECT id,config,user_id FROM market_configs") | 74 | cursor.execute("SELECT id,config,user_id FROM market_configs") |
@@ -82,30 +79,11 @@ def fetch_markets(pg_config, user): | |||
82 | yield row | 79 | yield row |
83 | 80 | ||
84 | def parse_config(args): | 81 | def parse_config(args): |
85 | pg_config = { | 82 | if args.db_host is not None: |
86 | "host": args.db_host, | 83 | dbs.connect_psql(args) |
87 | "port": args.db_port, | 84 | |
88 | "user": args.db_user, | 85 | if args.redis_host is not None: |
89 | "password": args.db_password, | 86 | dbs.connect_redis(args) |
90 | "database": args.db_database, | ||
91 | } | ||
92 | del(args.db_host) | ||
93 | del(args.db_port) | ||
94 | del(args.db_user) | ||
95 | del(args.db_password) | ||
96 | del(args.db_database) | ||
97 | |||
98 | redis_config = { | ||
99 | "host": args.redis_host, | ||
100 | "port": args.redis_port, | ||
101 | "db": args.redis_database, | ||
102 | } | ||
103 | if redis_config["host"].startswith("/"): | ||
104 | redis_config["unix_socket_path"] = redis_config.pop("host") | ||
105 | del(redis_config["port"]) | ||
106 | del(args.redis_host) | ||
107 | del(args.redis_port) | ||
108 | del(args.redis_database) | ||
109 | 87 | ||
110 | report_path = args.report_path | 88 | report_path = args.report_path |
111 | 89 | ||
@@ -113,8 +91,6 @@ def parse_config(args): | |||
113 | os.path.exists(report_path): | 91 | os.path.exists(report_path): |
114 | os.makedirs(report_path) | 92 | os.makedirs(report_path) |
115 | 93 | ||
116 | return pg_config, redis_config | ||
117 | |||
118 | def parse_args(argv): | 94 | def parse_args(argv): |
119 | parser = configargparse.ArgumentParser( | 95 | parser = configargparse.ArgumentParser( |
120 | description="Run the trade bot.") | 96 | description="Run the trade bot.") |
@@ -176,11 +152,10 @@ def parse_args(argv): | |||
176 | parsed.action = ["sell_all"] | 152 | parsed.action = ["sell_all"] |
177 | return parsed | 153 | return parsed |
178 | 154 | ||
179 | def process(market_config, market_id, user_id, args, pg_config, redis_config): | 155 | def process(market_config, market_id, user_id, args): |
180 | try: | 156 | try: |
181 | market.Market\ | 157 | market.Market\ |
182 | .from_config(market_config, args, market_id=market_id, | 158 | .from_config(market_config, args, market_id=market_id, |
183 | pg_config=pg_config, redis_config=redis_config, | ||
184 | user_id=user_id)\ | 159 | user_id=user_id)\ |
185 | .process(args.action, before=args.before, after=args.after) | 160 | .process(args.action, before=args.before, after=args.after) |
186 | except Exception as e: | 161 | except Exception as e: |
@@ -189,7 +164,7 @@ def process(market_config, market_id, user_id, args, pg_config, redis_config): | |||
189 | def main(argv): | 164 | def main(argv): |
190 | args = parse_args(argv) | 165 | args = parse_args(argv) |
191 | 166 | ||
192 | pg_config, redis_config = parse_config(args) | 167 | parse_config(args) |
193 | 168 | ||
194 | market.Portfolio.report.set_verbose(not args.quiet) | 169 | market.Portfolio.report.set_verbose(not args.quiet) |
195 | 170 | ||
@@ -205,8 +180,8 @@ def main(argv): | |||
205 | else: | 180 | else: |
206 | process_ = process | 181 | process_ = process |
207 | 182 | ||
208 | for market_id, market_config, user_id in fetch_markets(pg_config, args.user): | 183 | for market_id, market_config, user_id in fetch_markets(args.user): |
209 | process_(market_config, market_id, user_id, args, pg_config, redis_config) | 184 | process_(market_config, market_id, user_id, args) |
210 | 185 | ||
211 | if args.parallel: | 186 | if args.parallel: |
212 | for thread in threads: | 187 | for thread in threads: |