Skip to content

Instantly share code, notes, and snippets.

Last active August 31, 2018 01:53
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save psiofxt/a4592cfd2c89a2e991acea9fb7dc9249 to your computer and use it in GitHub Desktop.
import asyncio
import aiohttp
import aiofiles
import json
from datetime import datetime, timedelta
async def fetch(url):
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
return await response.json()
async def open_file(filename):
async with, mode='r') as f:
return await
def date_generator(from_date, to_date):
curr = from_date
while curr <= to_date:
yield curr
curr += timedelta(days=1)
async def price_writer(currency, fiat):
pair = fiat + '_' + currency
response = await fetch(
'data/histoday?fsym={}&tsym={}&allData=true'.format(currency, fiat)
price_data = response['Data']
sorted_data = [
data for data in price_data
async with'{}.json'.format(pair), mode='w') as f:
await f.write(json.dumps(sorted_data, sort_keys=True, indent=4))
print("Wrote data for pair {}_{}".format(currency, fiat))
async def price_getter(currency, fiat, from_date, to_date):
pair = fiat + '_' + currency
price_list = await (open_file('{}.json'.format(pair)))
price_data = json.loads(price_list)
prices = [price_data[0][d.strftime('%Y-%m-%d')]
for d in date_generator(datetime.utcfromtimestamp(from_date),
except ValueError:
return []
except KeyError:
return []
return prices
async def run():
currencies = ['ETH', 'BTC']
fiats = ['USD', 'AUD', 'JPY']
for currency in currencies:
for fiat in fiats:
await price_writer(currency, fiat)
prices = await price_getter('ETH', 'USD', 1535328000, 1535328000)
if __name__ == '__main__':
loop = asyncio.get_event_loop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment