-
Notifications
You must be signed in to change notification settings - Fork 2
/
order_checker.py
67 lines (54 loc) · 2.6 KB
/
order_checker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import asyncio
from logging import getLogger
from aiotg import Bot
import db
import settings
from exchanges import exchange_apis
from exchanges.base import state_text
from exchanges.exceptions import BaseExchangeException
class OrderChecker:
bot = Bot(settings.BOT_TOKEN)
async def check(self):
uids = await db.get_uids()
for uid in uids:
for exchange_api in exchange_apis:
exchange_id, exchange_name = exchange_api.api_id, exchange_api.name
api_key, secret_key = await db.get_keys(uid, exchange_id)
if not api_key or not secret_key:
continue
api = exchange_api(api_key, secret_key)
db_orders = await db.get_order_ids(exchange_id, uid)
try:
api_orders = await api.order_history()
except BaseExchangeException as e:
getLogger().error(f'Error while parsing exchange {exchange_name!r} of user id {uid}, skipping...')
getLogger().exception(e)
continue
new_orders = api_orders - db_orders
if not new_orders:
getLogger().info(f'There is no new orders of user id {uid} at exchange {exchange_name!r} '
f'with id {exchange_id}.')
continue
await db.add_orders((uid, exchange_id, order_id) for order_id in new_orders)
for order_id in new_orders:
try:
order = await api.order_info(order_id)
except BaseExchangeException as e:
getLogger().error(
f'Error while fetching order {order_id!r} of user id {uid} '
f'at exchange {exchange_name!r}, skipping...'
)
getLogger().exception(e)
continue
state = state_text[order.state]
getLogger().info(f'Order {order_id} of user {uid} at exchange {exchange_name!r} '
f'with id {exchange_id} is {state}.')
await self.send_message(uid, api.format_order(order))
async def periodic(self, interval=None):
while True:
getLogger().info('sleeping')
await asyncio.sleep(interval or settings.CHECK_INTERVAL)
await self.check()
async def send_message(self, uid, order_info):
user_chat = self.bot.private(uid)
await user_chat.send_text(order_info, parse_mode='Markdown')