add api.save_url
This commit is contained in:
parent
d3d2bede5c
commit
14b6ad1a63
1 changed files with 17 additions and 2 deletions
19
ox/api.py
19
ox/api.py
|
@ -1,12 +1,14 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# vi:si:et:sw=4:sts=4:ts=4
|
||||
# GPL 2011
|
||||
from types import MethodType
|
||||
import gzip
|
||||
import os
|
||||
import shutil
|
||||
|
||||
from six.moves import http_cookiejar as cookielib
|
||||
import gzip
|
||||
from six import BytesIO, PY2
|
||||
from six.moves import urllib
|
||||
from types import MethodType
|
||||
|
||||
from . import __version__
|
||||
from .utils import json
|
||||
|
@ -122,3 +124,16 @@ class API(object):
|
|||
form.add_field('data', json.dumps(data))
|
||||
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)
|
||||
|
|
Loading…
Reference in a new issue