Skip to content

Instantly share code, notes, and snippets.

@JialunC
Last active October 8, 2020 03:12
Show Gist options
  • Save JialunC/834d015552d5d6e402894bbd9fb48e89 to your computer and use it in GitHub Desktop.
Save JialunC/834d015552d5d6e402894bbd9fb48e89 to your computer and use it in GitHub Desktop.
Async_code_2 for downloading json (with aiofiles)
import asyncio
import aiohttp
# import json
import aiofiles
import config
async def store_json_2(symbol):
print(f'============downloading {symbol} json data ============')
payload = {
'function': 'TIME_SERIES_DAILY_ADJUSTED',
'symbol': symbol,
'outputsize': 'full',
'apikey': config.ALPHA_API_KEY,
# AA api default data type is json, another option is csv
'datatype': 'json'
}
async with aiohttp.ClientSession() as session:
async with session.get('https://www.alphavantage.co/query?', params=payload) as r:
data = await r.text()
async with aiofiles.open(f'./data/data_{symbol}.json', 'w') as outfile:
await outfile.write(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment