retrieving URL file size correctly | improving downloaded file size format

This commit is contained in:
Vinicius Moreira
2019-10-09 15:01:46 -03:00
parent ec12bbb1de
commit 6132155614
9 changed files with 35 additions and 14 deletions

View File

@@ -4,6 +4,8 @@ import traceback
import requests
SIZE_MULTIPLIERS = ((0.001, 'Kb'), (0.000001, 'Mb'), (0.000000001, 'Gb'), (0.000000000001, 'Tb'))
class HttpClient:
@@ -43,12 +45,20 @@ class HttpClient:
res = self.get(url)
return res.json() if res else None
def get_content_length(self, url: str) -> int:
def get_content_length(self, url: str) -> str:
"""
:param url:
:return:
"""
res = self.session.head(url)
res = self.session.get(url, allow_redirects=True, stream=True)
if res.status_code == 200:
return res.headers['content-length']
size = int(res.headers.get('Content-Length'))
if size is not None:
for m in SIZE_MULTIPLIERS:
size_str = str(size * m[0])
if len(size_str.split('.')[0]) < 4:
return '{0:.2f}'.format(float(size_str)) + ' ' + m[1]
return str(size)