10
10
11
11
12
12
def _get_filepath_from_url (url , dirpath ):
13
+ """Returns filepath from base file name in URL and directory path."""
13
14
url_path_list = urllib .parse .urlsplit (url )
14
15
abs_filepath = url_path_list .path
15
16
basepath = os .path .split (abs_filepath )[- 1 ]
16
17
return os .path .join (dirpath , basepath )
17
18
18
19
19
20
async def async_fetch (session , url ):
21
+ """Asynchronous I/O HTTP GET request with a ClientSession instantiated from the aiohttp library."""
20
22
async with session .get (url ) as response :
21
23
status = response .status
22
24
if status != 200 :
@@ -27,6 +29,10 @@ async def async_fetch(session, url):
27
29
28
30
29
31
async def async_fetch_and_write (session , url , dirpath ):
32
+ """Asynchronous I/O HTTP GET request with a ClientSession instantiated from the aiohttp library, followed
33
+ by an asynchronous I/O file write of the binary to disk with the aiofiles library.
34
+
35
+ :returns `FWRes` namedtuple with url, filepath, http_status, write_success fields"""
30
36
FWResponse = namedtuple (
31
37
"FWRes" , ["url" , "filepath" , "http_status" , "write_success" ]
32
38
)
@@ -45,6 +51,10 @@ async def async_fetch_and_write(session, url, dirpath):
45
51
46
52
47
53
async def create_async_get_request_session_and_run (urls , dirpath ):
54
+ """Creates an aiohttp library ClientSession and performs asynchronous GET requests +
55
+ binary file writes with the binary response from the GET request.
56
+
57
+ :returns list of asyncio Tasks that include `FWRes` namedtuple instances (defined in async_fetch_and_write)"""
48
58
async with aiohttp .ClientSession () as session :
49
59
tasks = []
50
60
for url in urls :
0 commit comments