From db3c235e1565815eeebf3e585955e21fc02d54ff Mon Sep 17 00:00:00 2001 From: j Date: Wed, 26 Oct 2016 23:35:49 +0200 Subject: [PATCH] support multiple instances of same document, extend url from api url --- pandora_client/__init__.py | 50 ++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/pandora_client/__init__.py b/pandora_client/__init__.py index 5ab092e..8e00e85 100755 --- a/pandora_client/__init__.py +++ b/pandora_client/__init__.py @@ -812,23 +812,33 @@ class Client(object): if f['id'] not in available and f['extension'] in DOCUMENT_FORMATS] return missing + def find_document(self, oshash): + r = self.api.findDocuments({ + 'keys': ['id'], + 'query': { + 'conditions': [ + {'key': 'oshash', 'value': oshash, 'operator': '=='} + ], + 'operator': '&' + } + }) + if r['data']['items']: + return r['data']['items'][0]['id'] + return None + def _add_document(self, f, item=None): if f.split('.')[-1] not in DOCUMENT_FORMATS: return False - url = '%supload/document/' % self._config['url'] - r = self.api.upload_chunks(url, f, { - 'filename': os.path.basename(f) - }) - if r and item: - oshash = ox.oshash(f) - r = self.api.findDocuments({ - "keys": ['id'], - "query": { - "conditions": [{"key": 'oshash', "value": oshash, "operator": '=='}], - "operator": '&' - } + oshash = ox.oshash(f) + did = self.find_document(oshash) + if not did: + url = '%supload/document/' % self._config['url'] + r = self.api.upload_chunks(url, f, { + 'filename': os.path.basename(f) }) - did = r['data']['items'][0]['id'] + if r and item: + did = self.find_document(oshash) + if item: r = self.api.addDocument({ 'id': did, 'item': item @@ -1061,12 +1071,16 @@ class API(ox.API): print(filename) hide_cursor() - result_url = data.get('url') - if 'uploadUrl' in data: - uploadUrl = data['uploadUrl'] - if uploadUrl.startswith('/'): + + def full_url(path): + if path.startswith('/'): u = urlparse(url) - uploadUrl = '%s://%s%s' % (u.scheme, u.netloc, uploadUrl) + path = '%s://%s%s' % (u.scheme, u.netloc, path) + return path + + result_url = full_url(data.get('url')) + if 'uploadUrl' in data: + uploadUrl = full_url(data['uploadUrl']) f = open(filename, 'rb') fsize = os.stat(filename).st_size done = 0