ch20: renamed directory
This commit is contained in:
107
20-executors/getflags/flags2_asyncio.py
Executable file
107
20-executors/getflags/flags2_asyncio.py
Executable file
@@ -0,0 +1,107 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
"""Download flags of countries (with error handling).
|
||||
|
||||
asyncio async/await version
|
||||
|
||||
"""
|
||||
# tag::FLAGS2_ASYNCIO_TOP[]
|
||||
import asyncio
|
||||
from collections import Counter
|
||||
from http import HTTPStatus
|
||||
from pathlib import Path
|
||||
|
||||
import httpx
|
||||
import tqdm # type: ignore
|
||||
|
||||
from flags2_common import main, DownloadStatus, save_flag
|
||||
|
||||
# default set low to avoid errors from remote site, such as
|
||||
# 503 - Service Temporarily Unavailable
|
||||
DEFAULT_CONCUR_REQ = 5
|
||||
MAX_CONCUR_REQ = 1000
|
||||
|
||||
async def get_flag(session: httpx.AsyncClient, # <2>
|
||||
base_url: str,
|
||||
cc: str) -> bytes:
|
||||
url = f'{base_url}/{cc}/{cc}.gif'.lower()
|
||||
resp = await session.get(url, timeout=3.1, follow_redirects=True) # <3>
|
||||
resp.raise_for_status()
|
||||
return resp.content
|
||||
|
||||
async def download_one(session: httpx.AsyncClient,
|
||||
cc: str,
|
||||
base_url: str,
|
||||
semaphore: asyncio.Semaphore, # <4>
|
||||
verbose: bool) -> DownloadStatus:
|
||||
try:
|
||||
async with semaphore: # <5>
|
||||
image = await get_flag(session, base_url, cc)
|
||||
except httpx.HTTPStatusError as exc: # <4>
|
||||
res = exc.response
|
||||
if res.status_code == HTTPStatus.NOT_FOUND:
|
||||
status = DownloadStatus.NOT_FOUND # <5>
|
||||
msg = f'not found: {res.url}'
|
||||
else:
|
||||
raise
|
||||
|
||||
else:
|
||||
await asyncio.to_thread(save_flag, image, f'{cc}.gif')
|
||||
status = DownloadStatus.OK
|
||||
msg = 'OK'
|
||||
if verbose and msg:
|
||||
print(cc, msg)
|
||||
return status
|
||||
# end::FLAGS2_ASYNCIO_TOP[]
|
||||
|
||||
# tag::FLAGS2_ASYNCIO_START[]
|
||||
async def supervisor(cc_list: list[str],
|
||||
base_url: str,
|
||||
verbose: bool,
|
||||
concur_req: int) -> Counter[DownloadStatus]: # <1>
|
||||
counter: Counter[DownloadStatus] = Counter()
|
||||
semaphore = asyncio.Semaphore(concur_req) # <2>
|
||||
async with httpx.AsyncClient() as session:
|
||||
to_do = [download_one(session, cc, base_url, semaphore, verbose)
|
||||
for cc in sorted(cc_list)] # <3>
|
||||
to_do_iter = asyncio.as_completed(to_do) # <4>
|
||||
if not verbose:
|
||||
to_do_iter = tqdm.tqdm(to_do_iter, total=len(cc_list)) # <5>
|
||||
error: httpx.HTTPError | None = None
|
||||
for coro in to_do_iter: # <6>
|
||||
try:
|
||||
status = await coro # <7>
|
||||
except httpx.HTTPStatusError as exc: # <8>
|
||||
error_msg = 'HTTP error {resp.status_code} - {resp.reason_phrase}'
|
||||
error_msg = error_msg.format(resp=exc.response)
|
||||
error = exc
|
||||
except httpx.RequestError as exc: # <9>
|
||||
error_msg = f'{exc} {type(exc)}'.strip()
|
||||
error = exc
|
||||
except KeyboardInterrupt: # <10>
|
||||
break
|
||||
else: # <11>
|
||||
error = None
|
||||
|
||||
if error:
|
||||
status = DownloadStatus.ERROR # <12>
|
||||
if verbose:
|
||||
url = str(error.request.url) # <13>
|
||||
cc = Path(url).stem.upper() # <14>
|
||||
print(f'{cc} error: {error_msg}')
|
||||
counter[status] += 1
|
||||
|
||||
return counter
|
||||
|
||||
def download_many(cc_list: list[str],
|
||||
base_url: str,
|
||||
verbose: bool,
|
||||
concur_req: int) -> Counter[DownloadStatus]:
|
||||
coro = supervisor(cc_list, base_url, verbose, concur_req)
|
||||
counts = asyncio.run(coro) # <14>
|
||||
|
||||
return counts
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(download_many, DEFAULT_CONCUR_REQ, MAX_CONCUR_REQ)
|
||||
# end::FLAGS2_ASYNCIO_START[]
|
||||
Reference in New Issue
Block a user