forked from delta1512/GRC-Wallet-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GRC_pricebot.py
26 lines (23 loc) · 875 Bytes
/
GRC_pricebot.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
from time import time
import async_timeout
import logging
import aiohttp
import asyncio
import json
class price_bot:
last_updated = 0
last_price = 0
timeout_sec = 380
price_url = 'https://api.coinmarketcap.com/v1/ticker/gridcoin/'
async def price(self):
if round(time()) > self.last_updated+self.timeout_sec:
try:
async with aiohttp.ClientSession() as session:
async with session.get(self.price_url) as response:
self.last_price = float(json.loads(await response.text())[0]['price_usd'])
except Exception as E:
logging.warning('[WARN] Error when trying to fetch USD price: '.format(E))
self.last_updated = round(time())
return self.last_price
async def conv(self, amt):
return round(amt*(await self.price()), 2)