Skip to content

Instantly share code, notes, and snippets.

@bersegosx
Created July 21, 2018 17:18
Show Gist options
  • Save bersegosx/5b6fc351f4019e635cd14a23c963cad6 to your computer and use it in GitHub Desktop.
Save bersegosx/5b6fc351f4019e635cd14a23c963cad6 to your computer and use it in GitHub Desktop.
import asyncio
import io
import imghdr
from PIL import Image
from aiohttp import ClientSession
import motor.motor_asyncio
client = motor.motor_asyncio.AsyncIOMotorClient()
db = client.test_database
async def save_to_mongo(document):
result = await db.test_collection.insert_one(document)
return result
def get_image_sizes(image_bytes):
img = io.BytesIO(image_bytes)
assert imghdr.what(img), 'It isnt an image'
image = Image.open(img)
return image.width, image.height
async def download(url):
async with ClientSession() as session:
async with session.get(url) as resp:
assert resp.status == 200
result = await resp.read()
return result
async def process_images(urls):
assert len(urls) < 50
async def process_image(url):
image_data = await download(url)
width, height = get_image_sizes(image_data)
result = await save_to_mongo({
'url': url,
'width': width,
'height': height
})
return result
await asyncio.gather(
*[process_image(url) for url in urls],
return_exceptions=True
)
if __name__ == '__main__':
urls = [
"http://www.e1.ru", # isn't image
"https://static.ngs.ru/news/99/preview/f026eb9b2ec0ec137f7578384744cd3f085b5da3_1079.jpg",
"https://static.ngs.ru/news/99/preview/6fc818ccd6eef6daa10e06045347c5972176888dd_700.JPG",
"http://www.blog.pythonlibrary.org/wp-content/uploads/2018/07/jupy_wp_widget.jpg",
]
loop = asyncio.get_event_loop()
loop.run_until_complete(process_images(urls*10))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment