Last active
March 8, 2017 12:51
-
-
Save Natim/438cd9a6300fd489ee0da3ded68517b5 to your computer and use it in GitHub Desktop.
Get anchors from Woleet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import aiohttp | |
import asyncio | |
import os | |
WOLEET_SERVER = 'https://api.woleet.io/v1' | |
WOLEET_HEADERS = { | |
'Authorization': 'Bearer {}'.format(os.getenv('WOLEET_BEARER_TOKEN')), | |
'Content-Type': 'application/json' | |
} | |
TAG = 'diplome' | |
async def get_anchors(session, tags): | |
async with session.get('{}/anchors'.format(WOLEET_SERVER), | |
params={"tags": ','.join(tags), "size": 500}, | |
headers=WOLEET_HEADERS) as response: | |
response.raise_for_status() | |
content = await response.json() | |
return content['content'] | |
async def get_receipt(session, anchor_id, filename): | |
async with session.get('{}/receipt/{}'.format(WOLEET_SERVER, anchor_id), | |
headers=WOLEET_HEADERS) as response: | |
response.raise_for_status() | |
content = await response.read() | |
# 3. Use the ID of the anchor to save the file with the proper name | |
print("Saving {}".format(filename)) | |
with open(filename, 'wb') as f: | |
f.write(content) | |
async def main(loop): | |
async with aiohttp.ClientSession(loop=loop) as session: | |
# 1. Get all anchors with tag diplome | |
anchors = await get_anchors(session, [TAG]) | |
tasks = [] | |
for anchor in anchors: | |
# 2. For each anchor fetch the record | |
filename = 'receipts/{}.json'.format(anchor['metadata']['id']) | |
tasks.append(get_receipt(session, anchor['id'], filename)) | |
await asyncio.gather(*tasks) | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(main(loop)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment