-
Notifications
You must be signed in to change notification settings - Fork 0
/
cbr.py
57 lines (44 loc) · 1.7 KB
/
cbr.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
from bs4 import BeautifulSoup
from datetime import datetime, timezone, timedelta
import aiohttp
import asyncio
class Currency(object):
def __init__(self, session, offset=3):
self.session = session
self.url = 'https://www.cbr.ru/currency_base/daily/'
self.offset = offset
self.update = self.get_time()
self._dict = {}
self._commands = {}
def get_time(self):
return datetime.now(timezone(timedelta(hours=self.offset)))
async def get_page_cbr(self):
async with self.session.get(self.url) as response:
return await response.text()
async def get_currency(self):
html = await self.get_page_cbr()
soup = BeautifulSoup(html, 'lxml')
return soup.findAll('tr')
async def update_state(self):
for tr in await self.get_currency():
line = [td.text for td in tr.findChildren()]
if any(item.isdigit() for item in line):
self._dict[line[1]] = float(line[-1].replace(",", "."))
self._commands.update({line[1].lower(): line[3]})
async def get_currency_dict(self):
if not self._dict or self.update.date() != self.get_time().date():
self.update = self.get_time()
await self.update_state()
return self._dict
@property
async def currency(self):
if not self._dict or self.update.date() != self.get_time().date():
await self.update_state()
return self._dict
async def main():
async with aiohttp.ClientSession() as session:
currency = Currency(session)
await currency.update_state()
print(currency.currency)
if __name__ == '__main__':
asyncio.run(main())