-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
244 lines (186 loc) · 7.83 KB
/
main.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
from aiogram import Dispatcher, Bot, enums, filters, types, exceptions
from io import BytesIO
from asyncio import sleep, create_task
from pytz import utc as utc_timezone
from datetime import datetime
from coingecko import CoinGecko
from basic_data import TEXTS
from config import config
import utils
from typing import Optional, Tuple, Dict, AsyncGenerator, List
bot: Bot = Bot(
token = config.bot_token,
parse_mode = enums.ParseMode.HTML
)
dispatcher: Dispatcher = Dispatcher()
coingecko: CoinGecko = CoinGecko(
vs_currency = config.coingecko.vs_currency,
chart_days = config.coingecko.chart_days,
proxies = config.proxies
)
CAN_STARTUP: bool = False
DEFAULT_MESSAGE: Optional[str] = None
logger: utils.Logger = utils.get_logger(
name = config.logger_name,
bot_token = config.bot_token,
chat_ids = [
config.logs_chat_id
],
level = config.logger_level
)
def parse_price(current_prices: Dict[str, float], price_change_percentages: Dict[str, float], currency_name: str, numbers_after: int) -> Tuple[str, str]:
return (
format(
current_prices[currency_name],
".{numbers_after}f".format(
numbers_after = numbers_after
)
),
utils.prettify_number(
string = format(
price_change_percentages[currency_name],
".1f"
)
)
)
class CustomBufferedInputFile(types.InputFile):
def __init__(self, buffered_file: BytesIO, filename: str, chunk_size: int = types.input_file.DEFAULT_CHUNK_SIZE):
super().__init__(filename=filename, chunk_size=chunk_size)
self.buffered_file: BytesIO = buffered_file
async def read(self, bot: Bot) -> AsyncGenerator[bytes, None]:
while chunk := self.buffered_file.read(self.chunk_size):
yield chunk
async def edit_default_message() -> None:
try:
await bot.edit_message_text(
chat_id = config.main_channel_id,
message_id = config.main_channel_message_id,
text = DEFAULT_MESSAGE,
disable_web_page_preview = True
)
except exceptions.TelegramAPIError:
pass
async def coingecko_prices_checker() -> None:
global DEFAULT_MESSAGE, CAN_STARTUP
vs_currency_upper: str = config.coingecko.vs_currency.upper()
started_at: float
started_at = utils.get_float_timestamp()
skipped_time: float = started_at % config.coingecko.prices_checker_delay
if skipped_time > 0:
await sleep(config.coingecko.prices_checker_delay - skipped_time)
while True:
started_at = utils.get_float_timestamp()
updated_prices: Dict[str, float] = {}
try:
for coin_name, (channel_id, _) in config.channels.items():
coin_code: str = config.coins[coin_name]
market_data: dict = await coingecko.get_market_data(
coin_code = coin_code
)
parse_prices: Dict[str, Tuple[str, int]] = config.parse_prices.copy()
if coin_name in parse_prices:
del parse_prices[coin_name]
current_prices: Dict[str, float] = market_data["current_price"]
price_change_percentages: Dict[str, float] = market_data["price_change_percentage_24h_in_currency"]
prices_and_percentages: List[str] = []
for currency_name_upper, (currency_name_lower, numbers_after) in parse_prices.items():
currency_price: str
currency_persentage: str
currency_price, currency_persentage = parse_price(
current_prices = current_prices,
price_change_percentages = price_change_percentages,
currency_name = currency_name_lower,
numbers_after = numbers_after
)
prices_and_percentages.append(
TEXTS.price.format(
currency_name = currency_name_upper,
currency_price = currency_price,
currency_persentage = currency_persentage
)
)
try:
await bot.send_photo(
chat_id = channel_id,
photo = CustomBufferedInputFile(
buffered_file = (
await coingecko.create_plot(
coin_code = coin_code,
title = "{coin_name}-{vs_currency}".format(
coin_name = coin_name,
vs_currency = vs_currency_upper
)
)
),
filename = config.upload_filename
),
caption = TEXTS.channel_notify.format(
prices = "\n".join(prices_and_percentages),
coin_code = coin_code,
main_channel_url = config.main_channel_url,
main_channel_title = config.main_channel_title,
datetime = utils.format_datetime(
datetime = datetime.now(
tz = utc_timezone
)
)
)
)
except exceptions.TelegramAPIError:
pass
updated_prices[coin_name] = market_data["current_price"][config.coingecko.vs_currency]
DEFAULT_MESSAGE = TEXTS.default_message.format(
channels = "\n".join([
TEXTS.channel.format(
coin_name = coin_name,
channel_url = channel_url,
channel_price = TEXTS.channel_price.format(
currency_price = format(
updated_prices[coin_name],
".{numbers_after}f".format(
numbers_after = config.parse_prices[vs_currency_upper][1]
)
),
vs_currency = vs_currency_upper
)
)
for coin_name, (_, channel_url) in config.channels.items()
]),
main_channel_url = config.main_channel_url,
main_channel_title = config.main_channel_title,
datetime = utils.format_datetime(
datetime = datetime.now(
tz = utc_timezone
)
)
)
await edit_default_message()
if not CAN_STARTUP:
CAN_STARTUP = True
except Exception as ex:
logger.exception(
msg = "coingecko",
exc_info = ex
)
sleep_time: float = config.coingecko.prices_checker_delay - (utils.get_float_timestamp() - started_at)
if sleep_time > 0:
await sleep(sleep_time)
@dispatcher.startup()
async def on_startup_handler() -> None:
create_task(coingecko_prices_checker())
while not CAN_STARTUP:
await sleep(1)
@dispatcher.message(filters.Command("start"))
async def start_handler(message: types.Message) -> None:
await message.answer(
text = DEFAULT_MESSAGE,
disable_web_page_preview = True
)
@dispatcher.error()
async def error_handler(event: types.ErrorEvent) -> None:
logger.exception(
msg = "bot",
exc_info = event.exception
)
if __name__ == "__main__":
dispatcher.run_polling(bot)