Commit 72942021 authored by Ad Schellevis's avatar Ad Schellevis

(ids) change download buffering

parent d9387089
...@@ -32,7 +32,6 @@ import syslog ...@@ -32,7 +32,6 @@ import syslog
import tarfile import tarfile
import gzip import gzip
import zipfile import zipfile
import shutil
import tempfile import tempfile
import requests import requests
...@@ -122,10 +121,16 @@ class Downloader(object): ...@@ -122,10 +121,16 @@ class Downloader(object):
frm_url = url.replace('//', '/').replace(':/', '://') frm_url = url.replace('//', '/').replace(':/', '://')
# stream to temp file # stream to temp file
if frm_url not in self._download_cache: if frm_url not in self._download_cache:
req = requests.get(url=frm_url, stream=True) req = requests.get(url=frm_url, stream=True, verify=False)
if req.status_code == 200: if req.status_code == 200:
src = tempfile.NamedTemporaryFile() src = tempfile.NamedTemporaryFile('wb+', 10240)
shutil.copyfileobj(req.raw, src) while True:
data = req.raw.read(10240)
if not data:
break
else:
src.write(data)
src.seek(0)
self._download_cache[frm_url] = src self._download_cache[frm_url] = src
# process rules from tempfile (prevent duplicate download for files within an archive) # process rules from tempfile (prevent duplicate download for files within an archive)
...@@ -134,7 +139,7 @@ class Downloader(object): ...@@ -134,7 +139,7 @@ class Downloader(object):
target_filename = '%s/%s' % (self._target_dir, filename) target_filename = '%s/%s' % (self._target_dir, filename)
save_data = self._unpack(self._download_cache[frm_url], url, url_filename) save_data = self._unpack(self._download_cache[frm_url], url, url_filename)
save_data = self.filter(save_data, input_filter) save_data = self.filter(save_data, input_filter)
open(target_filename, 'wb').write(save_data) open(target_filename, 'w', buffering=10240).write(save_data)
except IOError: except IOError:
syslog.syslog(syslog.LOG_ERR, 'cannot write to %s' % target_filename) syslog.syslog(syslog.LOG_ERR, 'cannot write to %s' % target_filename)
return None return None
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment