Skip to content

Instantly share code, notes, and snippets.

@delivrance
Created April 28, 2020 00:21
Show Gist options
  • Save delivrance/42032b581df38092dcd5c69cf07b0fd6 to your computer and use it in GitHub Desktop.
Save delivrance/42032b581df38092dcd5c69cf07b0fd6 to your computer and use it in GitHub Desktop.
Download all available xkcd comics metadata as json list
import asyncio
import aiohttp
import json
async def main():
async with aiohttp.ClientSession() as session:
n = (await (await session.get("https://xkcd.com/info.0.json")).json())["num"]
with open("xkcd.json", "w") as f:
f.write(json.dumps([
c for c in await asyncio.gather(*[
r.json()
for r in await asyncio.gather(*[
session.get(f"https://xkcd.com/{i}/info.0.json")
for i in range(1, n + 1)
if i not in [404]
])
])
], indent=4))
asyncio.run(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment