Last active
April 9, 2025 17:05
-
-
Save hohonuuli/7a6420added8b79ca62d857e972bb773 to your computer and use it in GitHub Desktop.
An example script to upload a CSV file to FathomNet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import requests | |
import argparse | |
import uuid | |
import json | |
from io import BytesIO | |
from typing import Dict | |
from datetime import datetime, timezone | |
__author__ = "Brian Schlining" | |
__copyright__ = "Copyright 2025, Monterey Bay Aquarium Research Institute" | |
# Exchange API Key for JWT in FathomNet | |
def exchange_api_key_for_jwt(api_key) -> str: | |
headers = { | |
'Accept': 'application/json', | |
'x-api-key': api_key | |
} | |
url = 'https://database.fathomnet.org/api/xapikey/auth' | |
response = requests.post(url, headers=headers) | |
return response.json()['token'] | |
def fetch_csv_data(csv_url: str) -> str: | |
response = requests.get(csv_url, stream=True) | |
return response.text | |
def build_darwincore(collection_code: str) -> Dict[str, str]: | |
""" This should be customized with your own institute's metadata """ | |
return { | |
"uuid": str(uuid.uuid4()), | |
"recordType": "StillImage", | |
"basisOfRecord": "MachineObservation", | |
"license": "https://fathomnet.org", | |
"modified": datetime.now(timezone.utc).isoformat(), | |
"accessRights": "https://database.fathomnet.org/fathomnet/#/license", | |
"collectionCode": collection_code, | |
"ownerInstitutionCode": "FathomNet", | |
"rightsHolder": "FathomNet", | |
"institutionCode": "FathomNet", | |
"language": "en-US" | |
} | |
def upload_file_to_fathomnet(jwt, endpoint, csv_data, darwincore): | |
headers = { | |
'Authorization': 'Bearer ' + jwt | |
} | |
files = { | |
"file": ("file.csv", csv_data.encode(), "text/csv"), | |
"darwincore": ("darwincore.json", json.dumps(darwincore), "application/json") | |
} | |
response = requests.post(endpoint, headers=headers, files=files) | |
print(response.json()) | |
def main(api_key, endpoint, csv_url, collection_code): | |
jwt = exchange_api_key_for_jwt(api_key) | |
csv_data = fetch_csv_data(csv_url) | |
darwincore = build_darwincore(collection_code) | |
upload_file_to_fathomnet(jwt, endpoint, csv_data, darwincore) | |
if __name__ == '__main__': | |
""" | |
Usage: python fathomnet_upload_csv.py <api_key> <endpoint> <csv_url> <collection_code> | |
Example: fathomnet_upload_csv.py MyApiKeyFromFathomNet https://database.fathomnet.org/api/boundingboxes/upload/csv https://someserver.com/path/to/somefile.csv "FathomNet Awesome upload 10" | |
""" | |
parser = argparse.ArgumentParser( | |
prog="test.py", | |
description="Test upload a zip file to FathomNet" | |
) | |
parser.add_argument("api_key", help="API Key for FathomNet") | |
parser.add_argument("endpoint", help="Endpoint to upload file to") | |
parser.add_argument("csv_url", help="URL to csv") | |
parser.add_argument("collection_code", help="name or code of collection") | |
args = parser.parse_args() | |
main(args.api_key, args.endpoint, args.csv_url, args.collection_code) | |
# Upload file to FathomNet |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment