use requests for api
This commit is contained in:
parent
4feacb4a97
commit
4b531c55aa
2 changed files with 30 additions and 44 deletions
73
ox/api.py
73
ox/api.py
|
@ -4,6 +4,7 @@
|
||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
from types import MethodType
|
from types import MethodType
|
||||||
import gzip
|
import gzip
|
||||||
|
import mimetypes
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
import sys
|
import sys
|
||||||
|
@ -13,6 +14,7 @@ from six.moves import http_cookiejar as cookielib
|
||||||
from six import BytesIO, PY2
|
from six import BytesIO, PY2
|
||||||
from six.moves import urllib
|
from six.moves import urllib
|
||||||
from six.moves.urllib.parse import urlparse
|
from six.moves.urllib.parse import urlparse
|
||||||
|
import requests
|
||||||
|
|
||||||
from . import __version__
|
from . import __version__
|
||||||
from .utils import json
|
from .utils import json
|
||||||
|
@ -37,12 +39,13 @@ class API(object):
|
||||||
self._cj = cj
|
self._cj = cj
|
||||||
else:
|
else:
|
||||||
self._cj = cookielib.CookieJar()
|
self._cj = cookielib.CookieJar()
|
||||||
self._opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(self._cj),
|
|
||||||
urllib.request.HTTPHandler(debuglevel=self.debuglevel))
|
|
||||||
self._opener.addheaders = [
|
|
||||||
('User-Agent', '%s/%s' % (self.__name__, self.__version__))
|
|
||||||
]
|
|
||||||
|
|
||||||
|
self._requests_session = requests.Session()
|
||||||
|
self._requests_session.cookies = self._cj
|
||||||
|
self._requests_session.headers = {
|
||||||
|
'User-Agent': '%s/%s' % (self.__name__, self.__version__),
|
||||||
|
'Accept-Encoding': 'gzip, deflate',
|
||||||
|
}
|
||||||
self.url = url
|
self.url = url
|
||||||
r = self._request('api', {'docs': True})
|
r = self._request('api', {'docs': True})
|
||||||
self._properties = r['data']['actions']
|
self._properties = r['data']['actions']
|
||||||
|
@ -76,26 +79,12 @@ class API(object):
|
||||||
method.func_name = action
|
method.func_name = action
|
||||||
self._add_method(method, action)
|
self._add_method(method, action)
|
||||||
|
|
||||||
def _json_request(self, url, form):
|
def _json_request(self, url, data, files=None):
|
||||||
result = {}
|
result = {}
|
||||||
try:
|
try:
|
||||||
body = form.body()
|
request = self._requests_session.post(url, data=data, files=files)
|
||||||
if PY2:
|
result = request.json()
|
||||||
if not isinstance(url, bytes):
|
return result
|
||||||
url = url.encode('utf-8')
|
|
||||||
request = urllib.request.Request(url)
|
|
||||||
request.add_data(body)
|
|
||||||
else:
|
|
||||||
request = urllib.request.Request(url, data=body, method='POST')
|
|
||||||
request.add_header('Content-Type', form.get_content_type())
|
|
||||||
request.add_header('Content-Length', str(len(body)))
|
|
||||||
request.add_header('Accept-Encoding', 'gzip, deflate')
|
|
||||||
f = self._opener.open(request)
|
|
||||||
result = f.read()
|
|
||||||
if f.headers.get('content-encoding', None) == 'gzip':
|
|
||||||
result = gzip.GzipFile(fileobj=BytesIO(result)).read()
|
|
||||||
result = result.decode('utf-8')
|
|
||||||
return json.loads(result)
|
|
||||||
except urllib.error.HTTPError as e:
|
except urllib.error.HTTPError as e:
|
||||||
if self.DEBUG:
|
if self.DEBUG:
|
||||||
import webbrowser
|
import webbrowser
|
||||||
|
@ -125,17 +114,15 @@ class API(object):
|
||||||
raise
|
raise
|
||||||
|
|
||||||
def _request(self, action, data=None):
|
def _request(self, action, data=None):
|
||||||
form = MultiPartForm()
|
form = {
|
||||||
form.add_field('action', action)
|
'action': action
|
||||||
|
}
|
||||||
if data:
|
if data:
|
||||||
form.add_field('data', json.dumps(data))
|
form['data'] = json.dumps(data)
|
||||||
return self._json_request(self.url, form)
|
return self._json_request(self.url, form)
|
||||||
|
|
||||||
def get_url(self, url):
|
def get_url(self, url):
|
||||||
request = urllib.request.Request(url, method='GET')
|
return self._requests_session.get(url).content
|
||||||
f = self._opener.open(request)
|
|
||||||
result = f.read()
|
|
||||||
return result
|
|
||||||
|
|
||||||
def save_url(self, url, filename, overwrite=False):
|
def save_url(self, url, filename, overwrite=False):
|
||||||
chunk_size = 16 * 1024
|
chunk_size = 16 * 1024
|
||||||
|
@ -143,21 +130,15 @@ class API(object):
|
||||||
dirname = os.path.dirname(filename)
|
dirname = os.path.dirname(filename)
|
||||||
if dirname and not os.path.exists(dirname):
|
if dirname and not os.path.exists(dirname):
|
||||||
os.makedirs(dirname)
|
os.makedirs(dirname)
|
||||||
request = urllib.request.Request(url, method='GET')
|
|
||||||
tmpname = filename + '.tmp'
|
tmpname = filename + '.tmp'
|
||||||
with open(tmpname, 'wb') as fd:
|
with open(tmpname, 'wb') as fd:
|
||||||
u = self._opener.open(request)
|
r = self._requests_session.get(url)
|
||||||
for chunk in iter(lambda: u.read(chunk_size), b''):
|
for chunk in iter(lambda: r.read(chunk_size), b''):
|
||||||
fd.write(chunk)
|
fd.write(chunk)
|
||||||
shutil.move(tmpname, filename)
|
shutil.move(tmpname, filename)
|
||||||
|
|
||||||
|
|
||||||
def upload_chunks(self, url, filename, data=None, silent=False):
|
def upload_chunks(self, url, filename, data=None, silent=False):
|
||||||
form = MultiPartForm()
|
data = self._json_request(url, data)
|
||||||
if data:
|
|
||||||
for key in data:
|
|
||||||
form.add_field(key, data[key])
|
|
||||||
data = self._json_request(url, form)
|
|
||||||
|
|
||||||
def full_url(path):
|
def full_url(path):
|
||||||
if path.startswith('/'):
|
if path.startswith('/'):
|
||||||
|
@ -178,16 +159,20 @@ class API(object):
|
||||||
resume_offset = 0
|
resume_offset = 0
|
||||||
chunk = f.read(CHUNK_SIZE)
|
chunk = f.read(CHUNK_SIZE)
|
||||||
fname = os.path.basename(filename)
|
fname = os.path.basename(filename)
|
||||||
|
mime_type = mimetypes.guess_type(fname)[0] or 'application/octet-stream'
|
||||||
if not isinstance(fname, bytes):
|
if not isinstance(fname, bytes):
|
||||||
fname = fname.encode('utf-8')
|
fname = fname.encode('utf-8')
|
||||||
while chunk:
|
while chunk:
|
||||||
form = MultiPartForm()
|
meta = {
|
||||||
form.add_file('chunk', fname, chunk)
|
'offset': str(done)
|
||||||
|
}
|
||||||
if len(chunk) < CHUNK_SIZE or f.tell() == fsize:
|
if len(chunk) < CHUNK_SIZE or f.tell() == fsize:
|
||||||
form.add_field('done', '1')
|
meta['done'] = '1'
|
||||||
form.add_field('offset', str(done))
|
files = [
|
||||||
|
('chunk', (fname, chunk, mime_type))
|
||||||
|
]
|
||||||
try:
|
try:
|
||||||
data = self._json_request(uploadUrl, form)
|
data = self._json_request(uploadUrl, meta, files=files)
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
if not slient:
|
if not slient:
|
||||||
print("\ninterrupted by user.")
|
print("\ninterrupted by user.")
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
chardet
|
chardet
|
||||||
six>=1.5.2
|
six>=1.5.2
|
||||||
lxml
|
lxml
|
||||||
|
requests
|
||||||
|
|
Loading…
Reference in a new issue