Skip to content

Commit

Permalink
add example code for score api get
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanandrews committed Dec 17, 2024
1 parent b213619 commit d3754db
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 2 deletions.
9 changes: 7 additions & 2 deletions habhub-dataserver/habhub/ifcb_datasets/api2/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def list(self, request):
index_name = "species-scores"

# set up initial pagination options
per_page = 100 # result per page returned by OS
query_params = request.query_params.dict()
query_params.pop("search_after", None)
query_params.pop("page", None)
Expand Down Expand Up @@ -102,6 +103,7 @@ def list(self, request):
# use search_after to paginate through all results
response = os_client.search(body=query, index=index_name, size=100)
# print(response)
results = response["hits"]["hits"]
total_hits = response["hits"]["total"]["value"]
# create next/prev link using the "sort" value from OS
last_element = response["hits"]["hits"][-1]
Expand All @@ -119,7 +121,10 @@ def list(self, request):
f"{uri}&search_after={current_search_after}&page={prev_page}"
)

next_link = f"{uri}&search_after={next_sort}&page={next_page}"
if len(results) < per_page:
next_link = None
else:
next_link = f"{uri}&search_after={next_sort}&page={next_page}"

# use scan to get more than 10000 responses
# response = helpers.scan(
Expand All @@ -134,7 +139,7 @@ def list(self, request):
},
"totalHits": total_hits,
"page": current_page,
"results": response,
"results": results,
}
except Exception as err:
print(err)
Expand Down
82 changes: 82 additions & 0 deletions opensearch/api-fetch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import requests

# set your filter parameters
# all parameters are optional, but unfiltered searches may return millions of results so use caution
start_date = "2023-06-01"
end_date = "2023-06-30"
species = "Alexandrium_catenella"
# dataset_id = ""
model_id = "HABLAB_20240110_Tripos2"
score_gte = ".9"
# bbox_sw = "lat,long"
# bbox_ne = "lat,long"


def fetch_paginated_results(api_url, params=None):
"""
Fetch paginated results from an API and combine them into a single response.
Args:
api_url (str): The base URL of the API endpoint.
params (dict, optional): Additional query parameters for the API request.
Returns:
list: A combined list of all results from all pages.
"""
if params is None:
params = {}

all_results = []

while True:
# Update the parameters to
# params.update({"page": page})

# Make the API request
response = requests.get(api_url, params=params)

# Raise an error if the request failed
response.raise_for_status()

# Parse the JSON response
data = response.json()

# Get next/prev links
links = data.get("links", None)
# Check if there are more pages
# Assuming the API provides a "next" field to indicate the next page
if not links:
break

# Get the results
results = data["results"]["hits"]["hits"]
print(results, len(results))
# Add the current page's results to the combined list
all_results.extend(results)

# Update the API URL to get next page of results
api_url = links["next"]
print("Next", api_url)

return all_results


# Example usage
if __name__ == "__main__":
API_URL = "https://habhub-api.whoi.edu/api/v2/ifcb-species-scores/"
PARAMS = {
"start_date": start_date,
"end_date": end_date,
"species": species,
# "dataset_id": dataset_id,
"model_id": model_id,
"score_gte": score_gte,
# "bbox_sw": bbox_sw,
# "bbox_ne": bbox_ne,
} # Adjust parameters as needed

try:
combined_results = fetch_paginated_results(API_URL, params=PARAMS)
print(f"Fetched {len(combined_results)} items.")
except requests.RequestException as e:
print(f"An error occurred: {e}")

0 comments on commit d3754db

Please sign in to comment.