|
| 1 | +from pathlib import Path |
| 2 | +import lib.downloading as dl |
| 3 | +from lib.secrets import secrets |
| 4 | +import requests |
| 5 | + |
| 6 | +def collect(outputFilePath: Path) -> None: |
| 7 | + baseURL = "https://api.gbif.org/v1" |
| 8 | + requestEndpoint = "/occurrence/download/request" |
| 9 | + statusEndpoint = "/occurrence/download" |
| 10 | + |
| 11 | + headers = { |
| 12 | + "Content-Type": "application/json" |
| 13 | + } |
| 14 | + |
| 15 | + formData = { |
| 16 | + "creator": secrets.gbif.creator, |
| 17 | + "notificationAddresses": [ |
| 18 | + secrets.general.email |
| 19 | + ], |
| 20 | + "sendNotification": "false", |
| 21 | + "format": "SIMPLE_CSV", |
| 22 | + "predicate": { |
| 23 | + "type": "and", |
| 24 | + "predicates": [ |
| 25 | + { |
| 26 | + "type": "in", |
| 27 | + "key": "BASIS_OF_RECORD", |
| 28 | + "values": [ |
| 29 | + "MATERIAL_SAMPLE", |
| 30 | + "MATERIAL_CITATION", |
| 31 | + "PRESERVED_SPECIMEN", |
| 32 | + "FOSSIL_SPECIMEN", |
| 33 | + "LIVING_SPECIMEN", |
| 34 | + "OCCURRENCE" |
| 35 | + ] |
| 36 | + }, |
| 37 | + { |
| 38 | + "type": "equals", |
| 39 | + "key": "COUNTRY", |
| 40 | + "value": "AU" |
| 41 | + }, |
| 42 | + { |
| 43 | + "type": "equals", |
| 44 | + "key": "HAS_COORDINATE", |
| 45 | + "value": "true" |
| 46 | + }, |
| 47 | + { |
| 48 | + "type": "equals", |
| 49 | + "key": "HAS_GEOSPATIAL_ISSUE", |
| 50 | + "value": "false" |
| 51 | + }, |
| 52 | + { |
| 53 | + "type": "equals", |
| 54 | + "key": "OCCURRENCE_STATUS", |
| 55 | + "value": "PRESENT" |
| 56 | + }, |
| 57 | + ] |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + strFormData = str(formData).replace(" ", "").replace("'", '"') |
| 62 | + response = requests.post(f"{baseURL}{requestEndpoint}", headers=headers, data=strFormData, auth=dl.buildAuth(secrets.general.email, secrets.gbif.password)) |
| 63 | + requestID = response.text |
| 64 | + |
| 65 | + statusURL = f"{baseURL}{statusEndpoint}/{requestID}" |
| 66 | + dl.asyncRunner(statusURL, "status", "SUCCEEDED", "downloadLink", outputFilePath) |
0 commit comments