This repository was archived by the owner on Apr 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
51 lines (39 loc) · 1.24 KB
/
utils.py
File metadata and controls
51 lines (39 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from aiofile import async_open
import aiohttp
import asyncio
"""
Utils functions to manage attachments
"""
async def request_header(client, url):
print(' Get attachment header: %s' % url)
res = None
try:
response = await client.head(url)
headers = response.headers
res = {}
res['content-type'] = headers['Content-Type']
except Exception as err:
res = 'Errore'
print(' %s' % str(err))
return {'url': url, 'response': res}
async def request(client, url):
print(' Get attachment: %s' % url)
res = None
file_name = None
try:
res = await client.get(url)
headers = res.headers
content = await res.read()
file_name = get_name_from_url(url)
open(file_name, 'wb').write(content)
except Exception as err:
headers = 'Errore'
print(' %s' % str(err))
return {'url': url, 'file_name': file_name, 'response': res.status}
async def multi_requests(urls, req_func):
async with aiohttp.ClientSession() as client:
tasks = [req_func(client, url) for url in urls]
results = await asyncio.gather(*tasks)
return results
def get_name_from_url(url):
return url.split('/')[-1]