From 4b531c55aafa5c11bc4e8aa8540e74f9f32929c0 Mon Sep 17 00:00:00 2001 From: j Date: Thu, 6 Jul 2023 18:35:13 +0530 Subject: [PATCH] use requests for api --- ox/api.py | 73 +++++++++++++++++++----------------------------- requirements.txt | 1 + 2 files changed, 30 insertions(+), 44 deletions(-) diff --git a/ox/api.py b/ox/api.py index 894e26d..639fec0 100644 --- a/ox/api.py +++ b/ox/api.py @@ -4,6 +4,7 @@ from __future__ import print_function from types import MethodType import gzip +import mimetypes import os import shutil import sys @@ -13,6 +14,7 @@ from six.moves import http_cookiejar as cookielib from six import BytesIO, PY2 from six.moves import urllib from six.moves.urllib.parse import urlparse +import requests from . import __version__ from .utils import json @@ -37,12 +39,13 @@ class API(object): self._cj = cj else: 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 r = self._request('api', {'docs': True}) self._properties = r['data']['actions'] @@ -76,26 +79,12 @@ class API(object): method.func_name = action self._add_method(method, action) - def _json_request(self, url, form): + def _json_request(self, url, data, files=None): result = {} try: - body = form.body() - if PY2: - if not isinstance(url, bytes): - 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) + request = self._requests_session.post(url, data=data, files=files) + result = request.json() + return result except urllib.error.HTTPError as e: if self.DEBUG: import webbrowser @@ -125,17 +114,15 @@ class API(object): raise def _request(self, action, data=None): - form = MultiPartForm() - form.add_field('action', action) + form = { + 'action': action + } if data: - form.add_field('data', json.dumps(data)) + form['data'] = json.dumps(data) return self._json_request(self.url, form) def get_url(self, url): - request = urllib.request.Request(url, method='GET') - f = self._opener.open(request) - result = f.read() - return result + return self._requests_session.get(url).content def save_url(self, url, filename, overwrite=False): chunk_size = 16 * 1024 @@ -143,21 +130,15 @@ class API(object): 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''): + r = self._requests_session.get(url) + for chunk in iter(lambda: r.read(chunk_size), b''): fd.write(chunk) shutil.move(tmpname, filename) - def upload_chunks(self, url, filename, data=None, silent=False): - form = MultiPartForm() - if data: - for key in data: - form.add_field(key, data[key]) - data = self._json_request(url, form) + data = self._json_request(url, data) def full_url(path): if path.startswith('/'): @@ -178,16 +159,20 @@ class API(object): resume_offset = 0 chunk = f.read(CHUNK_SIZE) fname = os.path.basename(filename) + mime_type = mimetypes.guess_type(fname)[0] or 'application/octet-stream' if not isinstance(fname, bytes): fname = fname.encode('utf-8') while chunk: - form = MultiPartForm() - form.add_file('chunk', fname, chunk) + meta = { + 'offset': str(done) + } if len(chunk) < CHUNK_SIZE or f.tell() == fsize: - form.add_field('done', '1') - form.add_field('offset', str(done)) + meta['done'] = '1' + files = [ + ('chunk', (fname, chunk, mime_type)) + ] try: - data = self._json_request(uploadUrl, form) + data = self._json_request(uploadUrl, meta, files=files) except KeyboardInterrupt: if not slient: print("\ninterrupted by user.") diff --git a/requirements.txt b/requirements.txt index 51c3f99..4e7d966 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ chardet six>=1.5.2 lxml +requests