add api.save_url

This commit is contained in:
j 2018-03-09 17:17:14 +05:30
parent d3d2bede5c
commit 14b6ad1a63

View file

@ -1,12 +1,14 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4 # vi:si:et:sw=4:sts=4:ts=4
# GPL 2011 # GPL 2011
from types import MethodType
import gzip
import os
import shutil
from six.moves import http_cookiejar as cookielib from six.moves import http_cookiejar as cookielib
import gzip
from six import BytesIO, PY2 from six import BytesIO, PY2
from six.moves import urllib from six.moves import urllib
from types import MethodType
from . import __version__ from . import __version__
from .utils import json from .utils import json
@ -122,3 +124,16 @@ class API(object):
form.add_field('data', json.dumps(data)) form.add_field('data', json.dumps(data))
return self._json_request(self.url, form) return self._json_request(self.url, form)
def save_url(self, url, filename, overwrite=False):
chunk_size = 16 * 1024
if not os.path.exists(filename) or overwrite:
dirname = os.path.dirname(filename)
if dirname and not os.path.exists(dirname):
os.makedirs(dirname)
request = urllib.request.Request(url, method='GET')
tmpname = filename + '.tmp'
with open(tmpname, 'wb') as fd:
u = self._opener.open(request)
for chunk in iter(lambda: u.read(chunk_size), b''):
fd.write(chunk)
shutil.move(tmpname, filename)