From a9efae5b3c4fc884ae1cb6e3a93d92a0aa6c7a8c Mon Sep 17 00:00:00 2001 From: stijnvanhoey Date: Mon, 6 Jun 2016 16:50:00 +0200 Subject: [PATCH 1/2] add support to retrieve the download key from the request --- demos/pygbif-intro.ipynb | 8 +++++--- pygbif/__init__.py | 2 +- pygbif/occurrences/__init__.py | 2 +- pygbif/occurrences/download.py | 16 ++++++++++------ 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/demos/pygbif-intro.ipynb b/demos/pygbif-intro.ipynb index ab222f3..73e89c7 100644 --- a/demos/pygbif-intro.ipynb +++ b/demos/pygbif-intro.ipynb @@ -284,7 +284,9 @@ "collapsed": true }, "outputs": [], - "source": [] + "source": [ + "" + ] } ], "metadata": { @@ -296,7 +298,7 @@ "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 2.0 }, "file_extension": ".py", "mimetype": "text/x-python", @@ -308,4 +310,4 @@ }, "nbformat": 4, "nbformat_minor": 0 -} +} \ No newline at end of file diff --git a/pygbif/__init__.py b/pygbif/__init__.py index 8d67379..5823993 100644 --- a/pygbif/__init__.py +++ b/pygbif/__init__.py @@ -42,6 +42,6 @@ __license__ = 'MIT' from .occurrences import search, get, count, download -from .species import names,name_parser +from .species import names, name_parser from .registry import datasets, nodes from .gbifissues import occ_issues_lookup diff --git a/pygbif/occurrences/__init__.py b/pygbif/occurrences/__init__.py index 55a2e7d..094b1c9 100644 --- a/pygbif/occurrences/__init__.py +++ b/pygbif/occurrences/__init__.py @@ -4,4 +4,4 @@ from .get import get, get_verbatim, get_fragment from .count import count, count_basisofrecord, count_year, \ count_datasets, count_countries, count_schema, count_publishingcountries -from .download import download_meta, download_list, download_get +from .download import download_meta, download_list, download_get, GBIFDownload diff --git a/pygbif/occurrences/download.py b/pygbif/occurrences/download.py index 411b3d3..65ea39f 100644 --- a/pygbif/occurrences/download.py +++ b/pygbif/occurrences/download.py @@ -167,6 +167,7 @@ def __init__(self, creator, email, polygon=None, main_pred_type='and'): 'predicates': self.predicates } } + self.request_id = None # prepare the geometry polygon constructions if polygon: @@ -262,24 +263,27 @@ def add_geometry(self, polygon, geom_type='within'): """ self.predicates.append({'type': geom_type, 'geometry': polygon}) - def post_download(self, user, pwd): + def post_download(self, user=None, pwd=None): """ :param user: Username :param pwd: password :return: """ - pprint.pprint(self.payload) + user = _check_environ('GBIF_USER', user) + pwd = _check_environ('GBIF_PWD', pwd) + + #pprint.pprint(self.payload) r = requests.post(self.url, auth=auth.HTTPBasicAuth(user, pwd), data=json.dumps(self.payload), headers=self.header) - if r.status_code > 203: raise Exception('error: ' + r.content) - if r.headers()['Content-Type'] == 'application/json': - raise Exception('not of type json') - return r.json() + else: + self.request_id = r.text + print('Your download key is ', self.request_id) + return self.request_id def download_meta(key, **kwargs): From 348f3bccef7590e4a5d629209cf1bb0b9d16fffb Mon Sep 17 00:00:00 2001 From: stijnvanhoey Date: Mon, 6 Jun 2016 17:17:10 +0200 Subject: [PATCH 2/2] add handling of download key --- pygbif/occurrences/download.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/pygbif/occurrences/download.py b/pygbif/occurrences/download.py index 65ea39f..1e7d461 100644 --- a/pygbif/occurrences/download.py +++ b/pygbif/occurrences/download.py @@ -129,7 +129,7 @@ def download(*args, user=None, pwd=None, predicate['type']) out = req.post_download(user, pwd) - return [out, user, email] + return (out, user, email) class GBIFDownload(object): @@ -279,12 +279,24 @@ def post_download(self, user=None, pwd=None): data=json.dumps(self.payload), headers=self.header) if r.status_code > 203: - raise Exception('error: ' + r.content) + raise Exception('error: ' + r.text + + ', with error status code ' + + str(r.status_code) + + 'check your number of active downloads.') else: self.request_id = r.text print('Your download key is ', self.request_id) return self.request_id + def get_status(self): + """get the current download status""" + return get_download_status(self.request_id) + + +def get_download_status(request_key): + """get the current download status""" + return download_meta(request_key).get('status') + def download_meta(key, **kwargs): """