Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Asyncio/Image-downloader/downloader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Download waifu images from www.thiswaifudoesnotexist.net using asyncio

import random
import io
import requests
import aiohttp
import asyncio
from PIL import Image


async def async_fetch(url: str) -> bytes:
"""fetch html content of a page. Returns a bytestring of the content"""
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
html_in_bytestring = await response.content.read()
return html_in_bytestring

async def waifu_fun():
rnum = random.randint(0, 100000)
url = 'https://www.thiswaifudoesnotexist.net/example-' + str(rnum) + '.jpg'
html = await async_fetch(url)
img = Image.open(io.BytesIO(html))
img.save(f'{rnum}.jpg')
print("Image Saved")

asyncio.run(waifu_fun())