http: make RequetsClient.file use streaming
This commit is contained in:
parent
48f0178405
commit
096dfd159b
|
@ -97,7 +97,7 @@ class HttpClient(object):
|
|||
request.remove_header('host')
|
||||
|
||||
|
||||
def __build_request(self, url, data=None, headers={}, method='GET'):
|
||||
def __build_request(self, url, data=None, headers={}, method='GET', stream=False):
|
||||
new_headers = self.headers.copy()
|
||||
new_headers.update(headers)
|
||||
|
||||
|
@ -112,7 +112,7 @@ class HttpClient(object):
|
|||
if isinstance(data, str):
|
||||
data = data.encode('UTF-8')
|
||||
|
||||
request = Request(url, data=data, headers=parsed_headers, method=method)
|
||||
request = Request(url, data=data, headers=parsed_headers, method=method, stream=stream)
|
||||
|
||||
if self.proxy.enabled:
|
||||
request.set_proxy(f'{self.proxy.host}:{self.proxy.port}', self.proxy.ptype)
|
||||
|
@ -136,21 +136,31 @@ class HttpClient(object):
|
|||
return HttpResponse(response)
|
||||
|
||||
|
||||
def file(self, url, filepath, *args, filename=None, **kwargs):
|
||||
resp = self.request(url, *args, **kwargs)
|
||||
def file(self, url, filepath, *args, filename=None, size=2048, **kwargs):
|
||||
path = Path(filepath)
|
||||
filepath = path.join(filename if filename else Path(url).name)
|
||||
|
||||
if filepath.exists():
|
||||
kwargs['headers']['range'] = f'bytes={filepath.size}'
|
||||
|
||||
resp = self.request(url, *args, stream=True, **kwargs)
|
||||
|
||||
if not resp.headers.get('content-length'):
|
||||
logging.verbose('File already downloaded fully')
|
||||
return True
|
||||
|
||||
if resp.status != 200:
|
||||
logging.error(f'Failed to download {url}:', resp.status, resp.body)
|
||||
return False
|
||||
|
||||
path = Path(filepath)
|
||||
|
||||
if not path.exists():
|
||||
logging.error('Path does not exist:', path)
|
||||
return False
|
||||
|
||||
with path.join(filename).open('wb') as fd:
|
||||
fd.write(resp.body)
|
||||
with filepath.open('ab') as fd:
|
||||
for chunk in resp.chunks(size):
|
||||
fd.write(chunk)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
|
|
|
@ -491,6 +491,10 @@ class Path(object):
|
|||
return os.path.getmtime(self.str())
|
||||
|
||||
|
||||
def size(self):
|
||||
return self.__path.stat().st_size
|
||||
|
||||
|
||||
def link(self, path):
|
||||
target = Path(path)
|
||||
|
||||
|
|
Loading…
Reference in a new issue