Last active
October 22, 2018 22:40
-
-
Save Anna-Myzukina/4efd1ef8eb9af84d66453e4186cb8c7f to your computer and use it in GitHub Desktop.
GetPhotoFromMars
This file contains hidden or 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
| from aiohttp import web | |
| async def get_mars_photo(request): | |
| return web.Response(text='A photo of Mars') | |
| app = web.Application() | |
| app.router.add_get('/', get_mars_photo, name='mars_photo') | |
| web.run_app(app, host='127.0.0.1', port=8080) | |
| #корутина get_mars_photo — обработчик запросов; принимает HTTP запрос в качестве аргумента и подготавливает содержимое для HTTP ответа (ну или бросает исключение) | |
| #app — высокоуровневый сервер; он поддерживает роутинг, middleware и сигналы (в примере будет показан только роутинг) | |
| #app.router.add_get — регистрирует обработчик HTTP метода GET по пути '/' | |
| #Примечание: обработчиком запросов может также быть и обычная функция, а не только корутина. | |
| #Но чтобы понять всю мощь asyncio, большинство функций будут определены как async def. |
This file contains hidden or 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
| #!/usr/bin/env python3 | |
| import io | |
| import random | |
| from aiohttp import web, ClientSession | |
| from aiohttp.web import HTTPFound | |
| from PIL import Image | |
| NASA_API_KEY = 'DEMO_KEY' | |
| ROVER_URL = 'https://api.nasa.gov/mars-photos/api/v1/rovers/curiosity/photos' | |
| async def validate_image(image_bytes): | |
| image = Image.open(io.BytesIO(image_bytes)) | |
| return image.width >= 1024 and image.height >= 1024 and image.mode != 'L' | |
| async def get_mars_image_url_from_nasa(): | |
| while True: | |
| sol = random.randint(0, 1722) | |
| params = {'sol': sol, 'api_key': NASA_API_KEY} | |
| async with ClientSession() as session: | |
| async with session.get(ROVER_URL, params=params) as resp: | |
| resp_dict = await resp.json() | |
| if 'photos' not in resp_dict: | |
| raise Exception | |
| photos = resp_dict['photos'] | |
| if not photos: | |
| continue | |
| return random.choice(photos)['img_src'] | |
| async def get_mars_photo_bytes(): | |
| while True: | |
| image_url = await get_mars_image_url_from_nasa() | |
| async with ClientSession() as session: | |
| async with session.get(image_url) as resp: | |
| image_bytes = await resp.read() | |
| if await validate_image(image_bytes): | |
| break | |
| return image_bytes | |
| async def get_mars_photo(request): | |
| image = await get_mars_photo_bytes() | |
| return web.Response(body=image, content_type='image/jpeg') | |
| app = web.Application() | |
| app.router.add_get('/', get_mars_photo, name='mars_photo') | |
| web.run_app(app, host='127.0.0.1', port=8080) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment