1616
1717from flags2_common import main , DownloadStatus , save_flag
1818
19- # default set low to avoid errors from remote site, such as
20- # 503 - Service Temporarily Unavailable
19+ # low concurrency default to avoid errors from remote site,
20+ # such as 503 - Service Temporarily Unavailable
2121DEFAULT_CONCUR_REQ = 5
2222MAX_CONCUR_REQ = 1000
2323
24- async def get_flag (session : httpx .AsyncClient , # <2 >
24+ async def get_flag (client : httpx .AsyncClient , # <1 >
2525 base_url : str ,
2626 cc : str ) -> bytes :
2727 url = f'{ base_url } /{ cc } /{ cc } .gif' .lower ()
28- resp = await session .get (url , timeout = 3.1 , follow_redirects = True ) # <3 >
28+ resp = await client .get (url , timeout = 3.1 , follow_redirects = True ) # <2 >
2929 resp .raise_for_status ()
3030 return resp .content
3131
32- async def download_one (session : httpx .AsyncClient ,
32+ async def download_one (client : httpx .AsyncClient ,
3333 cc : str ,
3434 base_url : str ,
35- semaphore : asyncio .Semaphore , # <4>
35+ semaphore : asyncio .Semaphore ,
3636 verbose : bool ) -> DownloadStatus :
3737 try :
38- async with semaphore : # <5 >
39- image = await get_flag (session , base_url , cc )
40- except httpx .HTTPStatusError as exc : # <4 >
38+ async with semaphore : # <3 >
39+ image = await get_flag (client , base_url , cc )
40+ except httpx .HTTPStatusError as exc : # <5 >
4141 res = exc .response
4242 if res .status_code == HTTPStatus .NOT_FOUND :
43- status = DownloadStatus .NOT_FOUND # <5>
43+ status = DownloadStatus .NOT_FOUND
4444 msg = f'not found: { res .url } '
4545 else :
4646 raise
47-
4847 else :
49- await asyncio .to_thread (save_flag , image , f'{ cc } .gif' )
48+ await asyncio .to_thread (save_flag , image , f'{ cc } .gif' ) # <6>
5049 status = DownloadStatus .OK
5150 msg = 'OK'
5251 if verbose and msg :
@@ -61,33 +60,31 @@ async def supervisor(cc_list: list[str],
6160 concur_req : int ) -> Counter [DownloadStatus ]: # <1>
6261 counter : Counter [DownloadStatus ] = Counter ()
6362 semaphore = asyncio .Semaphore (concur_req ) # <2>
64- async with httpx .AsyncClient () as session :
65- to_do = [download_one (session , cc , base_url , semaphore , verbose )
63+ async with httpx .AsyncClient () as client :
64+ to_do = [download_one (client , cc , base_url , semaphore , verbose )
6665 for cc in sorted (cc_list )] # <3>
6766 to_do_iter = asyncio .as_completed (to_do ) # <4>
6867 if not verbose :
6968 to_do_iter = tqdm .tqdm (to_do_iter , total = len (cc_list )) # <5>
70- error : httpx .HTTPError | None = None
71- for coro in to_do_iter : # <6 >
69+ error : httpx .HTTPError | None = None # <6>
70+ for coro in to_do_iter : # <7 >
7271 try :
73- status = await coro # <7 >
74- except httpx .HTTPStatusError as exc : # <8>
72+ status = await coro # <8 >
73+ except httpx .HTTPStatusError as exc :
7574 error_msg = 'HTTP error {resp.status_code} - {resp.reason_phrase}'
7675 error_msg = error_msg .format (resp = exc .response )
77- error = exc
78- except httpx .RequestError as exc : # <9>
76+ error = exc # <9>
77+ except httpx .RequestError as exc :
7978 error_msg = f'{ exc } { type (exc )} ' .strip ()
80- error = exc
81- except KeyboardInterrupt : # <10>
79+ error = exc # <10>
80+ except KeyboardInterrupt :
8281 break
83- else : # <11>
84- error = None
8582
8683 if error :
87- status = DownloadStatus .ERROR # <12 >
84+ status = DownloadStatus .ERROR # <11 >
8885 if verbose :
89- url = str (error .request .url ) # <13 >
90- cc = Path (url ).stem .upper () # <14 >
86+ url = str (error .request .url ) # <12 >
87+ cc = Path (url ).stem .upper () # <13 >
9188 print (f'{ cc } error: { error_msg } ' )
9289 counter [status ] += 1
9390
0 commit comments