Skip to content

Instantly share code, notes, and snippets.

@tyilo
Created February 23, 2021 13:17
Show Gist options
  • Save tyilo/2c605e87840139ec002782df1ca2a223 to your computer and use it in GitHub Desktop.
Save tyilo/2c605e87840139ec002782df1ca2a223 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import asyncio
from pathlib import Path
from tqdm import tqdm
import httpx
def tasks_with_concurrency(n, *tasks):
semaphore = asyncio.Semaphore(n)
async def sem_task(task):
async with semaphore:
return await task
return [sem_task(task) for task in tasks]
async def download_card_image(client, card_id):
path = Path(f"{card_id}.png")
try:
with path.open("xb") as f:
async with client.stream("GET", f"https://art.hearthstonejson.com/v1/render/latest/enUS/256x/{card_id}.png") as r:
async for chunk in r.aiter_bytes():
f.write(chunk)
except FileExistsError:
pass
async def main():
async with httpx.AsyncClient() as client:
r = await client.get(
"https://api.hearthstonejson.com/v1/latest/enUS/cards.json"
)
tasks = tasks_with_concurrency(20, *(download_card_image(client, card["id"]) for card in r.json()))
for t in tqdm(asyncio.as_completed(tasks), total=len(tasks)):
await t
asyncio.run(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment