-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcoin_api.py
38 lines (30 loc) · 1.06 KB
/
coin_api.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
import asyncio
import aiohttp
import json
class CoinApi:
""" API implementation for retrieving the current pricing. """
url = 'https://min-api.cryptocompare.com/data/generateAvg?fsym={}&tsym={}&e={}'
loop = None
def __init__(self, loop, coin, market, currency):
self.loop = loop
self.coin = coin
self.market = market
self.currency = currency
async def percent_change(self):
""" called to retrieve data from the API """
async with aiohttp.ClientSession(loop=self.loop) as session:
return await self.get(session)
async def get(self, session):
async with session.get(self.url.format(self.coin, self.currency, self.market)) as resp:
await asyncio.sleep(3)
percent_change = json.loads(await resp.text())['DISPLAY']['CHANGEPCT24HOUR']
return float(percent_change)
def get_coin(self):
return self.coin
def get_market(self):
return self.market
def get_endpoint(self):
return self.url.format(self.coin, self.currency, self.market)
def throttle(self):
""" returns the minimum number of seconds to wait before calling data. """
return 30